【问题标题】:Sonarqube coverage configuration for React and JestReact 和 Jest 的 Sonarqube 覆盖配置
【发布时间】:2019-01-10 11:21:35
【问题描述】:

我在配置 sonarqube 时遇到问题,无法与 React + Jest 一起正常工作。

我的配置:

my_moudle.sonar.projectBaseDir=front_app my_module.sonar.javascript.file.suffixes=.js,.jsx

my_module.sonar.tests=src

my_module.sonar.test.inclusions=**/__tests__/**my_module.sonar.javascript.lcov.reportPaths=coverage/lcov.info

目前我将 src 文件夹作为测试文件夹,因为我在同一项目文件夹的 tests 文件夹中为每个组件设置了一组测试。 感谢 SonarJS,我的项目得到了适当的覆盖,但我不知道为什么在覆盖度量指标中看不到单元测试的数量。 任何检查的配置将不胜感激。

谢谢, 倒钩

【问题讨论】:

    标签: reactjs sonarqube jestjs sonarjs


    【解决方案1】:

    SonarJS 不对单元测试报告负责,(Relevant FAQ entry) 导入单元测试报告是 SonarQube 的责任。

    我已经设法通过以下设置做到了。

    首先我们需要将 jest 结果转换为sonar consumable format。为此,我使用了以下 npm 模块:jest-sonar-reporter

    我的package.json 的片段:

    "devDependencies": {
       ...
        "jest": "^21.1.0",
        "jest-sonar-reporter": "^1.3.0",
        ...
      },
      "jestSonar": {
        "sonar56x": true,
        "reportPath": "testResults",
        "reportFile": "sonar-report.xml",
        "indent": 4
      }
    

    您现在需要告诉 jest 将此模块用作处理器,并告诉声纳扫描仪使用此模块生成的文件。说明在官方文档中。

    如果您使用的是 create-react-app/react-scripts,则可能需要一些额外的步骤,因为它们不会公开所有配置。

    您需要将 package.json 中 scripts 块中的 test 条目修改为:

    "test": "react-scripts test --env=jsdom --testResultsProcessor ./node_modules/jest-sonar-reporter/index.js
    

    【讨论】:

    • 这真的有效,谢谢!我的设置如下: package.json 中的脚本行:"test:ci": "cross-env CI=true react-scripts test --reporters=default --coverage --testResultsProcessor ./node_modules/jest-sonar-reporter/index.js", package.json 中的 jestSonar 配置 "jestSonar": { "reportPath": ".", "reportFile": "sonar-report.xml", "indent": 4 }, sonar-project.properties:``` sonar.javascript.lcov.reportPaths=coverage/lcov.info 声纳。 testExecutionReportPaths=sonar-report.xml ```
    • 谢谢,我还需要配置 sonar-scan 以包含我在stackoverflow.com/questions/57466369/…中讨论的测试
    猜你喜欢
    • 2020-09-24
    • 2016-02-29
    • 2018-10-07
    • 2017-09-13
    • 2020-06-03
    • 1970-01-01
    • 2021-07-29
    • 2016-03-06
    • 2018-11-17
    相关资源
    最近更新 更多