Skip to content

Jest Coverage Exclude Not Working Fix

DodaTech Updated 2026-06-24 2 min read

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 collectCoverageFrom to whitelist source files
  • Use coveragePathIgnorePatterns to 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

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### What's the difference between collectCoverageFrom and coveragePathIgnorePatterns?

collectCoverageFrom is a glob pattern that selects which files to include in coverage. coveragePathIgnorePatterns excludes files from the report. Use collectCoverageFrom to whitelist source directories and coveragePathIgnorePatterns for additional fine-grained exclusion.

Why are node_modules already excluded from coverage?

Jest automatically excludes node_modules from coverage. You don't need to add it manually unless you want to re-include specific packages with collectCoverageFrom.

Can I exclude files based on their extension?

Yes. Use patterns like !**/*.d.ts in collectCoverageFrom or \\.d\\.ts$ in coveragePathIgnorePatterns to exclude type declaration files, test snapshots, or any other file type.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro