SamuraiJack
SamuraiJack

Reputation: 5539

How to specify folder for which I want to include files for code coverage in angular?

I am using jasmine and karma for unit testing. Istanbul reporter.

I want to specify which folder is to be considered for code coverage. I want to exclude everything else.

Right now I am totally clueless how the configuration works. the core folder is the only folder I want to consider for code coverage. (had to paste screenshot because firewall doesnt let me upload images)

enter image description here

karma.config:

module.exports = function (config) {
  config.set({
    basePath: '',
    files:['mockData.js', 'mockLocalization.js'],
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      //require('karma-phantomjs-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma'),
      require('karma-scss-preprocessor'),
       require('karma-sonarqube-reporter'),
       require('karma-junit-reporter'),

    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    angularCli: {
      environment: 'dev'
    },
    coverageIstanbulReporter: {

      reports: [ 'html', 'lcov', 'cobertura' ],
      fixWebpackSourcePaths: true,

    },

    junitReporter:{
      useBrowserName: false, 
      outputDir: 'report',
      // will be resolved to basePath (in the same way as files/exclude patterns)
      outputFile: 'karma-report.xml'
    },

    coverageReporter: {
      includeAllSources: false,
      dir: 'coverage',

      reporters: [
        { type: 'html', subdir: 'html' }
      ]
    },

    reporters: ['progress', 'kjhtml','coverage-istanbul'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],    //make sure it is chromeHeadless while running the pipeline otherwise it will fail.
    singleRun: true,
    browserDisconnectTimeout: 20000,
    browserDisconnectTolerance: 3,
    browserNoActivityTimeout: 120000,
    flags: [
      '--disable-web-security',
      '--disable-gpu',
      '--no-sandbox'
    ]
  });
};

Reporter:

enter image description here

EDIT:

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

enter image description here LOOKS GOOD!

BUT

const context = require.context('./core', true, /\.spec\.ts$/);
context.keys().map(context);
const context2 = require.context('./modules', true, /\.spec\.ts$/);
context2.keys().map(context2);

Gives:

enter image description here WHY IS IT INCLUDING ASSETS FOLDER ALL OF A SUDDEN??

Upvotes: 1

Views: 7299

Answers (3)

SamuraiJack
SamuraiJack

Reputation: 5539

As @Chellappan pointed out, the framework was including a library in the code coverage. Because it was referenced in one of my components. I was able to exclude it from code coverage through the following in angular-cli.json

"test": {
    "codeCoverage": {
      "exclude": [
        "src/assets/jqwidgets/**/*",
        "src/assets/jqwidgets-ts/**/*"
      ]
    },

Upvotes: 1

Vinay sharma
Vinay sharma

Reputation: 745

There is a property to exclude specific file from coverages, refer below code from angular.json file

`"test": {
  "builder": "@angular-devkit/build-angular:karma",
  "options": {
    "main": "src/test.ts",
    ...
    "codeCoverageExclude": [
      "src/app/in-memory-data.service.ts",
      "src/test-util/stubs.ts",
      "src/testing/**/*"
    ]
  }
}`

"codeCoverageExclude" property will exclude mentioned files in it.

Also Refer this for supported parameters - https://github.com/angular/angular-cli/blob/v6.0.0-rc.8/packages/%40angular/cli/lib/config/schema.json#L1065-L1267

Upvotes: 0

Chellappan வ
Chellappan வ

Reputation: 27303

Inside your src folder configure your test.js to point only the folder you want to run test

// Then we find all the tests.
const context = require.context('./app/folderyouwantoruntestcas', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

Upvotes: 2

Related Questions