带有 Jest 单元测试的 SonarQube [英] SonarQube with Jest Unit Tests

查看:41
本文介绍了带有 Jest 单元测试的 SonarQube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出如何使用我的 Jest 单元测试.net 单元测试来填充 SonarQube.

我有 SQ 6.7 的本地版本以及所有最新版本的 Javascript 和 C# 插件.

说到 Jest,我有 sonar-jest-reporter 导出 test-report.xml 文件,同时生成 lcov.info 文件.

SonarQube 能够读取 lcov.info 并且我看到覆盖率 %,但无论我设置为 include/exclude,或测试路径,它不会显示与源文件关联的测试.

文件结构都是.js.test.js和每个模块在同一个目录下.

任何帮助我指出正确方向的帮助,或者其他遇到并克服了这个问题的人,将不胜感激.

解决方案

看起来我已经找到了让它工作的方法.诀窍是让 sonar.sourcessonar.tests 指向同一个目录(因为我们在同一个目录中有测试和源)然后使用 sonar.tests.inclusions 以模式匹配带有 .test.js.spec.js 扩展名的测试文件.

这是一个示例 sonar-project.properties,它假定以下内容:

  1. src/components/Foo/Foo.jsx 主要组件.
  2. src/components/Foo/Foo.spec.jsx 测试文件.

# 源码sonar.sources=src# 哪里可以找到测试文件,还有 srcsonar.tests=src# 但我们在这里得到具体的# 我们不需要在 sonar.sources 中排除它,因为它会自动处理sonar.test.inclusions=src/**/*.spec.js,src/**/*.spec.jsx,src/**/*.test.js,src/**/*.test.jsx# 现在指定lcov和testlog的路径sonar.javascript.lcov.reportPaths=coverage/jest/lcov.infosonar.testExecutionReportPaths=coverage/jest/testlog.xml

现在您的测试文件也会显示出来.

点击此处了解更多信息

I've been trying to find out how to populate SonarQube with both my Jest Unit Tests and the .net Unit Tests.

I have a local version of SQ 6.7 and all the latest versions of the Javascript and C# Plugins.

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.

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.

The file structure is all .js and the .test.js are in the same directory with each module.

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.

Here's a sample sonar-project.properties which assumes the following:

  1. src/components/Foo/Foo.jsx the main component.
  2. src/components/Foo/Foo.spec.jsx the test file.

# 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

这篇关于带有 Jest 单元测试的 SonarQube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