Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

c8

bcoe8.8mISC10.1.3TypeScript support: included

output coverage reports using Node.js' built in coverage

coverage, v8, test, istanbul, profiler, inspector, node

readme

c8 - native V8 code-coverage

ci nycrc config on GitHub Conventional Commits

Code-coverage using Node.js' built in functionality that's compatible with Istanbul's reporters.

Like nyc, c8 just magically works:

npm i c8 -g
c8 node foo.js

The above example will output coverage metrics for foo.js.

CLI Options / Configuration

c8 can be configured via command-line flags, a c8 section in package.json, or a JSON configuration file on disk.

A configuration file can be specified by passing its path on the command line with --config or -c. If no config option is provided, c8 searches for files named .c8rc, .c8rc.json, .nycrc, or .nycrc.json, starting from cwd and walking up the filesystem tree.

When using package.json configuration or a dedicated configuration file, omit the -- prefix from the long-form of the desired command-line option.

Here is a list of common options. Run c8 --help for the full list and documentation.

Option Description Type Default
-c, --config path to JSON configuration file string See above
-r, --reporter coverage reporter(s) to use Array<string> ['text']
-o, --reports-dir, --report-dir directory where coverage reports will be output to string ./coverage
--all see section below for more info boolean false
--src see section below for more info Array<string> [process.cwd()]
-n, --include see section below for more info Array<string> [] (include all files)
-x, --exclude see section below for more info Array<string> list
--exclude-after-remap see section below for more info boolean false
-e, --extension only files matching these extensions will show coverage string | Array<string> list
--skip-full do not show files with 100% statement, branch, and function coverage boolean false
--check-coverage check whether coverage is within thresholds provided boolean false
--per-file check thresholds per file boolean false
--temp-directory directory V8 coverage data is written to and read from string process.env.NODE_V8_COVERAGE
--clean should temp files be deleted before script execution boolean true
--experimental-monocart see section below for more info boolean false

Checking for "full" source coverage using --all

By default v8 will only give us coverage for files that were loaded by the engine. If there are source files in your project that are flexed in production but not in your tests, your coverage numbers will not reflect this. For example, if your project's main.js loads a.js and b.js but your unit tests only load a.js your total coverage could show as 100% for a.js when in fact both main.js and b.js are uncovered.

By supplying --all to c8, all files in directories specified with --src (defaults to cwd) that pass the --include and --exclude flag checks, will be loaded into the report. If any of those files remain uncovered they will be factored into the report with a default of 0% coverage.

SourceMap Support

c8 can handle source-maps, for remapping coverage from generated code to original source files (useful for TypeScript, JSX, etc).

Source map files versus inline source maps

Just-in-time instrumented codebases will often insert source maps inline with the .js code they generate at runtime (e.g, @babel/register can be configured to insert a source map footer).

Pre-instrumented codebases, e.g., running tsc to generate .js in a build folder, may generate either inline source maps, or a separate .map file stored on disk.

c8 can handle loading both types of source maps.

Exclude after remap

Depending on the size and configuration of your project, it may be preferable to apply exclusion logic either before or after source-maps are used to remap compiled to original source files.

--exclude-after-remap is used to control this behaviour.

c8 report

run c8 report to regenerate reports after c8 has already been run.

Checking coverage

c8 can fail tests if coverage falls below a threshold. After running your tests with c8, simply run:

c8 check-coverage --lines 95 --functions 95 --branches 95

c8 also accepts a --check-coverage shorthand, which can be used to both run tests and check that coverage falls within the threshold provided:

c8 --check-coverage --lines 100 npm test

The above check fails if coverage falls below 100%.

To check thresholds on a per-file basis run:

c8 check-coverage --lines 95 --per-file

If you want to check for 100% coverage across all dimensions, use --100:

c8 --100 npm test

Is equivalent to

c8 --check-coverage --lines 100 --functions 100 --branches 100 --statements 100  npm test

The --100 flag can be set for the check-coverage as well:

c8 check-coverage --100

Using Monocart coverage reports (experimental)

Monocart is an alternate library for outputting v8 code coverage data as Istanbul reports.

