What Code Coverage Measures
Running jest --coverage instruments your source files and tracks which parts of the code actually executed while the test suite ran, producing a report that highlights tested and untested code paths across every file included in the run.
Cricket analogy: Running jest --coverage is like a cricket analytics team reviewing match footage to see which parts of the field a bowler's deliveries actually reached, revealing gaps in line and length coverage across an innings.
The Four Coverage Metrics
Jest's coverage report breaks down into four metrics: statement coverage (individual statements executed), branch coverage (both sides of conditionals like if/else exercised), function coverage (functions invoked at least once), and line coverage (lines of source executed) — each surfacing a different kind of gap.
Cricket analogy: The four coverage metrics—statements, branches, functions, lines—are like tracking a bowler across four dimensions: deliveries bowled, variations used (yorkers vs bouncers), overs completed, and total balls faced by batsmen, each telling a different story.
// jest.config.js
module.exports = {
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{js,jsx}', '!src/**/*.stories.js'],
coverageReporters: ['text', 'lcov', 'html'],
coverageThreshold: {
global: {
statements: 80,
branches: 75,
functions: 80,
lines: 80,
},
},
};
/*
$ npx jest --coverage
--------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------|---------|----------|---------|---------|-------------------
All files | 82.14 | 76.92 | 83.33 | 82.14 |
pricing.js | 71.42 | 50.00 | 66.66 | 71.42 | 12-15,22
--------------------|---------|----------|---------|---------|-------------------
*/Enforcing Thresholds in CI
Setting coverageThreshold in jest.config.js causes the Jest process to exit with a failure if any configured metric falls below its percentage, which teams commonly wire into CI so a pull request that reduces coverage below the agreed bar cannot be merged.
Cricket analogy: Setting a coverageThreshold of 80% branches in jest.config.js is like a selection committee requiring a batsman to maintain at least an 80% conversion rate of fifties into centuries before being picked for the next Test series.
The html coverage reporter generates a browsable coverage/lcov-report/index.html you can open locally to see exactly which lines and branches in each file were or weren't executed, color-coded red for uncovered.
100% code coverage does not mean bug-free code — coverage only proves a line executed during tests, not that its output was correctly asserted; a test with no assertions can still achieve full coverage.
- jest --coverage instruments code to measure which lines execute during tests.
- Coverage reports four metrics: statements, branches, functions, and lines.
- coverageThreshold in jest.config.js can fail a CI build if coverage falls below set percentages.
- The html coverage reporter produces a browsable report highlighting uncovered code.
- High coverage percentage does not guarantee correct assertions or bug-free code.
- collectCoverageFrom controls which source files are included in coverage instrumentation.
Practice what you learned
1. What does running jest --coverage produce?
2. What does branch coverage specifically measure?
3. What does setting coverageThreshold in jest.config.js accomplish?
4. Why doesn't 100% code coverage guarantee bug-free code?
5. What does the html coverage reporter provide?
Was this page helpful?
You May Also Like
Testing React Components with React Testing Library
Learn how to test React components the way a user experiences them, using render, screen queries, userEvent, and async assertions.
Test Fixtures and Factories
Learn how to build reusable test fixtures and factory functions to generate consistent, customizable test data across a Jest suite.