Code coverage measures how much source code is executed by tests. Metrics include lines, statements, branches, and functions. High coverage doesn't guarantee quality - focus on testing critical paths and edge cases rather than chasing 100%.
Coverage Metrics:
Interpreting Results:
Practical Guidelines:
Limitations:
# Run tests with coverage
npm test -- --coverage
# Or configure in package.json
{
"scripts": {
"test": "jest",
"test:coverage": "jest --coverage"
}
}
# Jest config for coverage thresholds
// jest.config.js
module.exports = {
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80
},
// Per-file thresholds
'./src/utils/': {
branches: 90,
functions: 90
}
},
collectCoverageFrom: [
'src/**/*.{js,jsx}',
'!src/**/*.test.{js,jsx}',
'!src/index.js'
]
};