Package Exports
- coverage-node
- coverage-node/package
- coverage-node/package.json
Readme
coverage-node
A simple CLI to run Node.js and report code coverage.
- ✨ Zero config.
- 🏁 ~323 SLOC, written from scratch to use code coverage features built into Node.js v10+.
- 📦 ~337 kB install size, compared to 2.2 MB for
c8
v7.3.5 or 8.84 MB fornyc
v15.1.0. - 🖱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
^10.17.0 || ^12.0.0 || >= 13.7.0
, but for Node.js versions < v13.3 that produce unreliable coverage data thecoverage-node
CLI skips code coverage and logs a warning.
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 reportCoverage
- constant coverageSupported
- constant coverageSupportedMinNodeVersion
- type CoverageAnalysis
- type SemanticVersion
- 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
Ways to import
.
import { analyseCoverage } from 'coverage-node';import analyseCoverage from 'coverage-node/public/analyseCoverage.js';
Ways to require
.
const { analyseCoverage } = require('coverage-node');const analyseCoverage = require('coverage-node/public/analyseCoverage');
function reportCoverage
Reports a code coverage analysis to the console.
Parameter | Type | Description |
---|---|---|
coverageAnalysis |
CoverageAnalysis | Coverage analysis from analyseCoverage . |
Examples
Ways to import
.
import { reportCoverage } from 'coverage-node';import reportCoverage from 'coverage-node/public/reportCoverage.js';
Ways to require
.
const { reportCoverage } = require('coverage-node');const reportCoverage = require('coverage-node/public/reportCoverage');
constant coverageSupported
Is the process Node.js version greater at least the minimum required to support code coverage.
Type: boolean
Examples
Ways to import
.
import { coverageSupported } from 'coverage-node';import coverageSupported from 'coverage-node/public/coverageSupported.js';
Ways to require
.
const { coverageSupported } = require('coverage-node');const coverageSupported = require('coverage-node/public/coverageSupported');
constant coverageSupportedMinNodeVersion
Minimum Node.js version supported for code coverage. Although Node.js v10+ supports coverage, only v13.3+ produces coverage data reliable enough to use.
Type: SemanticVersion
Examples
Ways to import
.
import { coverageSupportedMinNodeVersion } from 'coverage-node';import coverageSupportedMinNodeVersion from 'coverage-node/public/coverageSupportedMinNodeVersion.js';
Ways to require
.
const { coverageSupportedMinNodeVersion } = require('coverage-node');const coverageSupportedMinNodeVersion = require('coverage-node/public/coverageSupportedMinNodeVersion');
type CoverageAnalysis
Node.js generated V8 JavaScript code coverage data analysis; useful for reporting.
Type: object
Property | Type | Description |
---|---|---|
filesCount |
number | Number of files analyzed. |
covered |
Array |
Covered file absolute paths. |
ignored |
Array<SourceCodeRanges> | Ignored source code ranges. |
uncovered |
Array<SourceCodeRanges> | Uncovered source code ranges. |
type SemanticVersion
A semantic version.
Type: object
Property | Type | Description |
---|---|---|
major |
number | Major version. |
minor |
number | Minor version. |
patch |
number | Patch version. |
prerelease |
string? | Prerelease version. |
build |
string? | Build metadata. |
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. |