如何使用Karma配置在Angular 4中排除单元测试的文件夹(组件)?

时间:2018-02-06 07:20:51

标签: angular karma-jasmine

我使用的是角度cli版本1.4.5及以下是karma.conf.ts文件

module.exports = function (config) {
  config.set({
  basePath: '',
  exclude: [
     "src/app/components/panel/panel.component.spec.ts",
     "src/app/components/accordion/accordion.component.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 
   },
   coverageIstanbulReporter: {
     reports: [ 'html', 'lcovonly' ],
     fixWebpackSourcePaths: true
   },
   angularCli: {
     environment: 'dev'
   },
   reporters: ['progress', 'kjhtml'],
   port: 9876,
   colors: true,
   logLevel: config.DEBUG,
   autoWatch: true,
   browsers: ['Chrome'],
   singleRun: false
 });
};

我甚至在tsconfig.spec.json文件中添加了exclude以排除以选择那些文件进行测试。

{
 .....,
 "include": [
 "**/*.spec.ts",
 "**/*.d.ts"
 ],
 "exclude": [
     "app/components/panel/panel.component.spec.ts",
     "app/components/accordion/accordion.component.spec.ts"
 ]
}

是否遗漏了要排除的内容?

1 个答案:

答案 0 :(得分:3)

更改src/test.ts

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

https://github.com/angular/angular-cli/issues/9026#issuecomment-354475734