Monocart also provides reporters based directly on v8's byte-offset-based output. Such as, console-details and v8. This removes a complex transformation step and may be less bug prone for some environments.

Example usage:

c8 --experimental-monocart --reporter=v8 --reporter=console-details node foo.js

NOTE: Monocart requires additional monocart-coverage-reports to be installed:

npm i monocart-coverage-reports@2 --save-dev

Ignoring Uncovered Lines, Functions, and Blocks

Sometimes you might find yourself wanting to ignore uncovered portions of your codebase. For example, perhaps you run your tests on Linux, but there's some logic that only executes on Windows.

To ignore lines, blocks, and functions, use the special comment:

/* c8 ignore next */.

Ignoring the next line

const myVariable = 99
/* c8 ignore next */
if (process.platform === 'win32') console.info('hello world')

Ignoring the next N lines

const myVariable = 99
/* c8 ignore next 3 */
if (process.platform === 'win32') {
  console.info('hello world')
}

Ignoring all lines until told

/* c8 ignore start */
function dontMindMe() {
  // ...
}
/* c8 ignore stop */

Ignoring a block on the current line

const myVariable = 99
const os = process.platform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'

Supported Node.js Versions

c8 uses native V8 coverage, make sure you're running Node.js >= 12.

Contributing to c8

See the contributing guide here.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

10.1.3 (2024-12-10)

