Code coverage is one of the most useful measurements for understanding the quality of a test suite. It shows which parts of an application are executed during testing and helps developers identify functions, branches, and lines that have not been tested.
However, there is a common problem with traditional coverage reports: they generally report files that were loaded during the test run. If a source file is never imported by any test, it may not appear in the report at all.
This can create a misleading picture of the application's overall test coverage. A project may show a high coverage percentage even though several source files have no tests and are never executed.
Node.js addresses this problem with the --test-coverage-include-all option, introduced in Node.js 26.7.0. It allows the built-in test runner to include source files that were never loaded during testing and report them with zero coverage.
This article explains how the feature works, how to configure it, and how to use it to get a more complete picture of test coverage in Node.js applications.
Why Untested Files Can Be Missing From Coverage Reports
Consider a Node.js project with the following structure:
my-node-app/
│
├── src/
│ ├── user.js
│ ├── order.js
│ ├── payment.js
│ └── notification.js
│
├── test/
│ ├── user.test.js
│ └── order.test.js
│
└── package.json
The application contains four source files, but the test directory contains tests for only two of them.
For example, user.test.js imports user.js, and order.test.js imports order.js. The payment.js and notification.js files are never imported during testing.
When the built-in Node.js test runner collects coverage, it normally reports files that were loaded during the test run. As a result, payment.js and notification.js may be missing from the report.
This creates an incomplete picture of the project.
A developer looking at the report might assume that all source files have been considered, even though some files have never been included in the coverage calculation.
The problem is not that Node.js cannot measure the coverage of these files. The problem is that the coverage collection process needs to discover files that were never executed.
What Is --test-coverage-include-all?
Node.js provides the --test-coverage-include-all command-line option to include source files that were not loaded during the test run.
When this option is enabled, the test runner searches for candidate source files in the current working directory and includes matching files in the coverage report.
Files that were never loaded are reported with zero coverage.
The option was introduced in Node.js 26.7.0 and is currently marked Experimental.
Its main purpose is to make the coverage report reflect the complete set of matching source files instead of only the files reached by the test suite.
For example, suppose your project contains:
Source file | Imported during tests | Coverage report |
|---|
user.js | Yes | Actual coverage |
order.js | Yes | Actual coverage |
payment.js | No | 0% coverage |
notification.js | No | 0% coverage |
This gives developers a more accurate understanding of which parts of the application have test coverage.
The option works with the existing --test-coverage-include and --test-coverage-exclude filters.
Enabling Complete Coverage From the Command Line
Let's start with a simple Node.js project.
Create the following directory structure:
coverage-demo/
│
├── src/
│ ├── user.js
│ ├── order.js
│ ├── payment.js
│ └── notification.js
│
├── test/
│ ├── user.test.js
│ └── order.test.js
│
└── package.json
Step 1: Create the source files
Create user.js:
function getUserName(user) {
return user.name;
}
function isActiveUser(user) {
return user.active === true;
}
module.exports = {
getUserName,
isActiveUser
};
Create order.js:
function calculateTotal(price, quantity) {
return price * quantity;
}
function isValidOrder(order) {
return order.quantity > 0;
}
module.exports = {
calculateTotal,
isValidOrder
};
Create payment.js:
function calculateTax(amount, taxRate) {
return amount * taxRate;
}
function processPayment(amount) {
return {
amount,
status: 'COMPLETED'
};
}
module.exports = {
calculateTax,
processPayment
};
Create notification.js:
function createMessage(username) {
return `Hello, ${username}`;
}
function sendNotification(message) {
return {
message,
sent: true
};
}
module.exports = {
createMessage,
sendNotification
};
At this point, the project contains four source files.
Step 2: Create test files
Create test/user.test.js:
const test = require('node:test');
const assert = require('node:assert');
const {
getUserName,
isActiveUser
} = require('../src/user');
test('returns the user name', () => {
const user = {
name: 'Alex',
active: true
};
assert.strictEqual(
getUserName(user),
'Alex'
);
});
test('checks whether a user is active', () => {
const user = {
name: 'Alex',
active: true
};
assert.strictEqual(
isActiveUser(user),
true
);
});
Create test/order.test.js:
const test = require('node:test');
const assert = require('node:assert');
const {
calculateTotal,
isValidOrder
} = require('../src/order');
test('calculates order total', () => {
assert.strictEqual(
calculateTotal(100, 2),
200
);
});
test('validates order quantity', () => {
assert.strictEqual(
isValidOrder({ quantity: 2 }),
true
);
});
Notice that neither test file imports payment.js or notification.js.
Those two files are currently untested and will not be loaded by these tests.
Step 3: Run coverage without including unloaded files
Run the following command from the project root:
node --test --experimental-test-coverage
The built-in test runner executes the test files and produces a coverage report.
The report contains the files loaded during testing. The payment.js and notification.js files may not appear because nothing imported them.
This is the limitation that the new option addresses.
Step 4: Include source files that were never loaded
Now run:
node --test \
--experimental-test-coverage \
--test-coverage-include="src/**/*.js" \
--test-coverage-include-all
The --test-coverage-include option selects the source files that should be included in the report.
The --test-coverage-include-all option adds matching files that were never loaded during testing.
The coverage report can now include all four source files.
The two files without tests appear with zero coverage:
File | Status | Coverage |
|---|
user.js | Loaded and tested | Actual measured coverage |
order.js | Loaded and tested | Actual measured coverage |
payment.js | Never loaded | 0% |
notification.js | Never loaded | 0% |
The exact percentages for user.js and order.js depend on which lines and branches are executed by their tests.
This makes missing test coverage much easier to identify.
Understanding --test-coverage-include
The --test-coverage-include option controls which files are eligible for the coverage report.
It accepts glob patterns and can be specified multiple times.
For example:
node --test \
--experimental-test-coverage \
--test-coverage-include="src/**/*.js"
This command limits the coverage report to matching JavaScript files under the src directory.
It is important to understand that --test-coverage-include alone does not guarantee that never-loaded files will appear.
The option selects which files are eligible for reporting. The --test-coverage-include-all option expands the report to include matching files that were never loaded.
You can also include multiple directories:
node --test \
--experimental-test-coverage \
--test-coverage-include="src/**/*.js" \
--test-coverage-include="services/**/*.js" \
--test-coverage-include-all
This is useful for projects where application code is separated into multiple directories.
The glob patterns can match relative and absolute file paths.
Excluding Files From the Coverage Report
Not every JavaScript file in a project needs to be included in application coverage.
For example, a project may contain:
The --test-coverage-exclude option allows you to exclude files that should not be part of the report.
Consider this command:
node --test \
--experimental-test-coverage \
--test-coverage-include="src/**/*.js" \
--test-coverage-exclude="src/generated/**/*.js" \
--test-coverage-include-all
This command includes source files under src while excluding files inside src/generated.
When both include and exclude options are supplied, a file must satisfy the inclusion rules and must not match the exclusion rules.
Both options can be specified multiple times.
For example:
node --test \
--experimental-test-coverage \
--test-coverage-include="src/**/*.js" \
--test-coverage-include="services/**/*.js" \
--test-coverage-exclude="src/generated/**/*.js" \
--test-coverage-exclude="services/legacy/**/*.js" \
--test-coverage-include-all
This provides more control over which files are considered part of the application's test coverage.
Using Complete Coverage With the Node.js Test Runner API
The same functionality is available through the programmatic test runner API.
Instead of passing command-line arguments, you can configure coverage directly when calling run() from node:test.
The following example demonstrates how to configure the test runner:
const { run } = require('node:test');
run({
files: [
'test/user.test.js',
'test/order.test.js'
],
coverage: true,
coverageIncludeGlobs: [
'src/**/*.js'
],
coverageIncludeAll: true
});
The important options are:
Option | Purpose |
|---|
files | Specifies the test files to execute |
coverage | Enables code coverage collection |
coverageIncludeGlobs | Selects files to include in the coverage report |
coverageExcludeGlobs | Excludes matching files from the report |
coverageIncludeAll | Includes matching source files that were never loaded |
The coverageIncludeAll option must be used with coverage enabled.
You can also exclude generated files:
const { run } = require('node:test');
run({
files: [
'test/user.test.js',
'test/order.test.js'
],
coverage: true,
coverageIncludeGlobs: [
'src/**/*.js'
],
coverageExcludeGlobs: [
'src/generated/**/*.js'
],
coverageIncludeAll: true
});
This is useful when a project has its own test execution script or needs to integrate the Node.js test runner into a custom development workflow.
Setting Coverage Thresholds
Including untested files makes coverage reporting more complete, but it is also important to decide what level of coverage is acceptable.
Node.js supports thresholds for lines, branches, and functions.
For example:
node --test \
--experimental-test-coverage \
--test-coverage-include="src/**/*.js" \
--test-coverage-include-all \
--test-coverage-lines=80 \
--test-coverage-branches=70 \
--test-coverage-functions=80
This command requires:
At least 80% line coverage.
At least 70% branch coverage.
At least 80% function coverage.
If the coverage report does not meet a configured threshold, the process exits with code 1.
This makes coverage thresholds useful in continuous integration pipelines.
The same configuration can be expressed through the run() API:
const { run } = require('node:test');
run({
files: [
'test/user.test.js',
'test/order.test.js'
],
coverage: true,
coverageIncludeGlobs: [
'src/**/*.js'
],
coverageIncludeAll: true,
lineCoverage: 80,
branchCoverage: 70,
functionCoverage: 80
});
Be careful when introducing thresholds into an existing project. Including previously unreported files can reduce the overall coverage percentage significantly. This does not mean the test suite suddenly became worse. It means the report now includes source files that were previously missing.
A practical approach is to establish a baseline first and then improve coverage gradually.
Integrating Complete Coverage Into package.json
For teams that regularly run tests from npm scripts, the complete coverage command can be stored in package.json.
For example:
{
"scripts": {
"test": "node --test",
"test:coverage": "node --test --experimental-test-coverage --test-coverage-include='src/**/*.js' --test-coverage-include-all"
}
}
Developers can then run:
npm run test:coverage
This makes the coverage configuration consistent across local development and CI environments.
For larger applications, you can also add coverage thresholds to the script.
{
"scripts": {
"test:coverage": "node --test --experimental-test-coverage --test-coverage-include='src/**/*.js' --test-coverage-include-all --test-coverage-lines=80"
}
}
The threshold should reflect the quality requirements of the project rather than being selected only to achieve a high percentage.
Adding Complete Coverage to CI Pipelines
Continuous integration is one of the best places to use complete coverage reporting.
A CI pipeline can execute the Node.js test suite, include all matching source files, and fail the build when coverage falls below the required threshold.
For example, a GitHub Actions workflow can use:
name: Node.js Tests
on:
push:
branches:
- main
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '26'
- name: Install dependencies
run: npm ci
- name: Run tests with coverage
run: |
node --test \
--experimental-test-coverage \
--test-coverage-include="src/**/*.js" \
--test-coverage-include-all \
--test-coverage-lines=80
This example uses Node.js 26 because the include-all option was introduced in the Node.js 26 release line.
For production CI environments, pin the workflow to a Node.js release that includes the required feature rather than assuming that every Node.js version supports it.
Also remember that the option is experimental. Review the documentation when upgrading Node.js because experimental features can change.
Common Mistakes to Avoid
Mistake | Why it causes problems | Recommended approach |
|---|
Using only --test-coverage-include | It does not automatically add files that were never loaded | Add --test-coverage-include-all |
Running on an older Node.js version | The option is unavailable before Node.js 26.7.0 | Use a compatible Node.js release |
Forgetting the coverage flag | Coverage collection is not enabled | Use --experimental-test-coverage |
Including the entire repository | Configuration and generated files may distort results | Use focused source directory patterns |
Ignoring exclusion rules | Unnecessary files can appear in the report | Exclude generated and irrelevant files |
Assuming zero coverage means dead code | A file may simply have no tests importing it | Review its role before removing it |
Setting a high threshold immediately | Existing untested files can cause CI failures | Establish a baseline and increase thresholds gradually |
Treating coverage as proof of quality | Executed code may still contain incorrect behavior | Combine coverage with meaningful assertions and integration tests |
Best Practices for Production Node.js Projects
Start by defining which directories contain application source code. Use those directories in your coverage include patterns rather than including every JavaScript file in the repository.
Enable --test-coverage-include-all so that files that have never been imported are not silently omitted from the report. This is particularly useful when a project has recently added modules or contains legacy code that is no longer covered by the existing test suite.
Review zero-coverage files before deciding what to do with them. Some may represent missing tests, while others may be obsolete modules, generated code, or utilities that should be excluded.
Introduce coverage thresholds gradually. A project with several years of existing code may need time to reach a new coverage target. Start with a realistic baseline and increase it as new tests are added.
Keep the Node.js version consistent across developer machines and CI environments. Since the feature is experimental, verify its availability and behavior when updating the runtime.
Finally, focus on meaningful tests rather than simply increasing percentages. A test that executes a function without checking its output may improve coverage numbers without providing much confidence in the application.
Advantages and Limitations
The --test-coverage-include-all option provides several benefits:
Includes source files that were never imported during testing.
Reports untested files with zero coverage.
Gives developers a more complete view of project coverage.
Works with existing include and exclude glob patterns.
Supports the Node.js test runner command line and programmatic API.
Helps identify missing tests earlier in development.
Makes coverage thresholds more representative of the complete source code.
There are also some limitations:
It requires Node.js 26.7.0 or later.
The feature is experimental.
Candidate files are discovered from the current working directory.
Only files matching the configured include and exclude filters are considered.
A zero-coverage report does not explain why a file is untested.
Coverage percentages do not measure the quality of test assertions.
The feature improves coverage visibility, but developers still need to decide which files require tests and what those tests should verify.
Summary
Node.js 26.7.0 introduced --test-coverage-include-all, a useful addition to the built-in test runner's coverage capabilities. It allows developers to include source files that were never loaded during a test run and report them with zero coverage.
When combined with --test-coverage-include, --test-coverage-exclude, and coverage thresholds, it provides a more complete way to measure test coverage across a Node.js project.
For production applications, use focused source file patterns, exclude generated files, review zero-coverage modules, and introduce coverage thresholds gradually. Most importantly, use coverage reports to identify missing tests rather than treating a high percentage as proof that the application is fully tested.
Join the conversation! Your thoughts help the community grow.