【发布时间】:2019-11-07 08:55:33
【问题描述】:
我有一个打字稿项目,它通过 Jenkins 管道并并行执行所有功能测试(在构建主容器之后)。在管道结束时 - 我们创建代码覆盖率检查,然后将结果发送到 sonarqube。
这是我的 package.json:
"test": "npm run test:unit && npm run test:component && npm run test:functional",
"test:component": "mocha --reporter mocha-sonarqube-reporter --reporter-options output=tests/coverage/component/test-xcomponent.xml --recursive -r ts-node/register tests/component/*.ts",
"test:functional": "mocha --reporter mocha-sonarqube-reporter --reporter-options output=tests/coverage/functional/test-xfunctional.xml --recursive -r ts-node/register tests/functional/*.ts",
"test:unit": "mocha --reporter mocha-sonarqube-reporter --reporter-options output=tests/coverage/unit/test-xunit.xml --recursive -r ts-node/register tests/unit/*.ts",
"test:unit:nosq": "mocha --recursive -r ts-node/register tests/unit/*.ts",
"lint": "tslint -t verbose --project tsconfig.json -c tslint.json",
"cover": "nyc --report-dir tests/coverage/all npm run test",
"cover:unit": "nyc --report-dir tests/coverage/unit npm run test:unit",
"cover:functional": "nyc --report-dir tests/coverage/functional -x 'app/repositories' -x 'app/entities' -x 'app/utils' --no-clean npm run test:functional"
我的 sonar-project.properties 如下:
sonar.exclusions=**/node_modules/**,**/*.spec.ts,app/entities/**,dependency-check-report/*,tests/coverage/**/*
sonar.tests=tests
sonar.test.inclusions=tests/**/*
sonar.ts.tslintconfigpath=tslint.json
sonar.typescript.lcov.reportPaths=tests/coverage/all/lcov.info
这个设置有两个问题:
我似乎找不到合并不同覆盖文件的方法。我检查了官方的 istanbuljs/nyc GitHub,它声明它可以通过
nyc merge命令组合,但是输出是 .json,sonarqube 需要 xml。我的代码覆盖率只有一半,因为我只发送一个文件而不是合并文件。我目前在 tests/coverage/all 文件夹中出现代码异味错误,因为它认为生成的覆盖范围中缺少字体。我已在
sonar-project.properties文件中排除了该文件夹,并将其包含在 .gitignore 中,但 sonarqube 仍将其报告为代码小。
【问题讨论】:
-
SonarQube 不需要 xml 文件进行 JS 覆盖,它需要 lcov:JavaScript Coverage Results Import
-
@zero298 是的,我知道但我不熟悉有什么区别,我已经将它包含在我的属性中:如上所示:sonar.typescript.lcov.reportPaths=tests/coverage/all/ lcov.info
-
nyc report --reporter=lcov --report-dir=.nyc_coverage应该获取.nyc_output中的每个 JSON 覆盖文件,并将其转换为单个 lcov 文件,该文件由运行时目录中的所有 JSON 报告组成。你可以试试看,看看这是否能给你一个好的报告?您也可以添加--reporter=text以获得 lcov 和即时文本输出以保持理智。 -
嘿@zero298 正确语法:
nyc --reporter lcov --reporter text --report-dir .nyc_coverage report,我在封面测试后添加了它:所以nyc npm run test && nyc --reporter lcov --reporter text --report-dir .nyc_coverage report,我将 sonarqube 属性设置为该路径。如果您想要积分 - 请写下解决方案。 tks.
标签: node.js typescript sonarqube