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

Package detail

c8js

origin-1444ISC0.8.0TypeScript support: included

A modern, asynchronous Node.js API for c8

api, c8, coverage, nodejs, v8

readme

c8js

A modern, asynchronous Node.js API for c8.

npm version

c8 leverages Node.js built-in V8 JavaScript code coverage to produce Istanbul-compatible reports.

c8js offers a usable API to access all c8 commands programmatically in Node.js with JavaScript functions.

c8js is not a wrapper around the command line interface: it calls c8 library functions in the current thread instead of spawning an instrumenter process.

Comparison without/with c8js

Installation

Use npm install:

npm i -D c8js

Usage

c8js exports the functions c8js (the default export), report, and checkCoverage, in place of the commands c8, c8 report, and c8 check-coverage respectively. Additionally, c8js exports the function exec that just runs a command and saves V8 coverage data.

While c8 commands accept inline arguments, c8js functions accept in-code options. All c8js options can be used in-code with all functions, but some options are only relevant to some of the functions. In-code options override options defined in .c8rc or another configuration file.

An important difference is that all c8js functions throw exceptions in situations where c8 commands would terminate the process with a nonzero exit code.

For detailed usage information, see the API documentation.

Examples

Run Mocha

This will run mocha --check-leaks --timeout=10000 test/*.spec.js and produce html and text-summary coverage reports.

async function runMocha()
{
    const { default: c8js } = await import('c8js');
    await c8js
    (
        'node_modules/mocha/bin/mocha.js',
        ['--check-leaks', '--timeout=10000', 'test/*.spec.js'],
        { reporter: ['html', 'text-summary'] },
    );
}

Run with npx

Similar to the previous example, but uses npx to locate and run Mocha. npx will download packages from the npm registry if necessary.

async function runMocha()
{
    const { default: c8js, commands } = await import('c8js');
    await c8js
    (
        commands.npx,
        ['mocha', '--check-leaks', '--timeout=10000', 'test/*.spec.js'],
        { reporter: ['html', 'text-summary'] },
    );
}

Run npm test

This will run npm test with the default coverage options.

async function runTest()
{
    const { default: c8js, commands } = await import('c8js');
    await c8js(commands.npm, ['test']);
}

Run node --test (Node.js ≥ 18.1)

Node.js 18.1 introduces the command line flag --test that launches the built-in Test runner. The following function shows how to run node --test with default coverage options.

async function runTest()
{
    const { default: c8js } = await import('c8js');
    await c8js(process.execPath, ['--test']);
}

NOTE: Some versions of Node.js contain a bug that causes node --test to crash when run from c8. The affected Node.js versions are 18.11.0, 18.12.0, 18.12.1, 19.0.0 and 19.0.1.

Sharing Options

To use c8js in different build scripts or packages with similar settings, it is useful to export a function that calls c8js with the set of shared options. This function can be imported by each build script and invoked with specific parameters.

// create-coverage.js
export async function createCoverage(options)
{
    const { default: c8js, commands } = await import('c8js');
    await c8js
    (
        commands.npm,
        ['test'],
        {
            all:            true,
            src:            'lib',
            throwExecError: 'late',
            watermarks:
            {
                branches:   [90, 100],
                functions:  [90, 100],
                lines:      [90, 100],
                statements: [90, 100],
            },
            ...options,
        }
    );
}
// build.js
import { createCoverage } from './create-coverage.js';

await createCoverage({ src: ['lib', 'src/app'], timeout: 300 * 1000 });

Compatibility

c8js requires Node.js 16 or later. The minimum supported c8 version is 8.0.0.

changelog

v0.8.0 (2024-07-25)

  • Dropped support for Node.js < 16.
  • Added support for c8 versions up until 10.1.2.
  • Backslashes ("\") in Unix file names are no longer replaced with forward slashes ("/") in the Fail objects of a LowCoverageError.

v0.7.0 (2024-04-24)

  • On Windows, commands.npm and commands.npx will now resolve to the path of a .js file, not a .cmd file like in previous versions of c8js. This is because new versions of Node.js prevent the execution of .cmd files on Windows when no shell process is spawned (see the announcement).
  • Fixed documentation pages.

v0.6.2 (2024-02-06)

  • Added support for c8 9.1.0.

v0.6.1 (2024-01-05)

  • Added support for c8 9.0.0.
  • Removed unused transitive dependency foreground-child.
  • Updated documentation.

v0.6.0 (2023-07-28)

  • Added CommonJS export.
  • When the option perFile is set to true, a thrown LowCoverageError will contain a list of all fails, not just of the fails in one file.
  • Added type definitions and API documentation for the types LowCoverageError and Fail.
  • Upgraded c8 to version 8.

v0.5.0 (2023-06-09)

  • New option mergeAsync, added in c8 7.14.0.
  • Updated npm badge.

v0.4.0 (2023-02-17)

  • New option reporterOptions, added in c8 7.13.0.
  • Improved documentation.

v0.3.0 (2022-09-26)

  • Fix: c8js works again in Node.js < 14.13.
  • The new option throwExecError has replaced failFast.
  • All c8js functions now throw an exception when passed unknown options.
  • Added lazy validation for the options killSignal, maxBuffer and timeout.
  • Signals sent to the process running c8js are no longer passed down to a subprocess.
  • When a process running c8js exits while a subprocess is still running, the subprocess will be sent the signal specified by the option killSignal. Previously, it would always be sent 'SIGHUP' or 'SIGTERM'.
  • Improved performance when the option maxBuffer is set to Infinity.
  • Extended documentation.
  • Linking to the new homepage.
  • Removed a few unused dependencies.

v0.2.1 (2022-06-18)

  • Improved stack traces for errors occurring during subprocess execution.

v0.2.0 (2022-06-06)

  • Transferred project to Origin₁.
  • Publishing API documentation on GitHub Pages.
  • Improved readme file.
  • Updated TypeScript type declaration for commands.

v0.1.0 (2022-05-25)

  • New option useC8Config.
  • Fix: c8 options defined in package.json files are no longer cached across unrelated invocations.
  • Added TypeScript type declaration for option c8Config.
  • Added license file.
  • npm badge in readme file.

v0.0.0 (2022-05-21)

First release.