Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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.

{
  "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"

The command to run it is

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

Thank you for the help

like image 502
Dinosan0908 Avatar asked Sep 27 '18 13:09

Dinosan0908


People also ask

How do you exclude files from code coverage?

To exclude test code from the code coverage results and only include application code, add the ExcludeFromCodeCoverageAttribute attribute to your test class.

How is code coverage used in Istanbul?

Under the hood, this script is using Jest to run all of the tests in the new app. Conveniently for you, Istanbul can be used to provide a coverage report by simply adding the --coverage flag onto the end of the test command like this: react-scripts test --coverage .


1 Answers

Thank you everyone, solution was to add the codeCoverageExclude option in angular.json

   "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"
                ]
              }
            },
like image 174
Dinosan0908 Avatar answered Sep 18 '22 06:09

Dinosan0908