cn

coverage-node

A simple CLI to run Node.js and report code coverage.

Showing:

Popularity

Downloads/wk

137

GitHub Stars

20

Maintenance

Last Commit

5mos ago

Contributors

0

Package

Dependencies

3

Size (min+gzip)

2.3KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

coverage-node

npm version CI status

A simple CLI to run Node.js and report code coverage.

Setup

To install with npm, run:

npm install coverage-node --save-dev

In a package.json script, replace the node command with coverage-node:

 {
   "scripts": {
-    "test": "node test.mjs"
+    "test": "coverage-node test.mjs"
   }
 }

Support

Ignored files

Code coverage analysis ignores:

  • node_modules directory files, e.g. node_modules/foo/index.mjs.
  • test directory files, e.g. test/index.mjs.
  • Files with .test prefixed before the extension, e.g. foo.test.mjs.
  • Files named test (regardless of extension), e.g. test.mjs.

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

Command coverage-node

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.

Examples

Using npx.

npx can be used to quickly check code coverage for a script:

npx coverage-node test.mjs

Using a package.json script.

{
  "scripts": {
    "test": "coverage-node test.mjs"
  }
}

API

Table of contents

function analyseCoverage

Analyzes Node.js generated V8 JavaScript code coverage data in a directory; useful for reporting.

ParameterTypeDescription
coverageDirPathstringCode 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.mjs';

function reportCoverage

Reports a code coverage analysis to the console.

ParameterTypeDescription
coverageAnalysisCoverageAnalysisCoverage analysis from analyseCoverage.

Examples

Ways to import.

import { reportCoverage } from 'coverage-node';
import reportCoverage from 'coverage-node/public/reportCoverage.mjs';

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.mjs';

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.mjs';

type CoverageAnalysis

Node.js generated V8 JavaScript code coverage data analysis; useful for reporting.

Type: object

PropertyTypeDescription
filesCountnumberNumber of files analyzed.
coveredArrayCovered file absolute paths.
ignoredArray<SourceCodeRanges>Ignored source code ranges.
uncoveredArray<SourceCodeRanges>Uncovered source code ranges.

type SemanticVersion

A semantic version.

Type: object

PropertyTypeDescription
majornumberMajor version.
minornumberMinor version.
patchnumberPatch version.
prereleasestring?Prerelease version.
buildstring?Build metadata.

type SourceCodeLocation

Source code location.

Type: object

PropertyTypeDescription
offsetnumberCharacter offset.
linenumberLine number.
columncolumnColumn number.

type SourceCodeRange

Source code range details.

Type: object

PropertyTypeDescription
ignoreboolean?Should it be ignored.
startSourceCodeLocationStart location.
endSourceCodeLocationEnd location.

type SourceCodeRanges

A source code file with ranges of interest.

Type: object

PropertyTypeDescription
pathstringFile absolute path.
rangesArray<SourceCodeRange>Ranges of interest.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial