SupercovBlog

Guide

Jest coverage exclude: files, folders and lines

The patterns and comments that work in Jest 30.5, and five that look right but are not, each one measured.

Include first. Exclude after. A card: ['src/**/*.js', '!src/generated/**'] leaves generated out; the negation first adds it back.

List your source in collectCoverageFrom and put ! patterns after it: ['src/**/*.js', '!src/generated/**']. coveragePathIgnorePatterns works too, but it takes regular expressions, not globs. To skip lines, use /* istanbul ignore next */ on the default provider.

Exclude patterns at a glance

To excludeWrite
A foldercollectCoverageFrom: ['src/**/*.js', '!src/generated/**']
A folder, as a regular expressioncoveragePathIgnorePatterns: ['/node_modules/', '/src/generated/']
One file'!src/legacy/old-pricing.js' in collectCoverageFrom
From the command line--collectCoverageFrom='src/**/*.js' --collectCoverageFrom='!src/generated/**'
A function or the next statement/* istanbul ignore next */
An if branch/* istanbul ignore if */
Lines, with coverageProvider: 'v8'/* c8 ignore start */ … /* c8 ignore stop */

We ran every setup here with Jest 30.5.2 and Node.js 24. The project has four source files; no test imports src/legacy/old-pricing.js. With collectCoverageFrom: ['src/**/*.js'] and nothing excluded, the report has 14 of 17 lines.

Exclude files and folders

// jest.config.js
module.exports = {
  collectCoverageFrom: [
    'src/**/*.js',
    '!src/generated/**',
    '!src/legacy/**',
  ],
};

The report then listed two files, 12 of 12 lines.

Without collectCoverageFrom, Jest reports only the files your tests require. Our legacy file was missing from that report, and it appeared, at 0 of 2 lines, once collectCoverageFrom named src.

Two pattern lists. src/**/*.js followed by !src/generated/** and !src/legacy/** leaves 2 files, 12 of 12 lines. The negations first, then src/**/*.js, adds them all back: 4 files, 14 of 17 lines. coveragePathIgnorePatterns takes regular expressions, and src/generated/** stops Jest.
collectCoverageFrom is read in order: the last pattern that matches a file decides.

coveragePathIgnorePatterns gave the same two files:

module.exports = {
  collectCoverageFrom: ['src/**/*.js'],
  coveragePathIgnorePatterns: ['/node_modules/', '/src/generated/', '/src/legacy/'],
};

Each entry is a regular expression matched against the full path. Setting the option replaces its default, ['/node_modules/'], so keep that entry.

Ignore lines with comments

function format(cents) {
  /* istanbul ignore if */
  if (cents < 0) {
    return `-${format(-cents)}`;
  }
  return `${Math.floor(cents / 100)}.${String(cents % 100).padStart(2, '0')}`;
}

/* istanbul ignore next */ above a function leaves the whole function out. These hints work on Jest’s default provider, which instruments with Babel.

With coverageProvider: 'v8' they did nothing: the same file had 5 uncovered lines and 1 uncovered branch. The V8 provider reads c8 and v8 hints instead, and its ignore next covers one line, not the next function. Over a three-line function, /* c8 ignore next */ left the body uncovered; use /* c8 ignore next 3 */ or start / stop.

Why your Jest coverage exclude isn’t working

What we triedWhat happened
! patterns before 'src/**/*.js'Nothing excluded: the later pattern adds the files back
coveragePathIgnorePatterns: ['src/generated/**']Jest stopped: “Invalid regular expression … Nothing to repeat”
testPathIgnorePatterns insteadNo change: it picks which test files run
istanbul ignore with coverageProvider: 'v8'Ignored
c8 ignore next above a function, V8 providerOnly the first line ignored
A grid of seven setups against four files. Without collectCoverageFrom, legacy is missing. src/**/*.js keeps all four. Adding a negation keeps pricing and receipt. Negations first keep all four. A regular-expression ignore keeps pricing and receipt. A glob in coveragePathIgnorePatterns stops Jest. testPathIgnorePatterns keeps all four.
What each setup left in the report, measured with Jest 30.5.2.

Patterns in collectCoverageFrom are globs from rootDir; ./src/** worked the same as src/**.

What Supercov adds

npx supercov -- npx jest
npx supercov runs latest gaps

Supercov runs the same command and records which test covered each line, with branches and MC/DC. It decides what counts from your source directories, not from collectCoverageFrom. See Jest coverage: V8 vs Babel for how Jest’s two providers count, Jest coverage threshold to fail below a floor, and Vitest coverage exclude for Vitest.

More coverage guides