4
votes

When I run ng test --code-coverage, The coverage report is sometimes not generating, sometimes it is generating so I'm unable to verify the coverage statement after doing test suites.

My Karma configuration

// 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: '',
    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
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
3

3 Answers

4
votes

The reason, your code coverage is not generating is that, there are some broken unit tests which stops the generation of the code coverage folder. Even though, it is generates some times, but that also must be taking much time then usual. Follow the below steps, in order to identify and fix the unit tests so that, code-coverage file will generate each time-

  1. Run the tests using npm test.
  2. Open the browser and check the tests, there shouldn't be any broken or failed tests case.
  3. It shouldn't give any console error (Verify on console and fix it.)
  4. Verify that all the unit tests are running and passing successfully. Sometimes it shows passed unit tests, but it gives some popup errors or console errors, so these tests prevents the unit test code coverage generation.

Fix all the tests and it should generate the unit tests code coverage folder everytime.

4
votes

In my case everything seemed correct, but no coverage files or messages were generated on the console.

What solved the problem was editing angular.json, adding the following key:

projects.ClientApp.test.options.codeCoverage = true;
1
votes

Generate fresh code coverage folder through following command

node --max_old_space_size=4096 ./node_modules/karma/bin/karma start ./test-config/karma.conf.js --coverage