c8

repository·main·Indexed 24 days ago

https://github.com/bcoe/c8

A native V8 code-coverage tool for Node.js that leverages built-in coverage capabilities and is compatible with Istanbul reporters. It provides a lightweight way to measure test coverage, generate reports via the CLI or Report class, and enforce coverage thresholds for lines, functions, branches, and statements.

Tokens
3.1K
Snippets
5
Records
15
Agent score
83%

What's inside c8

  1. Ensure full source coverage using --all

    main

    By default, V8 only provides coverage for files loaded by the engine during execution. If some files in your project are not loaded during tests, they won't appear in the report.

    Use the --all flag to include all files in the directories specified by --src (defaults to cwd) that pass the --include and --exclude filters. Uncovered files will be included in the report with 0% coverage.

  2. Configure c8 via CLI, package.json, or config files

    main

    c8 can be configured using command-line flags, a c8 section in your package.json, or a JSON configuration file.

    To specify a custom configuration file, use the --config or -c flag. If no config is provided, c8 searches for .c8rc, .c8rc.json, .nycrc, or .nycrc.json files by walking up the filesystem from the current working directory.

    Note: When using package.json or a config file, omit the -- prefix from long-form command-line options.

  3. Check coverage against thresholds

    main

    c8 can fail your test suite if coverage falls below specified thresholds for lines, functions, branches, or statements.

    Option 1: Separate check command Run tests first, then run check-coverage:

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

    Option 2: Combined run and check Use the --check-coverage flag to run tests and check thresholds in one step:

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

    Option 3: Per-file threshold checking To ensure every individual file meets a threshold:

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

    Option 4: The --100 shorthand To require 100% coverage across all dimensions (lines, functions, branches, and statements):

    c8 --100 npm test
    # OR
    c8 check-coverage --100
  4. Ignore uncovered code with c8 comments

    main

    You can use special comments to prevent c8 from reporting uncovered lines, blocks, or functions. This is useful for platform-specific logic (e.g., code that only runs on Windows).

    Ignore the next line

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

    Ignore the next N lines

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

    Ignore a block

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

    Ignore a block on the current line

    const os = process.platform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'
  5. Use Monocart coverage reports (experimental)

    main

    Monocart is an alternative library for outputting V8 coverage data as Istanbul reports. It also provides reporters based directly on V8's byte-offset-based output (like console-details and v8), which can be more robust in some environments.

    Requirement: You must install monocart-coverage-reports@2 as a dev dependency.

    Example usage:

    c8 --experimental-monocart --reporter=v8 --reporter=console-details node foo.js
    npm i monocart-coverage-reports@2 --save-dev
  6. Install and run c8 for code coverage

    main

    c8 provides native V8 code coverage compatible with Istanbul's reporters. You can install it globally and run it directly before your Node.js command to generate coverage metrics for the executed files.

    npm i c8 -g
    c8 node foo.js
  7. Include uncovered files in coverage using the `all` option

    main

    By default, coverage tools only report on files that were actually executed. To include files that were never touched (showing 0% coverage), set all: true in the Report options.

    When all is enabled, you must also provide the src option (a string or array of directory paths) so c8 knows which files to scan for existence. c8 will then create 'empty' coverage reports for those files and merge them into the final result.

  8. Reference: c8 CLI options

    main

    Commonly used command-line options for c8. Run c8 --help for the full list.

    | 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](#checking-for-full-source-coverage-using---all) for more info | `boolean` | `false` |
    | `--src` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[process.cwd()]`|
    | `-n`, `--include` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[]` (include all files) |
    | `-x`, `--exclude` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-exclude.js) |
    | `--exclude-after-remap` | see [section below](#exclude-after-remap) for more info | `boolean` | `false` |
    | `-e`, `--extension` | only files matching these extensions will show coverage | `string \| Array<string>` | [list](https://github.com/istanbuljs/schema/blob/master/default-extension.js) |
    | `--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](#using-monocart-coverage-reports-experimental) for more info | `boolean` | `false` |
  9. Generate coverage reports with the Report class

    main

    The Report class (exported as a function) is used to process V8 coverage data and generate human-readable or machine-readable reports. You can instantiate it by passing an options object and then calling the asynchronous .run() method.

    Key configuration options include:

    • reporter: An array of reporter names (e.g., ['text', 'lcov']).
    • reporterOptions: An object containing options for each reporter, keyed by the reporter name.
    • reportsDirectory: Where to save the generated reports.
    • tempDirectory: Where the raw V8 coverage files are located.
    • exclude: Patterns to exclude from coverage.
    • include: Patterns to include in coverage.
    • all: If true, includes files that have no coverage (requires src to be specified).
    • src: The source directory or directories to check for uncovered files when all is true.
    • mergeAsync: If true, uses asynchronous merging of coverage files, which is more memory-efficient for large multi-process runs.
  10. Reference: `check-coverage` CLI options

    main

    The following options are used by the check-coverage command to define thresholds and report configuration:

    OptionDescription
    --lines <number>Minimum percentage of lines covered
    --functions <number>Minimum percentage of functions covered
    --branches <number>Minimum percentage of branches covered
    --statements <number>Minimum percentage of statements covered
    --100Shorthand to set lines, functions, branches, and statements all to 100%
    --perFileIf provided, checks thresholds for each file individually instead of a global summary
    --include <pattern>Files to include in the report
    --exclude <pattern>Files to exclude from the report
    --extension <string>File extension to look for
    --reporter <string|string[]>Reporter(s) to use
    --reports-dir <path>Directory containing the coverage reports
    --tempDirectory <path>Directory for temporary files
    --watermarks <json>Watermark configuration for the report
    --resolve <string>Resolution strategy
    --omitRelative <boolean>Whether to omit relative paths
    --wrapperLength <number>Wrapper length for coverage
    --allUse all coverage files for the check
  11. Check coverage thresholds with `check-coverage`

    main

    The check-coverage command evaluates whether the coverage data in c8's output directory meets specified minimum thresholds for lines, functions, branches, and statements.

    If any threshold is not met, the command sets the process exit code to 1. It can check coverage globally across all files or individually for each file.

    Usage

    To check if coverage meets a 95% threshold for lines, use:

    c8 check-coverage --lines 95

    To enforce 100% coverage across all metrics, you can use the --100 flag (which sets lines, functions, branches, and statements to 100%):

    c8 check-coverage --100