← All tools

coverage

Popularity 65 Updated Development & Build

A command-line tool to collect and report code coverage of a JavaScript program

npmauto-collected

Installation

npm
npm install -g coverage

Install with npm.

coverage

A command-line tool to collect and report code coverage of a JavaScript program

$ coverage test.js

✔ write a file
✔ create directories as needed
✔ validate arguments

3 of 3 passed.

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |    95.15 |    79.52 |      100 |    95.09 |                   |

lib.js | 100 | 94.44 | 100 | 100 | 107 | ----------|----------|----------|----------|----------|-------------------|


This is an opinionated wrapper of [c8](https://github.com/bcoe/c8), different in the following points:

* Runs both [`html` and `text` reporter](https://github.com/istanbuljs/nyc#running-reports) by default
* Automatically enables [ECMAScript module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) support for `.mjs` files
* The first argument can be a JavaScript file path instead of a command
* Built-in [Codecov](https://codecov.io) support

## Installation

[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).

npm install coverage


Once this package is installed to the project directory, users can execute `coverage` command inside [npm scripts](https://docs.npmjs.com/misc/scripts#description).

## CLI

### `coverage [options] <file|command> [args]`

<img alt="An example of the HTML report" src="screenshot.png" width="330px" align="right">

Execute the command, print code coverage to the stdout and write [HTML reports](https://istanbul.js.org/docs/advanced/alternative-reporters/#html) under the `./coverage` directory.

`<file|command>` can be either a JavaScript file path or a command. If a path is provided, the file is run with the `node` command.
$ coverage /path/to/entry-point.js # is the same as ↓
$ coverage node /path/to/entry-point.js

If the provided JavaScript path ends with .mjs,

  • ECMAScript module mode is automatically enabled.
  • --es-module-specifier-resolution is automatically set to node.

#### Reporters

Users can override the default format of reports with --reporter option.

$ coverage example.js
------------|----------|----------|----------|----------|-------------------|
File        |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------|----------|----------|----------|----------|-------------------|
All files   |      100 |      100 |      100 |      100 |                   |
 example.js |      100 |      100 |      100 |      100 |                   |
------------|----------|----------|----------|----------|-------------------|

$ coverage --reporter=text-summary example.js

=============================== Coverage summary ===============================
Statements   : 100% ( 1/1 )

Functions : 100% ( 0/0 ) Lines : 100% ( 1/1 ) ================================================================================

$ coverage --reporter=none example.js # No reports


#### Codecov integration

When the execution exits with code `0` on a CI service or [GitHub Actions](https://github.com/features/actions), it automatically uploads the generated coverage to [Codecov](https://docs.codecov.io/docs). Whether `CODECOV_TOKEN` environment variable is required or not varies between [services](https://github.com/codecov/codecov-bash#ci-providers).

$ coverage test.js

[... test results and coverage ...]