Registry / testing / test-exclude

test-exclude

JSON →
library8.0.0jsnpmunverified

test-exclude is a JavaScript library designed to apply include and exclude glob patterns to file paths, primarily utilized within the istanbuljs ecosystem for test coverage tools like nyc and babel-plugin-istanbul. The current stable version is 8.0.0. Its release cadence is driven by updates to underlying dependencies like glob and minimatch, and by Node.js version support, typically seeing a few major versions per year. Key differentiators include its tight integration with istanbuljs tooling, robust handling of common exclusion patterns such as node_modules by default, and support for configurable file extensions. It offers both synchronous (globSync) and asynchronous (glob) methods for retrieving lists of files that match the defined inclusion/exclusion rules, making it versatile for various build and testing workflows.

npm install test-exclude
INSTALL
IMPORT
SIG · TEST-EXCLUDE
T
test-exclude
testingjavascriptv8.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TestExclude
const TestExclude = require('test-exclude');
CommonJS import for Node.js environments. TestExclude is exported as the default.
TestExclude
import TestExclude from 'test-exclude';
import { TestExclude } from 'test-exclude';
ES Modules import for Node.js and browser environments. TestExclude is a default export, not a named export.
TestExcludeConstructor
import type { TestExcludeOptions } from 'test-exclude';
import { TestExcludeOptions } from 'test-exclude';
Importing types for TypeScript. Options interface is available for strict typing.

Demonstrates how to initialize TestExclude, use `shouldInstrument` for individual files, and `glob` to find all matching files within a CWD.

import TestExclude from 'test-exclude'; import * as fs from 'fs'; import * as path from 'path'; // Create a temporary directory and files for demonstration const tempDir = path.join(__dirname, 'temp_project'); const srcDir = path.join(tempDir, 'src'); const testDir = path.join(tempDir, 'test'); const nodeModulesDir = path.join(tempDir, 'node_modules'); fs.mkdirSync(srcDir, { recursive: true }); fs.mkdirSync(testDir, { recursive: true }); fs.mkdirSync(nodeModulesDir, { recursive: true }); fs.writeFileSync(path.join(srcDir, 'index.js'), '// main code'); fs.writeFileSync(path.join(srcDir, 'util.ts'), '// utility code'); fs.writeFileSync(path.join(testDir, 'index.test.js'), '// test code'); fs.writeFileSync(path.join(nodeModulesDir, 'some-lib', 'index.js'), '// third-party lib'); fs.writeFileSync(path.join(tempDir, 'README.md'), '# Project'); console.log('--- Demonstrating test-exclude ---'); // Initialize TestExclude with specific options const exclude = new TestExclude({ cwd: tempDir, exclude: ['test/**/*.js', 'README.md'], // Exclude test files and README include: ['src/**/*.js', 'src/**/*.ts'], // Only include files in src extension: ['.js', '.ts'], // Only consider JS and TS files }); // Test individual files console.log(`Should instrument ${path.relative(tempDir, path.join(srcDir, 'index.js'))}?`, exclude.shouldInstrument(path.join(srcDir, 'index.js'))); console.log(`Should instrument ${path.relative(tempDir, path.join(srcDir, 'util.ts'))}?`, exclude.shouldInstrument(path.join(srcDir, 'util.ts'))); console.log(`Should instrument ${path.relative(tempDir, path.join(testDir, 'index.test.js'))}?`, exclude.shouldInstrument(path.join(testDir, 'index.test.js'))); console.log(`Should instrument ${path.relative(tempDir, path.join(nodeModulesDir, 'some-lib', 'index.js'))}?`, exclude.shouldInstrument(path.join(nodeModulesDir, 'some-lib', 'index.js'))); console.log(`Should instrument ${path.relative(tempDir, path.join(tempDir, 'README.md'))}?`, exclude.shouldInstrument(path.join(tempDir, 'README.md'))); // Get all instrumentable files within the cwd async function runGlob() { const instrumentableFiles = await exclude.glob(); console.log('\nInstrumentable files found:', instrumentableFiles.map(file => path.relative(tempDir, file))); // Clean up temporary files fs.rmSync(tempDir, { recursive: true, force: true }); console.log('\nTemporary files cleaned up.'); } runGlob();
Debug
Known issues
breakingVersion 8.0.0 and newer of test-exclude requires Node.js version 20 or higher due to transitive dependency updates.
fix
Ensure your project's Node.js environment is version 20 or greater. Update your `package.json` engines field to reflect this requirement and upgrade Node.js if necessary.
affects: >=8.0.0
breakingVersion 7.0.0 of test-exclude raised the minimum Node.js requirement to version 18.
fix
If migrating from a version prior to 7.0.0, upgrade your Node.js environment to version 18 or greater.
affects: >=7.0.0 <8.0.0
gotchaBy default, files within `node_modules` are automatically excluded from instrumentation. To include them, you must explicitly set the `excludeNodeModules` option to `false` in the `TestExclude` constructor.
fix
Pass `{ excludeNodeModules: false }` to the `TestExclude` constructor if you intend to instrument files within `node_modules`.
affects: >=1.0.0
gotchaThe `cwd` option defines the base directory for all path comparisons. Files located outside of this `cwd` will never be considered for inclusion, regardless of `include` or `exclude` patterns.
fix
Always ensure your `TestExclude` instance is initialized with a `cwd` that encompasses all relevant files you intend to process. For `glob` and `globSync`, the `cwd` argument can only further restrict results within `options.cwd`.
affects: >=1.0.0
gotchaThe `options.extension` array filters files by their extension. If a file's extension is not in this list, it will not be considered for instrumentation. For certain formats (e.g., TS, JSX), additional parser plugins might be required in your coverage configuration (e.g., `nyc` or `babel-plugin-istanbul`).
fix
Ensure all relevant file extensions are listed in `options.extension`. If using TypeScript or JSX, verify your build or coverage tooling has the necessary Babel/TypeScript plugins configured.
affects: >=1.0.0
Errors
Common errors & fixes
Error: This module was compiled against a different Node.js version
Attempting to run test-exclude v8.0.0 or later on a Node.js version older than 20.
fix
Upgrade your Node.js environment to version 20 or newer. Check your CI/CD pipelines and local development setups.
File not instrumented: unexpectedly excluded by default rules.
A file that should be covered is being ignored, most commonly due to being in `node_modules` or matching a default `exclude` pattern.
fix
Inspect `options.excludeNodeModules` and `options.exclude` when initializing `TestExclude`. You may need to set `excludeNodeModules: false` or override specific patterns in your `exclude` array. Also, verify `options.extension`.
TestExclude#glob() or TestExclude#globSync() returns an empty array when files are expected.
The `cwd` option, `include`/`exclude` patterns, or `extension` list are too restrictive, or the `cwd` argument to `glob` is set incorrectly.
fix
Verify the `cwd` is correct and encompasses your project. Review `options.include`, `options.exclude`, and `options.extension`. Ensure the `cwd` argument to `glob` is not stricter than the constructor's `options.cwd`.
Upgrade
Version history
8.0.0latest on npm
Audit
Dependencies
globrequiredCore functionality for matching file paths against glob patterns.
minimatchrequiredUnderlying library for advanced pattern matching logic.
Agent activity
4 hits · last 30 days
node
4
Resources