Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular karma code coverage report folder not generated

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
  });
};
like image 679
Raj Kumar Avatar asked Dec 23 '22 22:12

Raj Kumar


2 Answers

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;
like image 149
Alex Parloti Avatar answered Dec 28 '22 10:12

Alex Parloti


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.

like image 28
Neeraj Shende Avatar answered Dec 28 '22 10:12

Neeraj Shende