skip to content

Why does a source file that no test imports never show up in a Jest 30 coverage report, and how does collectCoverageFrom change that?

level: middleimportance: must knowfreq 40%

answer

  1. only what the tests loaded
  2. glob list relative to rootDir
  3. untested files reported at zero
  4. later pattern wins
  5. regex ignore list vs glob list

basics

~20 s

Without collectCoverageFrom, Jest reports only files the tests loaded, so an untested module is invisible and the totals look better than they are. Listing source globs in collectCoverageFrom adds every matching file, at zero when nothing ran it.

solid answer

~40 s

By default Jest instruments a file only when a test loads it, so a module with no tests never enters the coverage map. `collectCoverageFrom` takes glob patterns relative to `rootDir`; after the tests finish, Jest adds every matching file no test loaded, with all its counts at zero. It only scopes coverage: it needs `--coverage` or `collectCoverage: true`. Patterns apply in order, so a negation such as `'!**/*.d.ts'` must come after the positive glob it trims. `coveragePathIgnorePatterns` is a separate filter of regex strings tested against the full path, defaulting to `["/node_modules/"]`. Check the glob actually matches: if it matches nothing, the summary shows `Unknown% ( 0/0 )` and a global threshold has nothing to fail on.

code

javascript · 10 lines
javascript
const {defineConfig} = require('jest');

module.exports = defineConfig({
  collectCoverageFrom: [
    'src/**/*.{js,ts}',
    '!**/__generated__/**',
    '!**/*.d.ts',
  ],
  coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/src/legacy/'],
});

go deeper

for a junior

Recall that untested modules are missing from a default coverage run, and that collectCoverageFrom is the option that adds them back at zero.

for a middle

Explain the glob order rule, the separate regex ignore list with its node_modules default, and that the option needs coverage switched on.

for a senior

Show you verify the glob matched real files, since an empty match prints Unknown% and lets a global floor pass silently.

for a principal

Treat the collectCoverageFrom list as the agreed definition of shipped code, reviewed like any config, so every team's numbers cover the same scope.

## The default: only what ran is reported Jest 30 decides per file whether to collect coverage. When `collectCoverageFrom` is empty, a file is counted only if a test actually loaded it during the run. That is cheap and usually what you want while editing, but it has a blind spot: a module that **no test imports** never enters the coverage map at all. It is not reported at 0%; it simply is not there. The `All files` row is then computed over the files that had at least one test, and it looks better than the code base really is. The reserved example: a service reports 92% line coverage, but an `exporter.js` module beside it has no tests and is absent from the table. Nobody sees the gap until something in that module breaks. ## Adding collectCoverageFrom `collectCoverageFrom` is an array of **glob patterns**, relative to `rootDir`, naming the files you want reported whether or not a test touched them. After the tests finish, Jest looks for matching files that have no coverage data yet and adds them with every statement, branch, function and line counted as uncovered. In an interactive terminal you briefly see `Running coverage on untested files...` while that happens. How to write it well: 1. Start with the positive glob for your source tree, for example `'src/**/*.{js,ts}'`. 2. Add the negations after it: type declarations, generated code, stories, fixtures. 3. Remember the option does nothing alone. The documentation states it requires `collectCoverage` to be `true` or Jest to be invoked with `--coverage`. 4. Run once and confirm the table now lists the files you expected, including untested ones at 0. When a glob is set, it also narrows the report. A file the tests loaded but that matches no pattern is no longer counted, which is how a test helper outside `src/` drops out of the numbers. ## Glob order and negation Patterns are applied **in the order they are listed**. The documentation's own example: `["!**/__tests__/**", "**/*.js"]` does not exclude `__tests__`, because the later positive pattern re-includes what the negation removed. Put the negation after the pattern it trims: - wrong: `['!**/__generated__/**', 'src/**/*.ts']` still reports generated files; - right: `['src/**/*.ts', '!**/__generated__/**', '!**/*.d.ts']`. ## Two filters, two syntaxes | Option | Syntax | Matched against | Default | |---|---|---|---| | `collectCoverageFrom` | glob patterns, applied in order | path relative to `rootDir` | `undefined` (only loaded files) | | `coveragePathIgnorePatterns` | regular expression strings | the full file path | `["/node_modules/"]` | `coveragePathIgnorePatterns` removes files from coverage even when they are loaded or matched. Because it matches the full path, the documentation recommends the `<rootDir>` token, as in `"<rootDir>/build/"`, so a pattern cannot accidentally match a parent directory on another machine. Setting the option replaces the default, so keep `"/node_modules/"` in your list if you still want dependencies excluded. ## Files that are never counted Whatever the globs say, Jest does not instrument test files (unless `forceCoverageMatch` names them), files in a `__mocks__` directory, the modules listed in `setupFiles`, `setupFilesAfterEnv`, `globalSetup` and `globalTeardown`, and `.json` files. A broad glob such as `'**/*.js'` therefore does not drag your tests into the numbers. ## When the glob matches nothing A typo in the glob, or a glob written for a different `rootDir`, is the dangerous case. The table shows an `All files` row of zeros with no file rows, the `text-summary` reporter prints `Unknown% ( 0/0 )` for every metric, and the documentation's help note points to globs that do not match any file. In a Jest 30.5.2 run, a `coverageThreshold.global` floor did not fail in that situation, because there was no coverage data to compare against, so the run exited successfully. After changing `collectCoverageFrom` or moving the config, look at the file list in the table once rather than trusting the exit status. ## Summary of the behaviour - No glob: only files the tests loaded, so untested modules are invisible. - With a glob: every matching file, untested ones at zero, and loaded files outside the glob dropped. - Order matters: negations after positives. - The ignore list is regex over full paths; the glob list is relative to `rootDir`.

  • Does collectCoverageFrom also drop files the tests loaded but that match no pattern?
    Yes. Once `collectCoverageFrom` is non-empty, Jest instruments a file only if it matches the globs, so a helper or fixture outside `src/` that the tests import no longer appears. That is usually the point: the report describes the code you ship, not whatever the tests happened to pull in.
  • Why prefer the <rootDir> token in coveragePathIgnorePatterns?
    The patterns are regular expressions tested against the full file path. A bare `"/build/"` would also match a checkout that lives under a directory named build, ignoring everything. `<rootDir>/build/` anchors the pattern to the project, so it means the same on every machine.
  • How do you confirm a collectCoverageFrom change actually took effect?
    Run with `--coverage` and read the file list in the text table: untested files should now appear at 0. If the table has only an `All files` row of zeros, or `text-summary` prints `Unknown% ( 0/0 )`, the globs matched nothing and need fixing.

A default coverage run is like a register that lists only the pupils who turned up: absent ones are not marked absent, they are simply missing. collectCoverageFrom hands Jest the full class list, so every name appears and the absent ones show zero.

  • A module with no tests shows up in the default coverage table at 0%.
  • A negation listed first in collectCoverageFrom always wins over later patterns.
  • collectCoverageFrom turns coverage on by itself.
  • coveragePathIgnorePatterns takes the same globs as collectCoverageFrom.
  • A collectCoverageFrom typo makes the global coverage threshold fail loudly.