不满足阈值时 Jest 测试覆盖率不会失败

Jest test coverage does not fail when threshold is not met

利用 create-react-app,当 运行 在我的 CI 管道中进行测试时,如果未达到代码覆盖率阈值,我希望控制台 return 一个非-零响应。

package.json

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "test:coverage": "npm run test -- --coverage --watchAll=false",
  },
  "jest": {
    "collectCoverageFrom": [
      "src/components/**/*.js",
      "src/state/**/*.js",
      "src/templates/**/*.js",
      "src/routes/**/*.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 80,
        "lines": 80,
        "statements": 80
      }
    }
  }

当 运行 test:coverage 控制台报告未达到阈值时,但仍然 returns 0。我从 Jest 文档中理解错误应该是 returned 未达到覆盖阈值时。

https://jestjs.io/docs/en/configuration#coveragethreshold-object

具体...

If thresholds aren't met, jest will fail.

有人熟悉这个问题吗?我经历过 Jest 和 CRA github 问题,结果喜忧参半,大多数发现都与过时的版本有关。

命令失败时停止进一步执行:

command || exit 0

{
  "test:coverage": "npm run test -- --coverage --watchAll=false || exit 0"
}

参考:don't fail jenkins build if execute shell fails

在未达到覆盖率阈值时,在 packjage.json 文件中定义配置不会使 jest 命令失败。但是当我通过 jest.config.js 文件定义它时它起作用了!