UNPKG

7.29 kBMarkdownView Raw
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
5A simple CLI to run [Node.js](https://nodejs.org) and report code coverage.
6
7- ✨ Zero config.
8- 🏁 \~308 [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- 📦 [< 400 kB install size](https://packagephobia.com/result?p=coverage-node), compared to [2.2 MB for `c8` v7.7.1](https://packagephobia.com/result?p=c8@7.7.1) or [8.84 MB for `nyc` v15.1.0](https://packagephobia.com/result?p=nyc@15.1.0).
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
14To install with [npm](https://npmjs.com/get-npm), run:
15
16```sh
17npm install coverage-node --save-dev
18```
19
20In a [`package.json` script](https://docs.npmjs.com/files/package.json#scripts), replace the `node` command with [`coverage-node`](#command-coverage-node):
21
22```diff
23 {
24 "scripts": {
25- "test": "node test.mjs"
26+ "test": "coverage-node test.mjs"
27 }
28 }
29```
30
31## Support
32
33- Linux, macOS.
34- Node.js `^12.20 || >= 14.13`, but for Node.js versions < v13.3 that produce [unreliable coverage data](https://github.com/nodejs/node/issues/25937#issuecomment-563115421) the command [`coverage-node`](#command-coverage-node) skips code coverage and logs a warning.
35
36## Ignored files
37
38Code coverage analysis ignores:
39
40- `node_modules` directory files, e.g. `node_modules/foo/index.mjs`.
41- `test` directory files, e.g. `test/index.mjs`.
42- Files with `.test` prefixed before the extension, e.g. `foo.test.mjs`.
43- Files named `test` (regardless of extension), e.g. `test.mjs`.
44
45## Ignored lines
46
47In 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
51if (false) console.log('Never runs.');
52```
53
54## CLI
55
56### Command `coverage-node`
57
58Substitutes the normal `node` command; any [`node` CLI options](https://nodejs.org/api/cli.html#cli_options) can be used to run a test script. 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#### Examples
61
62_Using [`npx`](https://docs.npmjs.com/cli/v7/commands/npx)._
63
64> [`npx`](https://npm.im/npx) can be used to quickly check code coverage for a script:
65>
66> ```sh
67> npx coverage-node test.mjs
68> ```
69
70_Using a [`package.json` script](https://docs.npmjs.com/cli/v7/using-npm/scripts)._
71
72> ```json
73> {
74> "scripts": {
75> "test": "coverage-node test.mjs"
76> }
77> }
78> ```
79
80## API
81
82### Table of contents
83
84- [function analyseCoverage](#function-analysecoverage)
85- [function reportCoverage](#function-reportcoverage)
86- [constant coverageSupported](#constant-coveragesupported)
87- [constant coverageSupportedMinNodeVersion](#constant-coveragesupportedminnodeversion)
88- [type CoverageAnalysis](#type-coverageanalysis)
89- [type SemanticVersion](#type-semanticversion)
90- [type SourceCodeLocation](#type-sourcecodelocation)
91- [type SourceCodeRange](#type-sourcecoderange)
92- [type SourceCodeRanges](#type-sourcecoderanges)
93
94### function analyseCoverage
95
96Analyzes [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.
97
98| Parameter | Type | Description |
99| :---------------- | :----- | :--------------------------------- |
100| `coverageDirPath` | string | Code coverage data directory path. |
101
102**Returns:** Promise<[CoverageAnalysis](#type-coverageanalysis)> — Resolves the coverage analysis.
103
104#### Examples
105
106_Ways to `import`._
107
108> ```js
109> import { analyseCoverage } from 'coverage-node';
110> ```
111>
112> ```js
113> import analyseCoverage from 'coverage-node/public/analyseCoverage.mjs';
114> ```
115
116---
117
118### function reportCoverage
119
120Reports a code coverage analysis to the console.
121
122| Parameter | Type | Description |
123| :-- | :-- | :-- |
124| `coverageAnalysis` | [CoverageAnalysis](#type-coverageanalysis) | Coverage analysis from [`analyseCoverage`](#function-analysecoverage). |
125
126#### Examples
127
128_Ways to `import`._
129
130> ```js
131> import { reportCoverage } from 'coverage-node';
132> ```
133>
134> ```js
135> import reportCoverage from 'coverage-node/public/reportCoverage.mjs';
136> ```
137
138---
139
140### constant coverageSupported
141
142Is the process Node.js version greater at least [the minimum required to support code coverage](#constant-coveragesupportedminnodeversion).
143
144**Type:** boolean
145
146#### Examples
147
148_Ways to `import`._
149
150> ```js
151> import { coverageSupported } from 'coverage-node';
152> ```
153>
154> ```js
155> import coverageSupported from 'coverage-node/public/coverageSupported.mjs';
156> ```
157
158---
159
160### constant coverageSupportedMinNodeVersion
161
162Minimum Node.js version supported for code coverage. Although Node.js v10+ supports coverage, only v13.3+ produces coverage data reliable enough to use.
163
164**Type:** [SemanticVersion](#type-semanticversion)
165
166#### Examples
167
168_Ways to `import`._
169
170> ```js
171> import { coverageSupportedMinNodeVersion } from 'coverage-node';
172> ```
173>
174> ```js
175> import coverageSupportedMinNodeVersion from 'coverage-node/public/coverageSupportedMinNodeVersion.mjs';
176> ```
177
178---
179
180### type CoverageAnalysis
181
182[Node.js generated V8 JavaScript code coverage data](https://nodejs.org/api/cli.html#cli_node_v8_coverage_dir) analysis; useful for reporting.
183
184**Type:** object
185
186| Property | Type | Description |
187| :-- | :-- | :-- |
188| `filesCount` | number | Number of files analyzed. |
189| `covered` | Array<string> | Covered file absolute paths. |
190| `ignored` | Array<[SourceCodeRanges](#type-sourcecoderanges)> | Ignored source code ranges. |
191| `uncovered` | Array<[SourceCodeRanges](#type-sourcecoderanges)> | Uncovered source code ranges. |
192
193---
194
195### type SemanticVersion
196
197A semantic version.
198
199**Type:** object
200
201| Property | Type | Description |
202| :----------- | :------ | :------------------ |
203| `major` | number | Major version. |
204| `minor` | number | Minor version. |
205| `patch` | number | Patch version. |
206| `prerelease` | string? | Prerelease version. |
207| `build` | string? | Build metadata. |
208
209---
210
211### type SourceCodeLocation
212
213Source code location.
214
215**Type:** object
216
217| Property | Type | Description |
218| :------- | :----- | :---------------- |
219| `offset` | number | Character offset. |
220| `line` | number | Line number. |
221| `column` | column | Column number. |
222
223---
224
225### type SourceCodeRange
226
227Source code range details.
228
229**Type:** object
230
231| Property | Type | Description |
232| :-- | :-- | :-- |
233| `ignore` | boolean? | Should it be ignored. |
234| `start` | [SourceCodeLocation](#type-sourcecodelocation) | Start location. |
235| `end` | [SourceCodeLocation](#type-sourcecodelocation) | End location. |
236
237---
238
239### type SourceCodeRanges
240
241A source code file with ranges of interest.
242
243**Type:** object
244
245| Property | Type | Description |
246| :-- | :-- | :-- |
247| `path` | string | File absolute path. |
248| `ranges` | Array<[SourceCodeRange](#type-sourcecoderange)> | Ranges of interest. |