Jest Coverage Threshold Not Enforcing Fix
In this tutorial, you'll learn about Jest Coverage Threshold Not Enforcing Fix. We cover key concepts, practical examples, and best practices.
Your Jest coverage thresholds don't fail the build — even when coverage drops below the configured minimum. Tests pass but the coverage report shows values under the threshold.
The Problem
// WRONG — thresholds in wrong config section
// jest.config.js
module.exports = {
coverageThreshold: 80, // Wrong — should be an object
};
No effect — Jest ignores a number value for coverageThreshold
The threshold must be an object with specific metric keys. A plain number is silently ignored.
Step-by-Step Fix
1. Set global coverage thresholds
// RIGHT — proper threshold configuration
// jest.config.js
module.exports = {
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: -10, // Allow up to 10 uncovered statements
},
},
};
2. Set per-file thresholds
// RIGHT — different thresholds per file
module.exports = {
coverageThreshold: {
global: {
branches: 80,
functions: 85,
lines: 85,
statements: 80,
},
'./src/utils/**/*.js': {
branches: 90,
functions: 95,
lines: 95,
statements: 90,
},
'./src/legacy/**/*.js': {
branches: 50,
functions: 60,
lines: 60,
},
},
};
3. Combine with CI enforcement
// package.json — CI script
{
"scripts": {
"test:coverage": "jest --coverage --coverageThreshold='{}'",
"test:ci": "jest --ci --coverage"
}
}
4. Handle edge cases with negative thresholds
// RIGHT — allow a specific number of uncovered items
module.exports = {
coverageThreshold: {
'./src/legacy/**/*.js': {
statements: -5, // Allow 5 uncovered statements
},
},
};
Expected output:
FAIL jest
● Your test suite has coverage thresholds not met:
global: branches: 75% < 80%
./src/utils/helper.js: lines: 70% < 95%
Prevention Tips
- Always use object format for
coverageThreshold - Set separate thresholds for critical and legacy code
- Use negative values to allow specific counts of uncovered items
- Run with
--ciflag for stricter enforcement - Review coverage reports in PR pipelines
Common Mistakes with coverage threshold
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world JEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro