如何在GitHub动作中检查测试覆盖率百分比

发布于 2025-02-07 22:35:30 字数 389 浏览 2 评论 0原文

我有一个已发布到Github的React项目,并试图设置GitHub动作。

我想添加一个步骤,以检查单位测试覆盖范围是否通过65%(必须通过它成功完成CI/CD过程)。

这是我尝试的:

build:
    name: Test
    steps:
      - name: Coverage
        run: npm run jest-coverage

我需要帮助以下项目:

  • 如何检查覆盖率百分比是否通过65%?
  • 如果覆盖范围低于65%,则如何使过程失败?
  • 每当有新的提交推动时,我如何才能重新运行它?

I have a react project published to GitHub, and trying to set up GitHub Actions.

I want to add one step to check if the unit test coverage passes 65% (have to pass it to go through CI/CD process successfully).

This is what I've tried:

build:
    name: Test
    steps:
      - name: Coverage
        run: npm run jest-coverage

I need help on following items:

  • How can I check if the coverage percentage passes 65%?
  • How to make the process fail if the coverage is lower than 65%?
  • How can I make it rerun it whenever there's a new commit pushed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2025-02-14 22:35:30

假设您的NPM脚本开玩笑在启用的覆盖范围内运行开玩笑

可以使用Jest Configuration coveragageThreshreshold

将其添加到您的jest.config.js

{
  ...
  "jest": {
    "coverageThreshold": {
      "global": {
        "branches": 65,
        "functions": 65,
        "lines": 65,
        "statements": 65
      }
    }
  }
}

至于在推新代码时重新运行工作,您想在push> push上触发工作流,例如

on: push

jobs:
  ...

:关于您如何使用分支和拉请请求,push也可以用pull_request替换。

Assuming that your npm script jest-coverage runs jest with coverage enabled

Requiring 65% test coverage can be achieved with the jest configuration coverageThreshold.

Add this to your jest.config.js:

{
  ...
  "jest": {
    "coverageThreshold": {
      "global": {
        "branches": 65,
        "functions": 65,
        "lines": 65,
        "statements": 65
      }
    }
  }
}

As for re-running your job when new code is pushed, you want to trigger your workflow on push, like so:

on: push

jobs:
  ...

Depending on how you're working with branches and pull requests, the push could also be replaced with pull_request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文