Package Exports
- coverage-node
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (coverage-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
coverage-node
A simple CLI to run Node.js and report code coverage.
- ✨ Zero config.
- 🏁 ~240 SLOC, written from scratch to use code coverage features built into Node.js v10+.
- 📦 ~300 kB install size, compared to 6.61 MB for
c8
or 13 MB fornyc
. - 🖱Displays ignored or uncovered source code ranges as paths, clickable in IDEs such as VS Code.
Setup
To install from npm run:
npm install coverage-node --save-dev
In a package.json
script, replace the node
command with coverage-node
:
{
"scripts": {
- "test": "node test.js"
+ "test": "coverage-node test.js"
}
}
Support
- Linux, macOS.
- Node.js v10+, but Node.js v13.3+ generates more reliable coverage data.
Ignored files
Code coverage analysis ignores:
node_modules
directory files, e.g.node_modules/foo/index.js
.test
directory files, e.g.test/index.js
.- Files with
.test
prefixed before the extension, e.g.foo.test.js
. - Files named
test
(regardless of extension), e.g.test.js
.
Ignored lines
In source code, a comment (case insensitive) can be used to ignore code coverage ranges that start on the the next line:
// coverage ignore next line
if (false) console.log('Never runs.')
CLI
The coverage-node
command substitutes the normal node
command; any node
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
.
npx
can be used to quickly check code coverage for a script:
npx coverage-node test.js
API
Table of contents
- function analyseCoverage
- function nodeWithCoverage
- function reportCoverage
- type CoverageAnalysis
- type SourceCodeLocation
- type SourceCodeRange
- type SourceCodeRanges
function analyseCoverage
Analyzes Node.js generated V8 JavaScript code coverage data in a directory; useful for reporting.
Parameter | Type | Description |
---|---|---|
coverageDirPath |
string | Code coverage data directory path. |
Returns: Promise<CoverageAnalysis> — Resolves the coverage analysis.
Examples
How to import.
const { analyseCoverage } = require('coverage-node')
function nodeWithCoverage
Runs Node.js with code coverage enabled via the NODE_V8_COVERAGE
environment variable.
Parameter | Type | Description |
---|---|---|
coverageDirPath |
string | Directory path to store code coverage data. |
args |
Array<string> | Node.js CLI arguments. |
command |
string? = node |
Node.js CLI command to run with the arguments. |
Returns: Promise<number> — Resolves the Node.js exit code.
Examples
How to import.
const { nodeWithCoverage } = require('coverage-node')
function reportCoverage
Reports a code coverage analysis to the console.
Parameter | Type | Description |
---|---|---|
coverageAnalysis |
CoverageAnalysis | Coverage analysis from analyseCoverage . |
Examples
How to import.
const { reportCoverage } = require('coverage-node')
type CoverageAnalysis
Node.js generated V8 JavaScript code coverage data analysis; useful for reporting.
Type: object
Property | Type | Description |
---|---|---|
covered |
Array<string> | Covered file absolute paths. |
ignored |
Array<SourceCodeRanges> | Ignored source code ranges. |
uncovered |
Array<SourceCodeRanges> | Uncovered source code ranges. |
type SourceCodeLocation
Source code location.
Type: object
Property | Type | Description |
---|---|---|
offset |
number | Character offset. |
line |
number | Line number. |
column |
column | Column number. |
type SourceCodeRange
Source code range details.
Type: object
Property | Type | Description |
---|---|---|
ignore |
boolean? | Should it be ignored. |
start |
SourceCodeLocation | Start location. |
end |
SourceCodeLocation | End location. |
type SourceCodeRanges
A source code file with ranges of interest.
Type: object
Property | Type | Description |
---|---|---|
path |
string | File absolute path. |
ranges |
Array<SourceCodeRange> | Ranges of interest. |