principletypescriptModerate
Test coverage: branch coverage is more meaningful than line coverage
Viewed 0 times
branch coverageline coveragestatement coverageistanbulc8coverage threshold
Problem
Teams target 80% line coverage and feel safe. A function with
if (condition) { doA(); } else { doB(); } shows 100% line coverage even if only one branch is ever executed. Bugs in the untested branch ship to production.Solution
Configure Jest/Istanbul/c8 to report branch coverage (
--coverage --coverageThreshold '{"global":{"branches":80}}'). Branch coverage measures whether each branch of conditionals, ternaries, and short-circuits has been executed. Statement and function coverage are table stakes; branch coverage is the meaningful signal.Why
Branch coverage uncovers untested conditional paths — the most common location for real bugs. A statement-covered but branch-uncovered function is a false sense of security. Note: 100% branch coverage still does not guarantee correctness; it only guarantees the code ran.
Gotchas
- Optional chaining
?.and nullish coalescing??create implicit branches that require tests for both null and non-null paths - Default parameter values create branches; test both supplied and missing values
- Coverage thresholds should ratchet upward over time — never let them decrease in CI
- Mutation testing (Stryker) provides a more rigorous measure of test quality than any coverage metric
Revisions (0)
No revisions yet.