istanbul-js
Menu
Advanced reference

nyc file scope: all, include, exclude and source maps

Check which files count before interpreting a coverage percentage.

Source files pass an include filter while tests are separated by an exclude filter.
Define the measured source set before interpreting the percentage; keep untouched source visible.

Is an absent file untouched, excluded or outside the project root? Did a custom exclusion replace defaults? nyc normally reports eligible files visited during execution. all adds untouched eligible files, subject to selection rules.

Follow file selection in order

The root and supported extensions define the initial boundary. Include patterns narrow the set. Exclusions remove matches, and negated exclusions can restore paths removed at that stage. Negation cannot add a file that never matched the include set.

.nycrc.json

{
  "all": true,
  "include": ["src/**/*.js", "src/**/*.cjs"],
  "reporter": ["text", "html"]
}

This keeps nyc's default exclusions. Add an unused source module and check that it appears. That verifies the denominator, rather than only the files tests happened to import.

Treat an exclusion array as a replacement

The nyc README states that a custom exclude replaces defaults. Inspect schema or preset exclusions before overriding them, especially when tests, declarations and generated files must stay outside the source set.

This alternative CommonJS configuration deliberately extends defaults. It assumes @istanbuljs/schema is available as a development dependency:

nyc.config.cjs (alternative to .nycrc.json)

const defaults = require('@istanbuljs/schema/default-exclude');
module.exports = {
  all: true,
  include: ['src/**/*.js', 'src/**/*.cjs'],
  exclude: [...defaults, 'src/generated/**'],
  reporter: ['text', 'html']
};

Use one intended configuration file. The generated-directory boundary is an example to justify in your project, not permission to exclude hand-written code with missed tests.

Keep roots and globs reproducible

nyc resolves source selection relative to the project root, with --cwd available to override it. Compare local and CI working directories. Quote shell globs so the shell does not expand them before nyc receives them.

nyc --all --include='src/**/*.js' mocha

For pre-instrumented builds, inspect names after remapping too. The README documents exclude-after-remap: false for that workflow. Apply it when remapping exclusions are the reason intended source disappears.

Review omissions by cause

Absent fileCheck
Never importedall and eligible source scope.
Outside package rootcwd and actual source location.
Transformed originalMaps and remapped exclusions.
Inside node_modulesThe separate node_modules exclusion policy.

Once scope is stable, define thresholds. Record why each intentional omission belongs outside measurement so future changes remain reviewable.

Primary sources

Check official documentation against the tools installed in your project. These explanations and examples are independent.