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 |
|
7 | Code-coverage using [Node.js' built in functionality](https://nodejs.org/dist/latest-v10.x/docs/api/cli.html#cli_node_v8_coverage_dir)
|
8 | that's compatible with [Istanbul's reporters](https://istanbul.js.org/docs/advanced/alternative-reporters/).
|
9 |
|
10 | Like [nyc](https://github.com/istanbuljs/nyc), c8 just magically works:
|
11 |
|
12 | ```bash
|
13 | npm i c8 -g
|
14 | c8 node foo.js
|
15 | ```
|
16 |
|
17 | The above example will output coverage metrics for `foo.js`.
|
18 |
|
19 | ## CLI Options / Configuration
|
20 |
|
21 | c8 can be configured via command-line flags, a `c8` section in `package.json`, or a JSON configuration file on disk.
|
22 |
|
23 | A 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 |
|
26 | When using `package.json` configuration or a dedicated configuration file, omit the `--` prefix from the long-form of the desired command-line option.
|
27 |
|
28 | Here 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 |
|
46 | By default v8 will only give us coverage for files that were loaded by the engine. If there are source files in your
|
47 | project that are flexed in production but not in your tests, your coverage numbers will not reflect this. For example,
|
48 | if your project's `main.js` loads `a.js` and `b.js` but your unit tests only load `a.js` your total coverage
|
49 | could show as `100%` for `a.js` when in fact both `main.js` and `b.js` are uncovered.
|
50 |
|
51 | By supplying `--all` to c8, all files in directories specified with `--src` (defaults to `cwd`) that pass the `--include`
|
52 | and `--exclude` flag checks, will be loaded into the report. If any of those files remain uncovered they will be factored
|
53 | into the report with a default of 0% coverage.
|
54 |
|
55 | ## c8 report
|
56 |
|
57 | run `c8 report` to regenerate reports after `c8` has already been run.
|
58 |
|
59 | ## Checking coverage
|
60 |
|
61 | c8 can fail tests if coverage falls below a threshold.
|
62 | After running your tests with c8, simply run:
|
63 |
|
64 | ```shell
|
65 | c8 check-coverage --lines 95 --functions 95 --branches 95
|
66 | ```
|
67 |
|
68 | c8 also accepts a `--check-coverage` shorthand, which can be used to
|
69 | both run tests and check that coverage falls within the threshold provided:
|
70 |
|
71 | ```shell
|
72 | c8 --check-coverage --lines 100 npm test
|
73 | ```
|
74 |
|
75 | The above check fails if coverage falls below 100%.
|
76 |
|
77 | To check thresholds on a per-file basis run:
|
78 |
|
79 | ```shell
|
80 | c8 check-coverage --lines 95 --per-file
|
81 | ```
|
82 |
|
83 | ## Ignoring Uncovered Lines, Functions, and Blocks
|
84 |
|
85 | Sometimes you might find yourself wanting to ignore uncovered portions of your
|
86 | codebase. For example, perhaps you run your tests on Linux, but
|
87 | there's some logic that only executes on Windows.
|
88 |
|
89 | To ignore lines, blocks, and functions, use the special comment:
|
90 |
|
91 | `/* c8 ignore next */`.
|
92 |
|
93 | ### Ignoring the next line
|
94 |
|
95 | ```js
|
96 | const myVariable = 99
|
97 | /* c8 ignore next */
|
98 | if (process.platform === 'win32') console.info('hello world')
|
99 | ```
|
100 |
|
101 | ### Ignoring the next N lines
|
102 |
|
103 | ```js
|
104 | const myVariable = 99
|
105 | /* c8 ignore next 3 */
|
106 | if (process.platform === 'win32') {
|
107 | console.info('hello world')
|
108 | }
|
109 | ```
|
110 |
|
111 | ### Ignoring all lines until told
|
112 |
|
113 | ```js
|
114 | /* c8 ignore start */
|
115 | function dontMindMe() {
|
116 | // ...
|
117 | }
|
118 | /* c8 ignore stop */
|
119 | ```
|
120 |
|
121 | ### Ignoring a block on the current line
|
122 |
|
123 | ```js
|
124 | const myVariable = 99
|
125 | const os = process.platform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'
|
126 | ```
|
127 |
|
128 | ## Supported Node.js Versions
|
129 |
|
130 | c8 uses
|
131 | [native V8 coverage](https://github.com/nodejs/node/pull/22527),
|
132 | make sure you're running Node.js `>= 10.12.0`.
|
133 |
|
134 | ## Contributing to `c8`
|
135 |
|
136 | See the [contributing guide here](./CONTRIBUTING.md).
|