Code Coverage: What It Measures and What It Misses
Coverage shows which lines the tests execute, not whether they assert anything. Learn the metrics, the traps, and how to use it well.
By Velloreth
What coverage measures
Coverage measures which code the tests execute.
function divide(a, b) {
if (b === 0) {
throw new Error('division by zero');
}
return a / b;
}
test('divides', () => {
expect(divide(10, 2)).toBe(5);
});
This test covers the return line but not the throw. The line coverage is 75%, and the branch coverage is 50%.
Coverage is measured by instrumenting the code and recording which lines or branches run.
The metrics
Line coverage — the percentage of lines executed.
Branch coverage — the percentage of decision paths taken. An if has two branches.
Function coverage — the percentage of functions called.
Statement coverage — the percentage of statements executed.
Branch coverage is the most informative of the four, because it catches the untested path in a conditional.
What coverage misses
It does not measure the assertions. A test that calls a function and asserts nothing still counts as covered.
test('divides', () => {
divide(10, 2); // no assertion, but the line is covered
});
It does not measure the input space. A function with a boundary bug is covered by a test that uses a value in the middle.
It does not measure the integration. A unit test with a mocked repository covers the service, but not the query.
It does not measure the quality. A high coverage with weak assertions is a false sense of safety.
The coverage trap
A coverage target produces tests written to satisfy the number.
// Written to raise coverage, asserts nothing meaningful
test('covers the function', () => {
expect(() => process(data)).not.toThrow();
});
The symptom: coverage rises, and the number of bugs found by the tests does not.
The cause: the metric is easy to game, and a target makes gaming rational.
The fix: use coverage to find a gap, not as a goal.
Using coverage well
Find the untested area. A file with 0% coverage is a file with no tests, which is a real signal.
Look at the uncovered lines, not the percentage. A specific uncovered branch is actionable; a number is not.
Focus on the critical paths. A payment flow should be well covered; a logging helper need not be.
Use a floor, not a target. A gate that fails when coverage drops below 70% prevents a regression without encouraging gaming.
Review the coverage diff on a pull request, which shows whether the new code is tested.
Mutation testing
Mutation testing changes the code slightly and checks whether a test fails.
// Original
return a / b;
// Mutant
return a * b;
If the tests still pass, the mutant survived, which means the tests do not detect that change.
A surviving mutant is a stronger signal than a coverage gap, because it means the tests execute the code but do not verify it.
The cost: mutation testing is slow, because it runs the suite once per mutant. It is worth running on a critical module rather than the whole codebase.
Coverage in CI
Report the coverage on every run, so a trend is visible.
Fail the build when coverage drops below a floor.
Comment the coverage diff on a pull request, so a reviewer sees whether the new code is tested.
Do not fail the build on a small drop. A refactor that removes code can lower the percentage without a real problem.
Exclude the generated code and the test files from the measurement, or the number is misleading.
What to cover
The critical paths. A payment, an authentication, a data migration.
The complex logic. A pricing rule, a state machine, a parser.
The boundaries. An empty input, a maximum, a zero.
The error paths. A failure from a dependency, an invalid input.
The regression. A test for a fixed bug.
What not to chase
A trivial getter. A test that asserts a getter returns the field adds no value.
The generated code. It is not yours to test.
The framework. Do not test that the framework works.
A configuration file. A test that asserts a constant equals itself is noise.
Coverage in a team
Coverage is a social metric as much as a technical one, and how it is used matters more than the number. A team that treats it as a target will produce tests written to satisfy the number, and the coverage will rise while the number of bugs found by the tests does not. A team that treats it as a diagnostic will look at the uncovered lines, decide whether each one matters, and write a test only where the risk justifies it. The most useful practice is to review the coverage diff on a pull request, which shows whether the new code is tested without turning the whole codebase into a number to hit. A floor that fails the build when coverage drops prevents a regression without encouraging gaming, and it is the one gate that is worth having.
Practical guidance
- Use coverage to find a gap, not as a target.
- Prefer branch coverage to line coverage.
- Look at the uncovered lines, not the percentage.
- Focus on the critical paths.
- Use a floor in CI, not a goal.
- Review the coverage diff on a pull request.
- Try mutation testing on a critical module.
- Exclude the generated code.
- Remember coverage measures execution, not correctness.
For the related guides, see unit-testing-basics and integration-vs-unit-tests. For the broader context, test-driven-development-guide and code-review-checklist.
Quotable summary
Coverage measures which code the tests execute, not whether the assertions are meaningful, so a test that calls a function and asserts nothing still counts as covered. Line coverage is the simplest metric and branch coverage is more informative, because an if with only the true branch tested has full line coverage but half branch coverage. A high coverage with weak assertions is a false sense of safety, and a coverage target produces tests written to satisfy the number rather than to find a bug. Use coverage to find an untested area rather than as a target, and a coverage gate in CI is useful as a floor and harmful as a goal.