Branch Coverage vs Line Coverage
You'll learn why measuring which conditional paths are tested is more valuable than simply checking which lines run.
The Core Difference
When you look at a coverage report, you'll see two key metrics: line coverage and branch coverage. Line coverage tells you what percentage of your code lines were executed during tests. Branch coverage tells you what percentage of conditional paths (if/else, ternary operators, switch cases, logical operators) were tested.
Here's the critical insight: you can achieve 100% line coverage while leaving entire conditional paths completely untested.
Why This Matters
Consider this mental model: Line coverage is like checking if you've walked down every street in a neighborhood. Branch coverage is like checking if you've explored every possible turn at each intersection.
Imagine a function with an if statement:
function process(value) {
if (value > 10) {
return "high";
}
return "low";
}
A test with process(15) gives you 100% line coverage — every line runs. But branch coverage is only 50% because you never tested the value <= 10 path. The return "low" line executed, but not because the condition was false — it's just where execution lands after the if-block.
Catching Real Bugs
Branch coverage catches:
- Untested
elsebranches - Short-circuit logic in
&&and||expressions - Missing error-handling paths
- Edge cases where conditions evaluate differently
Key Takeaway: Branch coverage reveals untested decision points in your code, catching bugs that line coverage misses. Aim for high branch coverage to ensure all conditional paths — not just all lines — are verified.