【问题标题】:How to run a specific test file in Karma? (Angular 4+)如何在 Karma 中运行特定的测试文件? (角度 4+)
【发布时间】:2019-09-17 16:19:30
【问题描述】:

当我运行ng test 时,它会遍历所有测试文件。

我找到了答案,通过运行提供文件的路径:

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

在 stackoverflow 上,但我猜它已被弃用,我收到此错误:

async() 需要 AsyncTestZoneSpec

这是我的 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
  });

};

有人知道在 Karma for Angular 4+ 中运行特定文件的新方法吗?

【问题讨论】:

    标签: angular testing jasmine karma-runner


    【解决方案1】:

    要运行单个测试文件,请将文件中的describe 块更改为fdescribe,然后使用ng test 照常运行测试。

    您也可以使用fit 单独运行一个测试用例。

    【讨论】:

    • 如何在我的测试文件中做到这一点?
    • 如果你打开你的测试文件(home-modal.component.spec.ts),将会有一个describe块,所有的测试用例都写在里面。将其更改为fdescribe。运行ng test,现在只执行home-modal.component.spec.ts 测试。
    【解决方案2】:

    在您的 test.ts 文件中,您应该有一行

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

    /\.spec\.ts$/ 的最后一部分是用于加载任何以.spec.ts 结尾的查找的正则表达式。

    您可以将其(暂时)更改为/your\.component\.spec\.ts$/,然后正常运行npm test

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 2018-07-28
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      相关资源
      最近更新 更多