1 | # coverage-node
|
2 |
|
3 | [![npm version](https://badgen.net/npm/v/coverage-node)](https://npm.im/coverage-node) [![CI status](https://github.com/jaydenseric/coverage-node/workflows/CI/badge.svg)](https://github.com/jaydenseric/coverage-node/actions)
|
4 |
|
5 | A simple CLI to run [Node.js](https://nodejs.org) and report code coverage.
|
6 |
|
7 | - ✨ Zero config.
|
8 | - 🏁 ~240 [SLOC](https://en.wikipedia.org/wiki/Source_lines_of_code), written from scratch to use [code coverage features](https://nodejs.org/api/cli.html#cli_node_v8_coverage_dir) built into Node.js v10+.
|
9 | - 📦 [~300 kB install size](https://packagephobia.now.sh/result?p=coverage-node), compared to [6.61 MB for `c8`](https://packagephobia.now.sh/result?p=c8@6.0.1) or [13 MB for `nyc`](https://packagephobia.now.sh/result?p=nyc@14.1.1).
|
10 | - 🖱Displays ignored or uncovered source code ranges as paths, clickable in IDEs such as [VS Code](https://code.visualstudio.com).
|
11 |
|
12 | ## Setup
|
13 |
|
14 | To install from [npm](https://npmjs.com) run:
|
15 |
|
16 | ```sh
|
17 | npm install coverage-node --save-dev
|
18 | ```
|
19 |
|
20 | In a [`package.json` script](https://docs.npmjs.com/files/package.json#scripts), replace the `node` command with `coverage-node`:
|
21 |
|
22 | ```diff
|
23 | {
|
24 | "scripts": {
|
25 | - "test": "node test.js"
|
26 | + "test": "coverage-node test.js"
|
27 | }
|
28 | }
|
29 | ```
|
30 |
|
31 | ## Support
|
32 |
|
33 | - Linux, macOS.
|
34 | - Node.js v10+, but [Node.js v13.3+ generates more reliable coverage data](https://github.com/nodejs/node/issues/25937#issuecomment-563115421).
|
35 |
|
36 | ## Ignored files
|
37 |
|
38 | Code coverage analysis ignores:
|
39 |
|
40 | - `node_modules` directory files, e.g. `node_modules/foo/index.js`.
|
41 | - `test` directory files, e.g. `test/index.js`.
|
42 | - Files with `.test` prefixed before the extension, e.g. `foo.test.js`.
|
43 | - Files named `test` (regardless of extension), e.g. `test.js`.
|
44 |
|
45 | ## Ignored lines
|
46 |
|
47 | In source code, a comment (case insensitive) can be used to ignore code coverage ranges that start on the the next line:
|
48 |
|
49 | ```js
|
50 | // coverage ignore next line
|
51 | if (false) console.log('Never runs.')
|
52 | ```
|
53 |
|
54 | ## CLI
|
55 |
|
56 | The `coverage-node` command substitutes the normal `node` command; any [`node` CLI options](https://nodejs.org/api/cli.html) can be used to run a test script.
|
57 |
|
58 | If the script doesn’t error a code coverage analysis is reported to the console, and if coverage is incomplete the exit code is `1`.
|
59 |
|
60 | [`npx`](https://npm.im/npx) can be used to quickly check code coverage for a script:
|
61 |
|
62 | ```shell
|
63 | npx coverage-node test.js
|
64 | ```
|
65 |
|
66 | ## API
|
67 |
|
68 | ### Table of contents
|
69 |
|
70 | - [function analyseCoverage](#function-analysecoverage)
|
71 | - [function nodeWithCoverage](#function-nodewithcoverage)
|
72 | - [function reportCoverage](#function-reportcoverage)
|
73 | - [type CoverageAnalysis](#type-coverageanalysis)
|
74 | - [type SourceCodeLocation](#type-sourcecodelocation)
|
75 | - [type SourceCodeRange](#type-sourcecoderange)
|
76 | - [type SourceCodeRanges](#type-sourcecoderanges)
|
77 |
|
78 | ### function analyseCoverage
|
79 |
|
80 | Analyzes [Node.js generated V8 JavaScript code coverage data](https://nodejs.org/api/cli.html#cli_node_v8_coverage_dir) in a directory; useful for reporting.
|
81 |
|
82 | | Parameter | Type | Description |
|
83 | | :---------------- | :----- | :--------------------------------- |
|
84 | | `coverageDirPath` | string | Code coverage data directory path. |
|
85 |
|
86 | **Returns:** Promise<[CoverageAnalysis](#type-coverageanalysis)> — Resolves the coverage analysis.
|
87 |
|
88 | #### Examples
|
89 |
|
90 | _How to import._
|
91 |
|
92 | > ```js
|
93 | > const { analyseCoverage } = require('coverage-node')
|
94 | > ```
|
95 |
|
96 | ---
|
97 |
|
98 | ### function nodeWithCoverage
|
99 |
|
100 | Runs Node.js with code coverage enabled via the [`NODE_V8_COVERAGE`](https://nodejs.org/api/cli.html#cli_node_v8_coverage_dir) environment variable.
|
101 |
|
102 | | Parameter | Type | Description |
|
103 | | :-- | :-- | :-- |
|
104 | | `coverageDirPath` | string | Directory path to store code coverage data. |
|
105 | | `args` | Array<string> | Node.js CLI arguments. |
|
106 | | `command` | string? = `node` | Node.js CLI command to run with the arguments. |
|
107 |
|
108 | **Returns:** Promise<number> — Resolves the [Node.js exit code](https://nodejs.org/api/process.html#process_exit_codes).
|
109 |
|
110 | #### Examples
|
111 |
|
112 | _How to import._
|
113 |
|
114 | > ```js
|
115 | > const { nodeWithCoverage } = require('coverage-node')
|
116 | > ```
|
117 |
|
118 | ---
|
119 |
|
120 | ### function reportCoverage
|
121 |
|
122 | Reports a code coverage analysis to the console.
|
123 |
|
124 | | Parameter | Type | Description |
|
125 | | :-- | :-- | :-- |
|
126 | | `coverageAnalysis` | [CoverageAnalysis](#type-coverageanalysis) | Coverage analysis from [`analyseCoverage`](#function-analysecoverage). |
|
127 |
|
128 | #### Examples
|
129 |
|
130 | _How to import._
|
131 |
|
132 | > ```js
|
133 | > const { reportCoverage } = require('coverage-node')
|
134 | > ```
|
135 |
|
136 | ---
|
137 |
|
138 | ### type CoverageAnalysis
|
139 |
|
140 | [Node.js generated V8 JavaScript code coverage data](https://nodejs.org/api/cli.html#cli_node_v8_coverage_dir) analysis; useful for reporting.
|
141 |
|
142 | **Type:** object
|
143 |
|
144 | | Property | Type | Description |
|
145 | | :-- | :-- | :-- |
|
146 | | `filesCount` | number | Number of files analyzed. |
|
147 | | `covered` | Array<string> | Covered file absolute paths. |
|
148 | | `ignored` | Array<[SourceCodeRanges](#type-sourcecoderanges)> | Ignored source code ranges. |
|
149 | | `uncovered` | Array<[SourceCodeRanges](#type-sourcecoderanges)> | Uncovered source code ranges. |
|
150 |
|
151 | ---
|
152 |
|
153 | ### type SourceCodeLocation
|
154 |
|
155 | Source code location.
|
156 |
|
157 | **Type:** object
|
158 |
|
159 | | Property | Type | Description |
|
160 | | :------- | :----- | :---------------- |
|
161 | | `offset` | number | Character offset. |
|
162 | | `line` | number | Line number. |
|
163 | | `column` | column | Column number. |
|
164 |
|
165 | ---
|
166 |
|
167 | ### type SourceCodeRange
|
168 |
|
169 | Source code range details.
|
170 |
|
171 | **Type:** object
|
172 |
|
173 | | Property | Type | Description |
|
174 | | :-- | :-- | :-- |
|
175 | | `ignore` | boolean? | Should it be ignored. |
|
176 | | `start` | [SourceCodeLocation](#type-sourcecodelocation) | Start location. |
|
177 | | `end` | [SourceCodeLocation](#type-sourcecodelocation) | End location. |
|
178 |
|
179 | ---
|
180 |
|
181 | ### type SourceCodeRanges
|
182 |
|
183 | A source code file with ranges of interest.
|
184 |
|
185 | **Type:** object
|
186 |
|
187 | | Property | Type | Description |
|
188 | | :-- | :-- | :-- |
|
189 | | `path` | string | File absolute path. |
|
190 | | `ranges` | Array<[SourceCodeRange](#type-sourcecoderange)> | Ranges of interest. |
|