Angular 12 / 13:无法将代码覆盖率报告与 sonarqube 集成
当我运行 ng test --code-coverage,然后运行 sonar-scanner 时,仍然无法在声纳服务器上看到覆盖率报告。
我尝试使用 Angular 13 设置新项目并按照官方文档进行设置。还是没有运气。
我的声纳服务器版本:版本 9.2.1(内部版本 49989)
我的声纳扫描仪版本:4.7
我的 Karma 配置
// 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
});
};
我的声纳属性文件:
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
我的声纳服务器结果:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sonar.javascript.lcov.reportPaths
配置报告路径,而不是使用sonar.typescript.lcov.reportPaths
。使用 karma-coverage:(推荐)
karma-coverage
。通常它是预安装新的 Angular 项目的。karma.conf.js
文件,如下所示:{ type: 'lcov', subdir: 'lcov-report' }
。karma.config.js
应如下所示:使用 karma-coverage-istanbul-reporter:(已弃用)
npm i -D karma-coverage-istanbul-reporter
命令安装karma-coverage-istanbul-reporter
。karma-coverage
包来生成 lcov.info 格式的代码覆盖率。karma.conf.js
文件,如下所示:在插件下添加
require('karma-coverage-istanbul-reporter')
。设置以下配置:
karma.config.js
应如下所示:现在,运行
ng test --code-coverage --watch=false
命令来生成代码覆盖率。 (您还可以在 angular.json 文件中设置默认代码覆盖率和监视配置,以避免每次都指定它)。下一步是告诉您的声纳 qube 此覆盖范围信息。
如果您尚未安装,请使用
npm i -D sonar-scanner
安装声纳扫描仪。 (将其安装为开发依赖项将有助于团队中的其他开发人员)。在您的 sonar-project.properties 文件中添加
sonar.javascript.lcov.reportPaths=coverage/lcov-report/lcov.info
来告诉您的声纳服务器您的覆盖报告路径。更新
sonar-project.properties
后,它应如下所示。使用更新的属性运行声纳扫描仪。
我已经使用 Angular 12 和 Angular 13 版本测试了这两个选项。两者似乎都工作正常。如果您遇到任何问题,请告诉我。
sonar.javascript.lcov.reportPaths
for configuring report path instead ofsonar.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:Using karma-coverage-istanbul-reporter: (Deprecated)
karma-coverage-istanbul-reporter
usingnpm 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:
karma.config.js
should look like below: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.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.
除了 Yash Mochi 的答案 中的 karma.config.js 更改之外,我还必须将 angular.json 文件编辑为将其指向我的业力配置。这是我添加到 angular.json 中的内容:
现在,测试期间实际使用了 karma.config.js 中的设置,并且代码覆盖率报告按照我指定的方式进行格式化。
另请注意,您必须使用
--code-coverage
运行ng test
,或者将"codeCoverage": true
添加到测试选项angular.json,否则根本不会生成覆盖范围。In addition to the karma.config.js changes from Yash Mochi's answer, I also had to edit my angular.json file to point it at my karma config. This is what I added to angular.json:
Now the settings from karma.config.js are actually used during testing, and the code coverage report is formatted in the ways I specified.
Also note that you must to either run
ng test
with--code-coverage
, or add"codeCoverage": true
to the test options in angular.json, otherwise coverage won't be generated at all.