'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
});
};
Solution 1:[1]
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-
- Run the tests using
npm test
. - Open the browser and check the tests, there shouldn't be any broken or failed tests case.
- It shouldn't give any console error (Verify on console and fix it.)
- 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.
Solution 2:[2]
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;
Solution 3:[3]
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
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Neeraj Shende |
Solution 2 | Alex Parloti |
Solution 3 | Maheshvirus |
Solution 4 | Yogesh Borkhade |