AI-Assisted Testing & Tools
Code Coverage and Quality Metrics
Understand what code coverage measures, how to use it effectively, and what other metrics reveal test quality.
What Code Coverage Measures
Code coverage measures what percentage of your source code is executed when your tests run.
Four types of coverage:
- Line coverage — Which lines of code were executed?
- Branch coverage — Which branches (if/else, ternary) were taken?
- Function coverage — Which functions were called?
- Statement coverage — Which statements executed?
Branch coverage is the most meaningful — it catches logic that is only wrong in certain conditions.
The Coverage Trap
100% coverage does not mean zero bugs.
Coverage measures execution, not correctness. A test that calls a function and asserts nothing will count as coverage. Coverage is a floor, not a ceiling.
Practical Coverage Targets
- Auth, payments, data mutations: 95%+
- Core business logic: 90%+
- API routes: 85%+
- UI components: 70%+
- Configuration files and generated code: Skip
Mutation Testing: Coverage Done Right
Mutation testing answers: "If I introduce a bug, does a test fail?" It automatically introduces small bugs (mutations) and runs your tests. If tests still pass with a bug present, they're not actually catching that bug. Mutation score is a better quality metric than line coverage.
Key Takeaways
- Branch coverage is more meaningful than line coverage — it catches conditional logic bugs
- Set coverage thresholds that fail CI if coverage drops — this creates accountability
- 100% coverage does not mean zero bugs — coverage measures execution, not correctness
- Focus high coverage (95%+) on critical paths: authentication, payments, data mutations
- Mutation testing is the gold standard — it verifies that your tests would actually catch bugs
Example
// vitest.config.ts with coverage thresholds