This project enforces minimum test coverage thresholds to maintain code quality and prevent regression. Coverage is automatically checked on every pull request and push to main/develop branches.
All metrics must meet or exceed 30% coverage:
- Branches: 30%
- Functions: 30%
- Lines: 30%
- Statements: 30%
npm run test:coverageThis will:
- Run all tests with coverage collection
- Generate multiple report formats (text, HTML, LCOV, JSON)
- Display coverage summary in terminal
- Create detailed HTML report in
coverage/lcov-report/index.html
# After running coverage
open coverage/lcov-report/index.html # macOS
start coverage/lcov-report/index.html # Windows
xdg-open coverage/lcov-report/index.html # LinuxThe HTML report shows:
- File-by-file coverage breakdown
- Line-by-line coverage visualization
- Uncovered code highlighted in red
- Partially covered branches in yellow
Coverage is enforced through GitHub Actions:
- On Pull Request: Coverage workflow runs automatically
- Threshold Check: Jest validates all metrics meet 30% minimum
- Build Status:
- ✅ Pass: Coverage meets thresholds, PR can merge
- ❌ Fail: Coverage below thresholds, PR blocked
The .github/workflows/coverage.yml workflow:
- Runs on every PR and push to main/develop
- Executes
npm run test:coverage:ci - Uploads coverage reports as artifacts (30-day retention)
- Fails the build if thresholds not met
Percentage of executable statements that were executed during tests.
Percentage of conditional branches (if/else, switch, ternary) that were tested.
Percentage of functions that were called during tests.
Percentage of code lines that were executed during tests.
npm run test:coverage
# Look for files with low coverage percentagesFocus on:
- Critical business logic
- Error handling paths
- Edge cases
- Conditional branches
// Example: Testing both branches
describe('validateAmount', () => {
it('should accept valid amounts', () => {
expect(validateAmount(10)).toBe(true);
});
it('should reject negative amounts', () => {
expect(validateAmount(-5)).toBe(false);
});
});npm run test:coverage
# Check that coverage percentages increasedDisplayed after running tests, shows overall percentages.
Interactive report with file navigation and line-by-line coverage.
Machine-readable format for CI/CD integration and coverage tools.
Programmatic access to coverage data.
- ✅ Run coverage before submitting PRs
- ✅ Test critical business logic thoroughly
- ✅ Cover error handling paths
- ✅ Test edge cases and boundary conditions
- ✅ Aim for meaningful tests, not just coverage numbers
- ❌ Don't write tests just to hit coverage numbers
- ❌ Don't ignore failing coverage checks
- ❌ Don't commit coverage reports to git (they're in .gitignore)
- ❌ Don't lower thresholds to pass checks
Problem: CI fails with "Coverage threshold not met"
Solution:
- Run
npm run test:coveragelocally - Check which files have low coverage
- Add tests for uncovered code
- Verify coverage meets thresholds
- Commit and push changes
Problem: No coverage directory created
Solution:
# Clean and reinstall
rm -rf node_modules coverage
npm install
npm run test:coverageProblem: Tests succeed but coverage check fails
Solution: This is expected behavior. Coverage thresholds are independent of test results. Add more tests to increase coverage.
coverageThreshold: {
global: {
branches: 30,
functions: 30,
lines: 30,
statements: 30,
},
}{
"scripts": {
"test:coverage": "jest --coverage",
"test:coverage:ci": "jest --coverage --ci --maxWorkers=2"
}
}- Increase thresholds to 40% as coverage improves
- Add per-file coverage requirements for critical modules
- Generate coverage badges for README
- Target 50-60% overall coverage
- Implement coverage diff reporting (show coverage change in PRs)
- Add coverage trends tracking
- Achieve 70%+ coverage for production code
- 90%+ coverage for critical business logic
- Integrate with code review tools
If you encounter issues with coverage:
- Check this guide for troubleshooting steps
- Review the CI Pipeline docs
- Open an issue with coverage report output