Coverage in CI/CD
What you'll learn: How to enforce code coverage standards in your continuous integration pipeline to maintain quality gates without stifling productivity.
Why Coverage Belongs in CI/CD
Once you've established code coverage locally (lessons 2716-2722), the next step is integrating it into your automated build pipeline. This ensures coverage doesn't silently degrade over time as team members add features. Think of it as a quality gate—like a bouncer who checks every commit meets minimum standards before it joins the main codebase.
Setting Coverage Thresholds
Most test runners let you define minimum coverage percentages that must be met:
// package.json (Jest configuration)
{
"jest": {
"coverageThreshold": {
"global": {
"branches": 80,
"lines": 85,
"functions": 80,
"statements": 85
}
}
}
}
When thresholds aren't met, the test command exits with a non-zero code, failing the CI build.
Failing Builds on Coverage Drops
Your CI pipeline should run tests with coverage on every pull request:
# In your CI config (e.g., GitHub Actions, CircleCI)
npm test -- --coverage --coverageReporters=text-summary
If coverage drops below your threshold, the build fails and the pull request cannot merge.
Balancing Strictness with Pragmatism
Too strict? Teams get frustrated writing tests just to hit numbers, leading to meaningless tests. Too lenient? Coverage erodes gradually.
A pragmatic approach: set thresholds at your current coverage level, then incrementally raise them. Allow exceptions for generated code or truly hard-to-test edge cases using coverage ignore comments (from lesson 2722). Focus on meaningful coverage—tests that catch real bugs—not arbitrary percentages.
Key Takeaway: Coverage thresholds in CI/CD prevent quality regression, but they work best when set pragmatically and raised incrementally rather than enforced dogmatically from day one.