Angular karma code coverage report folder not generated
Asked Answered
O

4

6

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
  });
};
Ovida answered 2/8, 2019 at 8:3 Comment(0)
T
7

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.

Telekinesis answered 2/8, 2019 at 8:39 Comment(6)
No,But the test suites are all green only . There is no error or breakOvida
But i did 'nt check console error , will check again, Thanks,Ovida
Just check on the console, or if you getting error alerts, then resolve all of them. I had the similar issues when fixing them and it had taken me much time to identify the exact problem.Telekinesis
Also, if it does help then don't forget to vote the answer.Telekinesis
Hi , I have got a warning error in console.It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7.Ovida
Try to fix that somehow and you can try with --sourceMap=false option, which will help you to understand error in detail.Telekinesis
J
10

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;
Jeri answered 22/4, 2020 at 9:19 Comment(0)
T
7

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.

Telekinesis answered 2/8, 2019 at 8:39 Comment(6)
No,But the test suites are all green only . There is no error or breakOvida
But i did 'nt check console error , will check again, Thanks,Ovida
Just check on the console, or if you getting error alerts, then resolve all of them. I had the similar issues when fixing them and it had taken me much time to identify the exact problem.Telekinesis
Also, if it does help then don't forget to vote the answer.Telekinesis
Hi , I have got a warning error in console.It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7.Ovida
Try to fix that somehow and you can try with --sourceMap=false option, which will help you to understand error in detail.Telekinesis
S
1

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
Sporozoite answered 23/12, 2019 at 9:19 Comment(0)
M
-1

Add the following config in your Karma.conf.js file

module.exports = function(config) {
  config.set({
   ...
   coverageReporter: {
      dir: require('path').join(__dirname, 'dist/coverage/app-coverage'),
      subdir: '.',
      reporters: [
        { type: 'html' },
        { type: 'text-summary' },
        { type: 'cobertura' }
      ]
    }
   }

and then run :

ng test  --code-coverage=true

It will generate precise code coverage report, now go to the directory dist/coverage/app-coverage and open index.html file in browser. It will provide code coverage for individual files and which code section is need to cover.

Muna answered 20/4, 2022 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.