1 | # c8 - native V8 code-coverage
|
2 |
|
3 | [![Build Status](https://travis-ci.org/bcoe/c8.svg?branch=master)](https://travis-ci.org/bcoe/c8)
|
4 | [![Coverage Status](https://coveralls.io/repos/github/bcoe/c8/badge.svg?branch=master)](https://coveralls.io/github/bcoe/c8?branch=master)
|
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 | ## c8 report
|
20 |
|
21 | run `c8 report` to regenerate reports after `c8` has already been run.
|
22 |
|
23 | ## Checking coverage
|
24 |
|
25 | c8 can fail tests if coverage falls below a threshold.
|
26 | After running your tests with c8, simply run:
|
27 |
|
28 | ```shell
|
29 | c8 check-coverage --lines 95 --functions 95 --branches 95
|
30 | ```
|
31 |
|
32 | c8 also accepts a `--check-coverage` shorthand, which can be used to
|
33 | both run tests and check that coverage falls within the threshold provided:
|
34 |
|
35 | ```shell
|
36 | c8 --check-coverage --lines 100 npm test
|
37 | ```
|
38 |
|
39 | The above check fails if coverage falls below 100%.
|
40 |
|
41 | To check thresholds on a per-file basis run:
|
42 |
|
43 | ```shell
|
44 | c8 check-coverage --lines 95 --per-file
|
45 | ```
|
46 |
|
47 | ## Ignoring Uncovered Lines, Functions, and Blocks
|
48 |
|
49 | Sometimes you might find yourself wanting to ignore uncovered portions of your
|
50 | codebase. For example, perhaps you run your tests on Linux, but
|
51 | there's some logic that only executes on Windows.
|
52 |
|
53 | To ignore lines, blocks, and functions, use the special comment:
|
54 |
|
55 | `/* c8 ignore next */`.
|
56 |
|
57 | ### Ignoring the next element
|
58 |
|
59 | ```js
|
60 | const myVariable = 99
|
61 | /* c8 ignore next */
|
62 | if (process.platform === 'win32') console.info('hello world')
|
63 | ```
|
64 |
|
65 | ### Ignoring the next N elements
|
66 |
|
67 | ```js
|
68 | const myVariable = 99
|
69 | /* c8 ignore next 3 */
|
70 | if (process.platform === 'win32') {
|
71 | console.info('hello world')
|
72 | }
|
73 | ```
|
74 |
|
75 | ### Ignoring a block on the current line
|
76 |
|
77 | ```js
|
78 | const myVariable = 99
|
79 | const os = process.platform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'
|
80 | ```
|
81 |
|
82 | ## Supported Node.js Versions
|
83 |
|
84 | c8 uses
|
85 | [bleeding edge Node.js features](https://github.com/nodejs/node/pull/22527),
|
86 | make sure you're running Node.js `>= 10.12.0`.
|
87 |
|
88 | ## Contributing to `c8`
|
89 |
|
90 | See the [contributing guide here](./CONTRIBUTING.md).
|