Skip to content

Jest Coverage Configuration

By default, Jest only calculates coverage for files that were actually imported by a test. This lesson covers collectCoverageFrom and related options for controlling exactly what counts toward your coverage report.

collectCoverageFrom: Including Untested Files

Without collectCoverageFrom, a source file with zero tests simply doesn't appear in the coverage report at all — it's invisible, rather than showing as 0%. This can make a coverage report look better than reality by silently omitting entirely untested files.

collectCoverageFrom explicitly lists (via glob patterns) every source file that should be considered for coverage, whether or not any test happens to import it, ensuring untested files show up as 0% instead of disappearing.

// jest.config.js
module.exports = {
  collectCoverage: true,
  collectCoverageFrom: [
    'src/**/*.{js,jsx,ts,tsx}',
    '!src/**/*.test.{js,jsx,ts,tsx}',
    '!src/**/index.js',
  ],
};

The ! prefix excludes matching files; test files and simple re-export index.js files are commonly excluded.

Excluding Files from Coverage Collection

module.exports = {
  coveragePathIgnorePatterns: [
    '/node_modules/',
    '/dist/',
    '/src/generated/',
  ],
};
  • coveragePathIgnorePatterns excludes matching files/folders from coverage entirely.
  • Commonly excluded: node_modules, build output, and auto-generated code.
  • Excluding generated or vendored code keeps coverage percentages meaningful for code you actually maintain.
  • collectCoverageFrom and coveragePathIgnorePatterns can be combined for fine-grained control.

Coverage Configuration Cheat Sheet

Key options for controlling what counts toward coverage.

Option Purpose
collectCoverage Enables coverage collection by default (without needing --coverage)
collectCoverageFrom Explicitly includes files, even untested ones
coveragePathIgnorePatterns Excludes matching files/folders entirely
coverageDirectory Where coverage reports are written
coverageProvider 'babel' (default) or 'v8' for the underlying instrumentation engine

coverageProvider: babel vs v8

Jest supports two coverage instrumentation engines. The default babel provider instruments code via Babel transforms and tends to be more precise for branch coverage. The v8 provider uses V8's native coverage APIs, which is often faster on large codebases but can be slightly less precise for certain constructs.

// jest.config.js
module.exports = {
  coverageProvider: 'v8', // faster on large codebases
};

Excluding Generated or Vendored Code

Auto-generated files (GraphQL types, protobuf output, build artifacts accidentally included in src/) should be excluded from coverage — they inflate or deflate your real percentage without reflecting anything your team actually writes or maintains by hand.

Common Mistakes

  • Not setting collectCoverageFrom, letting entirely untested files disappear from the report instead of showing as 0%.
  • Forgetting to exclude generated code, skewing coverage percentages in either direction.
  • Including test files themselves in collectCoverageFrom, which distorts the numbers.
  • Switching coverageProvider without re-checking whether existing thresholds still make sense.

Key Takeaways

  • collectCoverageFrom ensures untested files show up as 0% instead of vanishing from the report.
  • coveragePathIgnorePatterns excludes files/folders like node_modules and generated code.
  • coverageProvider lets you choose between Babel-based and V8-native instrumentation.
  • Careful coverage configuration keeps the reported percentage meaningful and trustworthy.

Pro Tip

Always set collectCoverageFrom explicitly on any real project. Without it, a completely untested new module can hide from your coverage report entirely instead of dragging the percentage down where it belongs.