When I run ng test --code-coverage, and then run sonar-scanner, still not able to see coverage report on sonar server.
I tried setting up new project using Angular 13 and setting up as per official documentation. Still no luck.
My Sonar server version: Version 9.2.1 (build 49989)
My Sonar scanner version: 4.7
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/lcov'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
My sonar properties file:
sonar.projectKey=UnitTest
sonar.projectName=UnitTest
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8
sonar.host.url=http://localhost:9000
sonar.login=********
sonar.password=********
sonar.sources=src
sonar.tests=src
sonar.exclusions=**/node_modules/**, src/assets/**
sonar.test.inclusions=**/*.spec.ts
sonar.typescript.lcov.reportPaths=coverage/lcov/lcov.info
My sonar-server result:
sonar.javascript.lcov.reportPaths
for configuring report path instead of sonar.typescript.lcov.reportPaths
in your sonar.properties file.lcov.info
file. Make sure your karma configuration is generating lcov.info file at configured path.Using karma-coverage: (Recommended)
karma-coverage
installed. Generally it is pre-installed with new angular project.karma.conf.js
file as below:
{ type: 'lcov', subdir: 'lcov-report' }
under karma-coverage reporters.karma.config.js
should look like below:
// 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'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
jasmine: {
},
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
jasmineHtmlReporter: {
suppressAll: true // removes the duplicated traces
},
coverageReporter: {
dir: require('path').join(__dirname, './coverage'),
subdir: '.',
reporters: [
{ type: 'html', subdir: 'html-report' },
{ type: 'lcov', subdir: 'lcov-report' }
]
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
Using karma-coverage-istanbul-reporter: (Deprecated)
karma-coverage-istanbul-reporter
using npm i -D karma-coverage-istanbul-reporter
command.karma-coverage
package to generate code coverage in lcov.info format.karma.conf.js
file as below:
Add require('karma-coverage-istanbul-reporter')
under plugins.
Set below configuration:
coverageIstanbulReporter: {
dir: require('path').join(__dirname, './coverage/lcov-report'),
reports: ['lcovonly'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml']
karma.config.js
should look like below:
// 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/lcov-report'),
reports: ['lcovonly'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
Now, run ng test --code-coverage --watch=false
command to generate code coverage. (You may also set default code coverage and watch configuration in angular.json file to avoid specifying it every time).
Next step would be telling your sonar qube about this coverage information.
Install sonar scanner using npm i -D sonar-scanner
if you haven't installed yet. (Installing it as dev-dependency will help other developers in your team).
Add sonar.javascript.lcov.reportPaths=coverage/lcov-report/lcov.info
in your sonar-project.properties file to tell your sonar server about your coverage report path.
After updating sonar-project.properties
, it should look like below.
sonar.projectKey=UnitTest
sonar.projectName=UnitTest
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8
sonar.host.url=http://localhost:9000
sonar.login=*********
sonar.password=*********
sonar.sources=src
sonar.tests=src
sonar.exclusions=**/node_modules/**,src/assets/**
sonar.test.inclusions=**/*.spec.ts
sonar.javascript.lcov.reportPaths=coverage/lcov-report/lcov.info
Run your sonar-scanner with updated properties.
I have tested both options with Angular 12 and Angular 13 version. Both seems to work fine. Please let me know if you face any issue.