有没有办法以 junit.xml 格式生成 Jest/Istanbul 覆盖率报告

lip*_*ipp 8 xml junit istanbul gitlab-ci jestjs

我想使用 Gitlab CI 来跟踪/嵌入合并请求中的覆盖率分析。该gitlab-ci.yml artifacts:reports:junit配置的选择似乎是非常适合这项工作。但是,这要求覆盖 输出为 junit.xml 格式。

我没有找到合适的设置来输出这种格式的覆盖率。我也找不到将 lcov/json/clover 转换为 junit.xml 的工具。

Jef*_*ets 7

可以使用 jest-junit 报告器插件来完成。https://github.com/jest-community/jest-junit

yarn add --dev jest-junit
Run Code Online (Sandbox Code Playgroud)

然后在本地执行这个,看看它是否工作

yarn test --colors --coverage --reporters=default --reporters=jest-junit
Run Code Online (Sandbox Code Playgroud)

您将在 root 中看到一个 junit.xml 文件。

像这样配置你的 .gitlab-ci.yml ,然后在 Gitlab 中查看输出:

test:
  stage: test
  coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/
  artifacts:
    reports:
      junit: junit.xml
  script:
    - yarn test --colors --coverage --reporters=default --reporters=jest-junit
Run Code Online (Sandbox Code Playgroud)

哦,将coverage/ 文件夹和junit.xml 添加到.gitignore,这样它们就不会添加到git repo 中。

这一切似乎在 Create React App 项目中也能正常工作