UNPKG

5.33 kBMarkdownView Raw
1# c8 - native V8 code-coverage
2
3![ci](https://github.com/bcoe/c8/workflows/ci/badge.svg)
4![nycrc config on GitHub](https://img.shields.io/nycrc/bcoe/c8)
5[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
6
7Code-coverage using [Node.js' built in functionality](https://nodejs.org/dist/latest-v10.x/docs/api/cli.html#cli_node_v8_coverage_dir)
8that's compatible with [Istanbul's reporters](https://istanbul.js.org/docs/advanced/alternative-reporters/).
9
10Like [nyc](https://github.com/istanbuljs/nyc), c8 just magically works:
11
12```bash
13npm i c8 -g
14c8 node foo.js
15```
16
17The above example will output coverage metrics for `foo.js`.
18
19## CLI Options / Configuration
20
21c8 can be configured via command-line flags, a `c8` section in `package.json`, or a JSON configuration file on disk.
22
23A 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
24`cwd` and walking up the filesystem tree.
25
26When using `package.json` configuration or a dedicated configuration file, omit the `--` prefix from the long-form of the desired command-line option.
27
28Here is a list of common options. Run `c8 --help` for the full list and documentation.
29
30| Option | Description | Type | Default |
31| ------ | ----------- | ---- | ------- |
32| `-c`, `--config` | path to JSON configuration file | `string` | See above |
33| `-r`, `--reporter` | coverage reporter(s) to use | `Array<string>` | `['text']` |
34| `-o`, `--reports-dir`, `--report-dir` | directory where coverage reports will be output to | `string` | `./coverage` |
35| `--all` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `boolean` | `false` |
36| `--src` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[process.cwd()]`|
37| `-n`, `--include` | see [section below](#checking-for-full-source-coverage-using---all) for more info | `Array<string>` | `[]` (include all files) |
38| `-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) |
39| `--skip-full` | do not show files with 100% statement, branch, and function coverage | `boolean` | `false` |
40| `--check-coverage` | check whether coverage is within thresholds provided | `boolean` | `false` |
41| `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` |
42| `--clean` | should temp files be deleted before script execution | `boolean` | `true` |
43
44## Checking for "full" source coverage using `--all`
45
46By default v8 will only give us coverage for files that were loaded by the engine. If there are source files in your
47project that are flexed in production but not in your tests, your coverage numbers will not reflect this. For example,
48if your project's `main.js` loads `a.js` and `b.js` but your unit tests only load `a.js` your total coverage
49could show as `100%` for `a.js` when in fact both `main.js` and `b.js` are uncovered.
50
51By supplying `--all` to c8, all files in directories specified with `--src` (defaults to `cwd`) that pass the `--include`
52and `--exclude` flag checks, will be loaded into the report. If any of those files remain uncovered they will be factored
53into the report with a default of 0% coverage.
54
55## c8 report
56
57run `c8 report` to regenerate reports after `c8` has already been run.
58
59## Checking coverage
60
61c8 can fail tests if coverage falls below a threshold.
62After running your tests with c8, simply run:
63
64```shell
65c8 check-coverage --lines 95 --functions 95 --branches 95
66```
67
68c8 also accepts a `--check-coverage` shorthand, which can be used to
69both run tests and check that coverage falls within the threshold provided:
70
71```shell
72c8 --check-coverage --lines 100 npm test
73```
74
75The above check fails if coverage falls below 100%.
76
77To check thresholds on a per-file basis run:
78
79```shell
80c8 check-coverage --lines 95 --per-file
81```
82
83## Ignoring Uncovered Lines, Functions, and Blocks
84
85Sometimes you might find yourself wanting to ignore uncovered portions of your
86codebase. For example, perhaps you run your tests on Linux, but
87there's some logic that only executes on Windows.
88
89To ignore lines, blocks, and functions, use the special comment:
90
91`/* c8 ignore next */`.
92
93### Ignoring the next line
94
95```js
96const myVariable = 99
97/* c8 ignore next */
98if (process.platform === 'win32') console.info('hello world')
99```
100
101### Ignoring the next N lines
102
103```js
104const myVariable = 99
105/* c8 ignore next 3 */
106if (process.platform === 'win32') {
107 console.info('hello world')
108}
109```
110
111### Ignoring all lines until told
112
113```js
114/* c8 ignore start */
115function dontMindMe() {
116 // ...
117}
118/* c8 ignore stop */
119```
120
121### Ignoring a block on the current line
122
123```js
124const myVariable = 99
125const os = process.platform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'
126```
127
128## Supported Node.js Versions
129
130c8 uses
131[native V8 coverage](https://github.com/nodejs/node/pull/22527),
132make sure you're running Node.js `>= 10.12.0`.
133
134## Contributing to `c8`
135
136See the [contributing guide here](./CONTRIBUTING.md).