Test Coverage
Measure code coverage, set thresholds, and interpret what coverage really tells you.
What you'll learn
- What line, branch, function, and statement coverage mean
- How to collect coverage with Vitest and Jest (v8 / istanbul)
- Setting coverage thresholds in CI
- Why 100% coverage does not guarantee bug-free code
Concept
What Coverage Measures
Code coverage instruments your code so the test runner can record which lines, branches, functions, and statements were executed during the test run. Line coverage is the percentage of executable lines that ran. Branch coverage tracks both sides of every if and ternary. Function coverage tracks which functions were called. Statement coverage is similar to line coverage but counts statements, which can differ when multiple statements share a line.
Collecting Coverage
In Vitest, run vitest run --coverage; in Jest, jest --coverage. Both support the V8 instrumentation (fast, native) and the istanbul-style instrumentation (more accurate for edge cases). The output is a table per file plus HTML and LCOV reports you can open in a browser to see exactly which lines are red (uncovered) and green (covered).
Thresholds in CI
Set coverage thresholds so coverage cannot silently drop. In Vitest config, coverage.thresholds.lines = 80 fails the run if line coverage falls below 80%. Enforce thresholds in CI to prevent merges that reduce coverage. Start with a realistic target (70–80%) and raise it gradually; demanding 100% on a legacy codebase blocks productivity without adding safety.
Coverage Is a Signal, Not a Goal
High coverage means code was executed, not that it was meaningfully tested. A test that calls a function with no assertions still counts as covered. 100% coverage does not prove correctness, edge cases, or that the right thing happens for every input. Use coverage to find untested code, but judge test quality by whether tests assert meaningful behavior and catch real bugs.
In Practice
Treat the coverage report as a map of where to look next, not a scoreboard to maximize. When you see a red line, ask "what behavior lives here, and what test would prove it?" — then write a test with real assertions, not a call that merely executes the line. Pay attention to branch coverage especially: a function with 100% line coverage can still have untested if branches that hide bugs. Set a threshold that is achievable today (say 75%) and ratchet it up as the suite matures; a threshold that blocks every PR just gets disabled. In code review, glance at the coverage diff — a PR that drops coverage by 5% deserves a question about which behavior is now untested. Coverage is a useful servant but a terrible master; let it guide you, not drive you.
Example
// vitest.config.ts — coverage configuration
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
coverage: {
provider: "v8", // fast, built into Node
reporter: ["text", "html", "lcov"],
include: ["src/**/*.{ts,tsx}"],
exclude: ["src/**/*.d.ts"],
thresholds: {
lines: 80,
branches: 75,
functions: 80,
statements: 80,
},
},
},
});
// Run: npx vitest run --coverage
// Fails the build if any threshold is not met.
The v8 provider is fastest. Thresholds make coverage a gate: if a PR drops below 80% lines, the CI run fails and the merge is blocked.
Try it
- Code Diff
See which lines a coverage report marks as uncovered.
- JSON Formatter
Read the JSON coverage summary produced for CI dashboards.
Common mistakes
The mistake
Chasing 100% coverage with assertion-free tests
The fix
Coverage without assertions is theater. Write tests that would fail if the code broke, then let coverage follow naturally.
The mistake
Ignoring branch coverage and only watching lines
The fix
Line coverage can be 100% while half the if-branches are untested. Always track branch coverage too.
Practice Test Coverage
6 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.