简体   繁体   English

SonarQube 带有 Jest 单元测试

[英]SonarQube with Jest Unit Tests

I've been trying to find out how to populate SonarQube with both my Jest Unit Tests and the .net Unit Tests .我一直在尝试找出如何使用Jest Unit Tests.net Unit Tests填充SonarQube

I have a local version of SQ 6.7 and all the latest versions of the Javascript and C# Plugins.我有一个本地版本的SQ 6.7和所有最新版本的 Javascript 和 C# 插件。

When it comes to Jest, I have the sonar-jest-reporter to export a test-report.xml file, and also have the lcov.info file being generated.谈到 Jest,我有sonar-jest-reporter导出一个test-report.xml文件,并且还生成了lcov.info文件。

SonarQube is able to read the lcov.info and I see a coverage %, but no matter what I set as include / exclude , or tests path, it will not show the tests associated with the source file. SonarQube 能够读取lcov.info并且我看到了覆盖率 %,但无论我将什么设置为include / exclude或测试路径,它都不会显示与源文件关联的测试。

The file structure is all .js and the .test.js are in the same directory with each module.文件结构全部为.js.test.js与每个模块位于同一目录中。

Any help with pointing me in the correct direction, or others who have encountered and overcome this issue that would be appreciated.任何帮助我指出正确方向的帮助,或者遇到并克服这个问题的其他人,将不胜感激。

Looks like I've figured out the way to make it work.看起来我已经找到了让它工作的方法。 The trick is to have both sonar.sources and sonar.tests point to same directory (because we have both tests and source in the same directory) then use sonar.tests.inclusions to pattern match test files with .test.js or .spec.js extension.诀窍是让sonar.sourcessonar.tests指向同一个目录(因为我们在同一个目录中有测试和源代码)然后使用sonar.tests.inclusions将测试文件与.test.js.spec.js进行模式匹配.spec.js扩展名。

Here's a sample sonar-project.properties which assumes the following:这是一个示例sonar-project.properties ,它假定以下内容:

  1. src/components/Foo/Foo.jsx the main component. src/components/Foo/Foo.jsx主要组件。
  2. src/components/Foo/Foo.spec.jsx the test file. src/components/Foo/Foo.spec.jsx测试文件。
# Source
sonar.sources=src
# Where to find tests file, also src
sonar.tests=src
# But we get specific here
# We don't need to exclude it in sonar.sources because it is automatically taken care of
sonar.test.inclusions=src/**/*.spec.js,src/**/*.spec.jsx,src/**/*.test.js,src/**/*.test.jsx

# Now specify path of lcov and testlog
sonar.javascript.lcov.reportPaths=coverage/jest/lcov.info
sonar.testExecutionReportPaths=coverage/jest/testlog.xml

Now your test files will also show up.现在您的测试文件也将显示。

More information here 更多信息在这里

If coverage is not shown for your js file then try to use absolute path of your lcov or try to use the lcov path as sonar.javascript.lcov.reportPaths=./coverage/jest/lcov.info Not sure if this help you or not but it worked for me.如果您的js文件未显示覆盖率,则尝试使用lcov的绝对路径或尝试将lcov路径用作sonar.javascript.lcov.reportPaths=./coverage/jest/lcov.info不确定这是否对您有帮助或不是,但它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM