Coverage Strategy
Problem
Teams chase 100% coverage on trivial code while ignoring critical paths, or have no coverage strategy at all. Coverage metrics become a checkbox exercise that doesn’t correlate with actual confidence, allowing high-risk features to ship untested while trivial getters have full coverage.
Solution
Measure which lines of code are executed during tests to identify untested paths. This highlights gaps in your test suite but doesn’t guarantee meaningful tests, so use it as a guide rather than a goal.
Example
This example demonstrates how to configure test coverage thresholds in Jest to enforce minimum coverage requirements.
{
"jest": {
// Enable coverage collection during test runs
"collectCoverage": true,
// Set minimum coverage thresholds - tests fail if below these percentages
"coverageThreshold": {
"global": {
"branches": 70, // Require 70% of conditional branches tested
"functions": 80, // Require 80% of functions called in tests
"lines": 80 // Require 80% of code lines executed
}
}
}
}
Benefits
- Identifies untested code paths and gaps in test coverage.
- Provides measurable metric to track testing progress over time.
- Helps prioritize which areas need more test attention.
- Can enforce minimum coverage thresholds in
CIpipelines. - Highlights dead code that’s never executed in tests or production.
Tradeoffs
- High coverage doesn’t guarantee meaningful or effective tests.
- Can incentivize writing tests that hit lines without testing behavior.
- May encourage testing trivial code while ignoring critical paths.
- Coverage metrics can become a checkbox rather than quality indicator.
- Focusing on coverage numbers can detract from writing valuable tests.