azn*_*t81 5 node.js typescript sonarqube
我有一个打字稿项目,它通过 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"
Run Code Online (Sandbox Code Playgroud)
我的 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
Run Code Online (Sandbox Code Playgroud)
这个设置有两个问题:
我似乎找不到合并不同覆盖文件的方法。我已经检查了官方 istanbuljs/nyc GitHub,它声明它可以通过nyc merge命令组合,但是输出是 .json 并且 sonarqube 需要 xml。我被我的一半代码覆盖所困,因为我只输出一个文件而不是组合文件。
我目前有来自 tests/coverage/all 文件夹的代码气味错误,因为它认为生成的覆盖范围中缺少字体。我已经在文件中排除了该文件夹,sonar-project.properties并且我也将它包含在 .gitignore 中,但是 sonarqube 仍然将其报告为代码小。
SonarQube 不需要用于 JavaScript 覆盖的 XML 文件,它要求报告采用lcov格式。请参阅 SonarQube 的文档:JavaScript Coverage Results Import。
为了生成此 lcov 报告,您可以执行以下操作:
__coverage__全局写入的内容)放在一个目录中,默认为.nyc_outputnyc report --reporter=lcov --report-dir=.nyc_coveragenyc表示您希望使用--report-dir(.nyc_coverage在本例中)指定的目录中的所有文件生成报告,并且您希望以--reporter(lcov在本例中)指定的格式生成报告nyc将创建一个文件夹(.nyc_output默认情况下)并在那里写入 lcov 文件如果您愿意,您还可以添加额外的记者以保持理智。我通常会添加,--reporter=text以便它也会打印出覆盖范围。
所以你的最终命令可能是:
nyc report \
--reporter=lcov \
--reporter=text \
--report-dir=.nyc_coverage
Run Code Online (Sandbox Code Playgroud)
将=是可选的,命令参数可以先子命令,所以你也可以运行你记下的命令:
nyc --reporter lcov --reporter text --report-dir .nyc_coverage report
Run Code Online (Sandbox Code Playgroud)
此外,您可以通过在命令行上指定报告来告诉 SonarQube 报告的位置:
sonar-scanner \
-Dsonar.projectKey=whatever \
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
Run Code Online (Sandbox Code Playgroud)
或者您可以在项目设置中进行设置:
Project -> Administration -> JavaScript -> Tests and Coverage -> LCOV Files
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4109 次 |
| 最近记录: |