What is code coverage?
JavaScriptThe short answer
Code coverage measures how much of your code is executed when your tests run. It is expressed as a percentage — 80% coverage means 80% of your code lines were hit by at least one test. It helps you find untested code, but high coverage does not guarantee good tests.
Types of coverage
- Line coverage — what percentage of code lines were executed
- Branch coverage — what percentage of if/else branches were taken
- Function coverage — what percentage of functions were called
- Statement coverage — what percentage of statements were executed
Branch coverage is the most important because it catches untested conditional logic:
function getDiscount(total) { if (total > 100) { return total * 0.1; } return 0;}// A test that only tests total = 150 gives:// Line coverage: 100% (all lines ran)// Branch coverage: 50% (only the true branch)You need a test with total = 50 to cover the other branch.
Running coverage
In Jest:
jest --coverageThis generates a report showing which lines and branches are covered.
What is a good coverage number?
- 80% is a common target for most projects
- 100% is usually not worth pursuing — the last 10-20% requires a lot of effort for diminishing returns
- Some code (error handling, edge cases) is hard to test and may not justify the effort
The more important question is: are the critical paths well tested? 80% coverage with good tests is better than 100% coverage with shallow tests.
Common Pitfalls
High code coverage does not mean your tests are good. You can have 100% coverage with tests that never check the output. Coverage tells you what code ran, not whether the code works correctly. A test that calls a function but does not assert anything gives you coverage but no confidence.
Interview Tip
Make the point that coverage is a useful metric but not the goal. The goal is confidence that your code works. Mention branch coverage specifically — it catches more bugs than line coverage alone. If you can say "I aim for 80% coverage but focus on testing critical paths," that shows a practical mindset.
Why interviewers ask this
This tests your understanding of testing quality. Interviewers want to see if you know what coverage measures, what it does not measure, and what a reasonable target is. Candidates who chase 100% coverage or ignore coverage entirely both raise flags — the ideal answer shows a balanced, practical approach.