Bug Fixes

  • deps: update bcoe/v8-coverage addressing v8 issue (#552) (b32a563)

10.1.2 (2024-06-13)

Bug Fixes

  • deps: make monocart-coverage-reports an optional with meta defined (3b91fda)

10.1.1 (2024-06-11)

Bug Fixes

  • stop installing monocart-coverage-reports (#535) (13979a7)

10.1.0 (2024-06-11)

Features

10.0.0 (2024-06-10)

⚠ BREAKING CHANGES

  • deps: Node 18 is now the minimum supported Node.js version

Bug Fixes

  • deps: update test-exclude with new glob / minimatch (#531) (e33cf30)

9.1.0 (2024-01-11)

Features

  • support passing reporter options from config (#459) (88db5db)

Bug Fixes

  • refactor: remove stale check for createDynamicModule (5e18365)

9.0.0 (2024-01-03)

⚠ BREAKING CHANGES

  • build: minimum Node.js version is now 14.14.0

Features

  • build: minimum Node.js version is now 14.14.0 (2cdc86b)
  • deps: update foreground-child to promise API (#512) (b46b640)
  • deps: use Node.js built in rm (2cdc86b)

8.0.1 (2023-07-25)

Bug Fixes

8.0.0 (2023-06-05)

⚠ BREAKING CHANGES

  • dropped Node 10 support (#475)

Miscellaneous Chores

7.14.0 (2023-05-26)

Features

  • added a new CLI arg --merge-async to asynchronously and incrementally merge process coverage files to avoid OOM due to heap exhaustion (#469) (45f2f84)

7.13.0 (2023-02-14)

Features

  • add skipFull and excludeNodeModules to type definitions (#417) (b93b9c0)
  • support passing reporter options (#423) (bc347a9)
  • types: add typings for reporterOptions (#446) (3646e6e)

7.12.0 (2022-07-19)

Features

  • use process.stdout.columns for reporter maxCols (#409) (7731574)

7.11.3 (2022-05-16)

Bug Fixes

  • --all now respects --extension flag. (#357) (a5deb27)

7.11.2 (2022-04-20)

Bug Fixes

  • perf: cache this.exclude.shouldInstrument for improved performance (#388) (8b36f23)

7.11.1 (2022-04-20)

Bug Fixes

  • deps: update deps to latest (#384) (78eac8c), closes #375
  • deps: v8-to-istanbul with fixes for Node 10/18 (d5f642a)
  • fix package.json test:snap script to use cross-env (#366) (5d2981c)

7.11.0 (2021-12-30)

Features

Bug Fixes

  • config: support configuration inheritance (#343) (e81ed5d)

7.10.0 (2021-10-06)

Features

7.9.0 (2021-09-10)

Features

7.8.0 (2021-07-10)

Features

  • add --config option and documentation on options and configs (#308) (99436ef)

7.7.3 (2021-06-03)

Bug Fixes

  • deps: v8-to-istanbul with fix for Windows paths (#311) (ef1b875)

7.7.2 (2021-05-02)

Bug Fixes

  • address bugs with source remapping on Windows (#301) (c817902)

7.7.1 (2021-04-07)

Bug Fixes

  • types: add excludeAfterRemap and allowExternal (#297) (e32a53f)

7.7.0 (2021-03-30)

Features

7.6.0 (2021-02-17)

Features

7.5.0 (2021-02-01)

Features

  • all: handle base64 inline source maps (#283) (3f12dd4)

7.4.0 (2020-12-31)

Features

7.3.5 (2020-10-25)

Bug Fixes

  • v8-to-istanbul: fixes shebang handling/ignore behavior (#267) (21cd41f)

7.3.4 (2020-10-15)

Bug Fixes

7.3.3 (2020-10-09)

Bug Fixes

  • v8-to-istanbul: revert off by one that broke TypeScript (#262) (81ab5b7)

7.3.2 (2020-10-08)

Bug Fixes

  • v8-to-istanbul: pull in fix for missing branches (#258) (eaffa78)

7.3.1 (2020-09-10)

Bug Fixes

  • deps: update dependency yargs to v16 (#251) (0436816)
  • deps: update dependency yargs-parser to v20 (#252) (ae845f0)
  • add missing space in text (#245) (efe6d04)
  • deps: update dependency find-up to v5 (#242) (8a0cfd7)
  • deps: update dependency yargs-parser to v19 (#241) (baa01df)

7.3.0 (2020-08-03)

Features

7.2.1 (2020-07-11)

Bug Fixes

  • ignore missing source maps in raw coverage output (#233) (eed98af)

7.2.0 (2020-05-25)

Features

  • support for instrumenting files outside of current working directory (7e53a0e)

7.1.2 (2020-05-04)

Bug Fixes

  • deps: update dependency yargs-parser to v18 (#202) (983de44)

7.1.1 (2020-04-29)

Bug Fixes

  • deps: update dependency yargs-parser to v17 (#201) (d730c63)
  • escaping issue with cobertura reporter (#203) (e93747b)

7.1.0 (2020-02-09)

Features

Bug Fixes

  • deps: update dependency furi to v2 (#193) (6b9af6e)
  • deps: v8-to-istanbul with patch for crasher (#200) (d4b7d80)

7.0.1 (2020-01-13)

Bug Fixes

  • all flag not propagated to check-coverage command (#188) (86eaf72)

7.0.0 (2019-12-22)

⚠ BREAKING CHANGES

  • new test-exclude with modified exclude rules (#179)
  • istanbul-reports: lcov reports now use relative paths (#168)

Features

  • adds --all functionality (#158) (2eb631e)
  • istanbul-reports: lcov reports now use relative paths (#168) (35d9338)
  • new test-exclude with modified exclude rules (#179) (af7d94d)

Bug Fixes

  • deps: update dependency v8-to-istanbul to v4 (#167) (97b9769)
  • deps: update dependency yargs to v15 (#164) (e41a483)
  • deps: update dependency yargs-parser to v16 (#157) (15746e5)

6.0.1 (2019-10-26)

Bug Fixes

  • regex flags in dependency were breaking Node 8 (a9d9645)

6.0.0 (2019-10-24)

⚠ BREAKING CHANGES

  • Node.js' source-map and lineLength cache is now used to remap coverage output (this allows tools like ts-node to be supported, which transpile at runtime).

Features

  • use Node.js' source-map cache, to support tools like ts-node (#152) (53bba15)

Bug Fixes

  • deps: update dependency yargs-parser to v15 (#153) (80153de)

5.0.4 (2019-09-06)

Bug Fixes

  • deps: merging failed when the same script occurred multiple times in the same report (#147) (1ebcaf9)
  • don't load JSON that does not look like coverage (#146) (a6481f1)
  • deps: update dependency yargs-parser to v14 (#144) (9b3d089)

5.0.3 (2019-09-06)

Bug Fixes

  • deps: update dependency rimraf to v3 (#132) (7601748)
  • deps: update dependency yargs to v14 (#134) (e49737f)
  • deps: update deps to address warning in cross-spawn (#141) (4b66221)

5.0.2 (2019-06-24)

Bug Fixes

  • HTML report now has correct source positions for Node >10.16.0 (#125) (c49fa7f)
  • deps: update dependency find-up to v4 (#119) (c568d96)
  • deps: update dependency yargs-parser to v13 (#124) (1eb3394)
  • do not override NODE_V8_COVERAGE if set (#70) (8bb67b0)

5.0.1 (2019-05-20)

Bug Fixes

  • temporary files should be in tmp folder (#106) (64dd2e6)

5.0.0 (2019-05-20)

⚠ BREAKING CHANGES

  • temp directory now defaults to setting for report directory

Features

  • default temp directory to report directory (#102) (8602f4a)
  • load .nycrc/.nycrc.json to simplify migration (#100) (bd7484f)

4.1.5 (2019-05-11)

Bug Fixes

  • exit with code 1 when report output fails (#92) (a27b694)
  • remove the unmaintained mkdirp dependency (#91) (a465b65)

4.1.4 (2019-05-03)

Bug Fixes

  • we were not exiting with 1 if mkdir failed (#89) (fb02ed6)

4.1.3 (2019-05-03)

Bug Fixes

  • switch to mkdirp for Node 8 (206b83f)

4.1.2 (2019-05-02)

Bug Fixes

  • make tmp directory regardless of clean (44d2185)

4.1.1 (2019-05-02)

4.1.0 (2019-05-02)

Bug Fixes

  • exclude coverage of the CJS-ESM bridge from results (#83) (da2c945)
  • upgrade to @bcoe/v8-coverage with breaking regex dropped (6c28e7f)

Features

  • add --report-dir alias (for consistency with nyc) (0dd1b04)
  • add support for ignoring lines, functions, and blocks (#87) (c66950e)

4.0.0 (2019-05-02)

Features

  • add support for 1:1 source-maps (#85) (6ca4345)
  • foreground-child's done() method was not being called (#82) (fde596e)

BREAKING CHANGES

  • c8 will now load source-maps if possible and remap coverage accordingly

3.5.0 (2019-04-12)

Features

  • allow --reports-dir to be configured (#65) (5ab31f5)

3.4.0 (2019-01-24)

Features

  • support --check-coverage for reports (#60) (b542930)

3.3.0 (2019-01-23)

Bug Fixes

  • file URL to system path conversion (#46) (e7f8cf2)
  • float patch for branch/function coverage merge bug (#56) (1de0cca)
  • snapshot (7fd9e13)

Features

  • add thresholds for enforcing coverage percentage (#59) (70e8943)
  • allow script wrapper length to be specified (#51) (a22c4e0)

3.2.1 (2018-10-21)

Bug Fixes

3.2.0 (2018-09-16)

Bug Fixes

Features

3.1.0 (2018-09-11)

Features

  • allow relative paths to be optionally included (3806c79)

3.0.3 (2018-09-10)

3.0.2 (2018-09-10)

3.0.1 (2018-09-10)

3.0.0 (2018-09-10)

Features

  • switch to using Node's built in coverage (#22) (3c1b92b)

BREAKING CHANGES

  • switches to using NODE_V8_COVERAGE rather than inspector directly

2.0.0 (2017-12-17)

Bug Fixes

  • tweak inspector event timing (#6) (01f654e)

Features

  • first pass at functional prototype without subprocess support (#5) (9534f56)
  • implement Istanbul reporting (#8) (8e430bf)
  • switch to stderr and default port (#7) (bb117b7)

BREAKING CHANGES

  • dropped subprocess support for the time being, while we march towards an initial implementation.

1.0.1 (2017-10-26)

Bug Fixes

  • pin to functional version of spawn-wrap (d1ced8c)

1.0.0 (2017-10-26)

Features

  • playing around with initial implementation (18f5471)