--experimental-test-coverage flag.
If using the run() API, the coverage option must be set to true. For more information on the run() API, see the node:test documentation.
What is code coverage?
Code coverage is a metric for test runners that gauges how much of a program’s source code is executed during testing. It reveals which portions of the codebase are tested and which are not, helping to pinpoint gaps in the test suite. This ensures more comprehensive testing of the software and minimizes the risk of undetected bugs. Typically expressed as a percentage, higher code coverage percentages indicate more thorough test coverage. For a more detailed explanation, refer to the “Code coverage” Wikipedia article.Basic coverage reporting
Let’s walk through a simple example to demonstrate how code coverage works in Node.js.This example, and all other ones in this page, are written using CommonJS. If you are unfamiliar with this concept, please read the CommonJS Modules documentation.
add, isEven, and multiply. In the test file, we are testing the add() and isEven() functions. Notice that the multiply() function is not covered by any tests.
To collect code coverage while running your tests:
- CLI
- run() API
- Line coverage: The percentage of lines executed during the tests.
- Branch coverage: The percentage of code branches (like if-else statements) tested.
- Function coverage: The percentage of functions that have been invoked during testing.
main.jsshows 76.92% line coverage and 66.67% function coverage because themultiply()function was not tested. The uncovered lines (9-11) correspond to this function.main.test.jsshows 100% coverage across all metrics, indicating that the tests themselves were fully executed.
Including and excluding
When working on applications, you might encounter situations where certain files or lines of code need to be excluded. Node.js provides mechanisms to handle this, including the use of comments to ignore specific code sections and the CLI to exclude entire patterns.Using comments
There are multiple ways to ignore sections of code using comments:- ignore next N
- ignore next (line by line)
- disable / enable
Using the CLI
Node.js offers two CLI arguments for managing the inclusion or exclusion of specific files in a coverage report.--test-coverage-include(coverageIncludeGlobsin therun()API) restricts the coverage to files that match the provided glob pattern. By default, files in the/node_modules/directory are excluded, but this flag allows you to explicitly include them.--test-coverage-exclude(coverageExcludeGlobsin therun()API) omits files that match the given glob pattern from the coverage report.
src/age.js has less-than-optimal coverage, but with --test-coverage-exclude you can exclude it from the report entirely:
- CLI
- run() API
src/ directory. The --test-coverage-include flag can be used in this case:
- CLI
- run() API
Thresholds
By default, when all tests pass, Node.js exits with code0, which indicates a successful execution. However, the coverage report can be configured to exit with code 1 when coverage is failing.
Node.js currently supports thresholds for all three coverage types:
--test-coverage-lines(lineCoveragein therun()API) for line coverage.--test-coverage-branches(branchCoveragein therun()API) for branch coverage.--test-coverage-functions(functionCoveragein therun()API) for function coverage.
- CLI
- run() API