简体   繁体   English

如何从Code Coverage Istanbul Reporter中排除模拟文件

[英]How to exclude mock files from Code Coverage Istanbul Reporter

I have a question regarding Istanbul Reporter used for reporting my unit testing coverage in my angular 6 application. 我有一个关于Istanbul Reporter的问题,用于在我的角度6应用程序中报告我的单元测试覆盖率。

My problem is: when the coverage is rendered, I see the mocks in the tested files list and obviously the mocks aren't tested, which gives me wrong coverage stats. 我的问题是:当覆盖范围呈现时,我看到测试文件列表中的模拟,显然模拟没有经过测试,这给了我错误的覆盖统计数据。

This is my karma.conf file setup by a colleague and I'd like to know if you have any idea on how to exclude those mock files. 这是我的同事设置的karma.conf文件,我想知道你是否知道如何排除那些模拟文件。

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: {
      dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'local'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

I saw on StackOverflow that it might be done by adding an exclude in the tsconfig.spec.json but even by re-running the code coverage, it still includes them. 我在StackOverflow上看到它可以通过在tsconfig.spec.json中添加一个exclude来完成,但即使重新运行代码覆盖,它仍然包含它们。

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/spec",
    "module": "commonjs",
    "types": [
      "jasmine",
      "node"
    ],
    "typeRoots": [ "../node_modules/@types" ]
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ],
  "exclude": [
    "/**/*mock*.ts"
  ]
}

My mock files are inside tests /mocks folder in every module/feature and are called "mock-whatevertheymock.ts" 我的模拟文件位于每个模块/功能的tests / mocks文件夹中,称为“mock-whatevertheymock.ts”

The command to run it is 运行它的命令是

test:wc-dogs": "ng test --project=wc-dogs--code-coverage

Thank you for the help 感谢您的帮助

Thank you everyone, solution was to add the codeCoverageExclude option in angular.json 谢谢大家,解决方法是在angular.json中添加codeCoverageExclude选项

   "test": {
              "builder": "@angular-devkit/build-angular:karma",
              "options": {
                "main": "projects/wc-claims/src/test.ts",
                "polyfills": "projects/wc-claims/src/polyfills.ts",
                "tsConfig": "projects/wc-claims/tsconfig.spec.json",
                "karmaConfig": "projects/wc-claims/karma.conf.js",
                "styles": [
                  "projects/wc-claims/src/styles.css"
                ],
                "scripts": [],
                "assets": [
                  "projects/wc-claims/src/favicon.ico",
                  "projects/wc-claims/src/assets"
                ],
                "codeCoverageExclude": [
                    "/**/*mock*.ts"
                ]
              }
            },

You should add exclude to karma config. 您应该将排除添加到karma配置。

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: {
      dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'local'
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    exclude: ["/**/*mock*.ts"],
    singleRun: false
  });
};

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

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