gpt4 book ai didi

node.js - 使用 istabuljs/nyc 合并 Sonarqube 的覆盖范围

转载 作者:行者123 更新时间:2023-12-01 15:01:00 26 4
gpt4 key购买 nike

我有一个 typescript 项目,它通过 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 不需要用于 JavaScript 覆盖的 XML 文件,它要求报告位于 lcov 格式。请参阅 SonarQube 的文档:JavaScript Coverage Results Import .

    为了生成此 lcov 报告,您可以执行以下操作:

  • 将所有 JSON 覆盖数据(浏览器/线束将 __coverage__ 全局写入的内容)放在一个目录中,默认为 .nyc_output
  • 运行命令 nyc report --reporter=lcov --report-dir=.nyc_coverage
  • 这告诉 nyc您希望使用 --report-dir 指定的目录中的所有文件生成报告。 (在本例中为 .nyc_coverage)并且您希望报告采用 --reporter 指定的格式( lcov 在这种情况下)
  • nyc将创建一个文件夹(默认为 .nyc_output)并在那里写入 lcov 文件

  • 如果您愿意,您还可以添加额外的记者以保持理智。我一般加 --reporter=text这样它也会打印出覆盖范围。

    所以你的最终命令可能是:
    nyc report \
    --reporter=lcov \
    --reporter=text \
    --report-dir=.nyc_coverage
    =是可选的,命令参数可以在子命令之前,因此您还可以运行您注意到的命令:
    nyc --reporter lcov --reporter text --report-dir .nyc_coverage report

    此外,您可以通过在命令行上指定报告来告诉 SonarQube 报告的位置:
    sonar-scanner \
    -Dsonar.projectKey=whatever \
    -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info

    或者您可以在项目设置中进行设置:
    Project -> Administration -> JavaScript -> Tests and Coverage -> LCOV Files

    关于node.js - 使用 istabuljs/nyc 合并 Sonarqube 的覆盖范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56755397/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com