Coverage Below Threshold
错误信息
Jest: Coverage threshold 不 met — 分支: 75% < 80%
快速摘要
你的测试 suite 通过, 但 coverage 是 below 配置 threshold. 新 code 是添加不使用 sufficient 测试.
为什么发生
新 code 是添加不使用 corresponding 测试.
Existing 测试是 移除 or skipped.
新分支 (如果/else) 是添加不使用测试所有 paths.
coverage threshold 是设置也 高 for current codebase.
最小示例
// New function added without tests
function newFeature(x) {
if (x > 0) return 'positive';
return 'non-positive';
} // Coverage drops below threshold// Add tests for the new function
test('newFeature returns positive for positive input', () => {
expect(newFeature(5)).toBe('positive');
});
test('newFeature returns non-positive for zero/negative', () => {
expect(newFeature(0)).toBe('non-positive');
expect(newFeature(-1)).toBe('non-positive');
});添加测试 covering 两者都分支的 新函数到 meet coverage threshold.
如何诊断
运行 jest --coverage 到 see coverage report and identify uncovered code.
Look for files 使用低 coverage 百分比.
检查如果新 code 是 recently 添加不使用测试.
Review 哪个分支 or 行是 uncovered.
如何修复
编写测试 for uncovered code — focus on meaningful 测试, 不只是 coverage.
如果 threshold 是 unrealistic, lower 它 in Jest 配置.
如果 uncovered code 是 unreachable or 不 worth 测试, 添加 /* istanbul ignore */ comment.
如何预防
编写测试 alongside 新 code (or 之前, 使用 TDD).
设置 realistic coverage thresholds — 80% 是常见开始点.
运行 coverage locally 之前 pushing 到 CI.
相关资源
相关课程
- 测试 Coverage
Measuring and interpreting coverage.