Skip to content

测试 Coverage Threshold

一句话解释

Coverage thresholds 是最小值 coverage 百分比配置 in 你的测试 runner — 如果 coverage drops below, 构建失败.

用大白话说

coverage threshold 是规则那个 says 'coverage 必须是 at 最少 X%.' 如果 coverage drops below 那个 level, 测试运行失败 — 偶数如果所有测试通过.

This prevents coverage 从 gradually declining over 时间. 不使用 thresholds, team 可能添加新 code 不使用测试, and coverage 缓慢地 erodes. 使用 thresholds, 任何新 code 不使用测试 causes 构建到 失败, forcing developers 到 maintain coverage.

You 配置 thresholds in Jest 如这个:

"jest": {
 "coverageThreshold": {
 "全局": {
 "分支": 80,
 "函数": 80,
 "行": 80,
 "语句": 80
 }
 }
}

Be careful: 设置 thresholds 也高 (如 100%) 能 lead 到 coverage-chasing — 编写 meaningless 测试只是到 hit 数字. 设置 realistic thresholds and focus on meaningful 测试.

示例

json
"jest": {
  "coverageThreshold": {
    "global": {
      "branches": 80,
      "lines": 80,
      "functions": 80,
      "statements": 80
    }
  }
}

如果任何 coverage 类型 drops below 80%, 测试运行失败.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表