简体   繁体   English

如何在Karma中运行特定的测试文件? (角度4+)

[英]How to run a specific test file in Karma? (Angular 4+)

When I run ng test , it goes through all test files. 当我运行ng test ,它将遍历所有测试文件。

I found an answer, by running the providing the path of the file: 通过运行提供文件的路径,我找到了答案:

ng test --main src/app/pages/home-modal/home-modal.component.spec.ts

on stackoverflow but I guess it was deprecated and I received this error: 在stackoverflow上,但是我想它已被弃用,并且我收到此错误:

AsyncTestZoneSpec is needed for the async() async()需要AsyncTestZoneSpec

Here is my karma.config.ts file: 这是我的karma.config.ts文件:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true
  });

};

Does anyone know the new way of running a specific file in Karma for Angular 4+? 有人知道在Karma for Angular 4+中运行特定文件的新方法吗?

In your test.ts file you should have a line for 在您的test.ts文件中,您应该有一行

const context = require.context('./', true, /\.spec\.ts$/);

That last part of /\\.spec\\.ts$/ is regex for load any find that ends with .spec.ts . /\\.spec\\.ts$/最后一部分是正则表达式,用于加载以.spec.ts结尾的所有查找。

You can change this (temporarily) to be /your\\.component\\.spec\\.ts$/ and then run npm test as normal 您可以(暂时)将其更改为/your\\.component\\.spec\\.ts$/ ,然后正常运行npm test

To run a single test file, change the describe block in the file to fdescribe and run the tests as usual using ng test . 要运行单个测试文件,请将文件中的describe块更改为fdescribe ,然后像往常一样使用ng test运行ng test

You can also use fit to run a single test case alone. 您还可以使用fit单独运行一个测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM