Jest Coverage Exclude Not Working Fix
In this tutorial, you'll learn about Jest Coverage Exclude Not Working Fix. We cover key concepts, practical examples, and best practices.
Your coverage report includes files you don't want — test files, configuration files, or vendor code are counted in the coverage percentage.
The Problem
// WRONG — using the wrong config key
// jest.config.js
module.exports = {
coveragePathIgnorePatterns: ['node_modules'], // Already excluded by default
// But test utilities in src/test-utils/ are still included
};
Coverage includes files from src/test-utils/, src/config/, and other non-production code, inflating your coverage numbers or making them unreliable.
Step-by-Step Fix
1. Use collectCoverageFrom to include only specific files
// RIGHT — only collect coverage from production source files
module.exports = {
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/**/*.test.*',
'!src/**/*.spec.*',
'!src/test-utils/**',
'!src/config/**',
'!src/types/**',
],
};
2. Exclude with coveragePathIgnorePatterns
// RIGHT — additional exclusion patterns
module.exports = {
coveragePathIgnorePatterns: [
'/node_modules/',
'/test-utils/',
'/__mocks__/',
'/__fixtures__/',
'\\.d\\.ts$',
'index\\.ts$', // Barrel files
],
};
3. Use both together for precise control
// RIGHT — full coverage configuration
module.exports = {
collectCoverage: true,
collectCoverageFrom: [
'src/**/*.{js,ts}',
'!src/**/*.test.*',
'!src/**/*.spec.*',
'!src/legacy/**',
],
coveragePathIgnorePatterns: [
'/node_modules/',
'/coverage/',
'/__tests__/',
'/__mocks__/',
],
};
4. Verify with coverage report
npx jest --coverage --verbose
# Check the coverage report to confirm excluded files are absent
Expected output:
File | % Stmts | % Branch | % Funcs | % Lines
----------------------|---------|----------|---------|---------
All files | 85 | 78 | 82 | 84
src/utils/helper.js | 90 | 85 | 88 | 89
src/services/api.js | 95 | 90 | 92 | 94
No test files, config files, or mock files appear in the report.
Prevention Tips
- Use
collectCoverageFromto whitelist source files - Use
coveragePathIgnorePatternsto blacklist specific paths - Combine include and exclude patterns for precision
- Verify exclusions by inspecting the HTML coverage report
- Keep patterns in sync with project structure changes
Common Mistakes with coverage exclude
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
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