为什么Karma配置文件的“排除”选项不起作用?

Sur*_*esh 6 karma-runner angular

我的示例Angular应用中有两个规格文件。规范文件的名称为 src/app/app.component.spec.tssrc/app/app.component-two.spec.ts。我只想运行file中的测试src/app/app.component.spec.ts。因此,我在文件中添加了另一个文件作为排除karma.conf.js文件(粘贴在下面),但仍然执行exclude选项中提到的测试。任何帮助是极大的赞赏。

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    exclude: [
      'src/app/app.component-two.spec.ts'
    ],
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
Run Code Online (Sandbox Code Playgroud)

Max*_*kyi 7

您可以排除以下文件中的文件tsconfig.spec.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": { ... },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ],
  "exclude": [
    "app/app.component-two.spec.ts"    <------------------
  ]
Run Code Online (Sandbox Code Playgroud)

  • 对我不起作用:_Module 构建失败:错误:TypeScript 编译中缺少 &lt;file&gt;.spec.ts。请通过“文件”或“包含”属性确保它在您的 tsconfig 中。_,后跟脚本错误。 (3认同)
  • 正确答案位于@zulu 的链接下。在 Angular 项目中,测试文件实际上是在 `src/test.ts` 中选择的,而不是在 `karma.conf.js` 中选择的。 (2认同)