c8 - native V8 code-coverage
Like nyc, c8 just magically works:
npm i c8 -gc8 node foo.js
The above example will output coverage metrics for
Checking for "full" source coverage using
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
b.js but your unit tests only load
a.js your total coverage
could show as
a.js when in fact both
b.js are uncovered.
--all to c8, all files in
cwd that pass the
--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.
c8 report to regenerate reports after
c8 has already been run.
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
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 element
const myVariable = 99/* c8 ignore next */if processplatform === 'win32' console
Ignoring the next N elements
const myVariable = 99/* c8 ignore next 3 */if processplatform === 'win32'console
Ignoring a block on the current line
const myVariable = 99const os = processplatform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'
Supported Node.js Versions
bleeding edge Node.js features,
make sure you're running Node.js
See the contributing guide here.