Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,20 @@ added: v22.8.0
Require a minimum percent of covered lines. If code coverage does not reach
the threshold specified, the process will exit with code `1`.

### `--test-files-glob=glob`

<!-- YAML
added: REPLACEME
-->

> Stability: 1.0 - Early development

Override the default test file glob patterns. This option is ignored if
positional `arguments` are provided as well.

See [running tests from the command line][] for more information on the
default patterns.

### `--test-force-exit`

<!-- YAML
Expand Down Expand Up @@ -3581,6 +3595,7 @@ one is included in the list below.
* `--test-coverage-functions`
* `--test-coverage-include`
* `--test-coverage-lines`
* `--test-files-glob`
* `--test-global-setup`
* `--test-isolation`
* `--test-name-pattern`
Expand Down
4 changes: 3 additions & 1 deletion doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ The Node.js test runner can be invoked from the command line by passing the
node --test
```

By default, Node.js will run all files matching these patterns:
Unless overridden with the [`--test-files-glob`][] flag, by default Node.js will
run all files matching these patterns:

* `**/*.test.{cjs,mjs,js}`
* `**/*-test.{cjs,mjs,js}`
Expand Down Expand Up @@ -4000,6 +4001,7 @@ Can be used to abort test subtasks when the test has been aborted.
[`--test-concurrency`]: cli.md#--test-concurrency
[`--test-coverage-exclude`]: cli.md#--test-coverage-exclude
[`--test-coverage-include`]: cli.md#--test-coverage-include
[`--test-files-glob`]: cli.md#--test-files-globglob
[`--test-name-pattern`]: cli.md#--test-name-pattern
[`--test-only`]: cli.md#--test-only
[`--test-reporter-destination`]: cli.md#--test-reporter-destination
Expand Down
6 changes: 6 additions & 0 deletions doc/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@
"test-coverage-lines": {
"type": "number"
},
"test-files-glob": {
"type": "string"
},
"test-global-setup": {
"type": "string"
},
Expand Down Expand Up @@ -653,6 +656,9 @@
"test-coverage-lines": {
"type": "number"
},
"test-files-glob": {
"type": "string"
},
"test-force-exit": {
"type": "boolean"
},
Expand Down
4 changes: 4 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,10 @@ A glob pattern that only includes matching files in the coverage report
.It Fl -test-coverage-lines Ns = Ns Ar threshold
Require a minimum threshold for line coverage (0 - 100).
.
.It Fl -test-files-glob
A glob pattern that configures the test runner to only run tests whose filename
matches the provided glob.
.
.It Fl -test-force-exit
Configures the test runner to exit the process once all known tests have
finished executing even if the event loop would otherwise remain active.
Expand Down
13 changes: 9 additions & 4 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ const kCanceledTests = new SafeSet()

let kResistStopPropagation;

function createTestFileList(patterns, cwd) {
function createTestFileList(patterns, cwd, defaultPattern) {
const hasUserSuppliedPattern = patterns != null;
if (!patterns || patterns.length === 0) {
patterns = [kDefaultPattern];
patterns = [defaultPattern || kDefaultPattern];
}
const glob = new Glob(patterns, {
__proto__: null,
Expand Down Expand Up @@ -507,7 +507,7 @@ function watchFiles(testFiles, opts) {
// Watch for changes in current filtered files
watcher.on('changed', ({ owners, eventType }) => {
if (!opts.hasFiles && (eventType === 'rename' || eventType === 'change')) {
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd);
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd, opts.testFilesGlob);
const newFileName = ArrayPrototypeFind(updatedTestFiles, (x) => !ArrayPrototypeIncludes(testFiles, x));
const previousFileName = ArrayPrototypeFind(testFiles, (x) => !ArrayPrototypeIncludes(updatedTestFiles, x));

Expand Down Expand Up @@ -585,6 +585,7 @@ function run(options = kEmptyObject) {
globalSetupPath,
only,
globPatterns,
testFilesGlob,
coverage = false,
lineCoverage = 0,
branchCoverage = 0,
Expand Down Expand Up @@ -616,6 +617,9 @@ function run(options = kEmptyObject) {
if (globPatterns != null) {
validateArray(globPatterns, 'options.globPatterns');
}
if (testFilesGlob != null) {
validateString(testFilesGlob, 'options.testFilesGlob');
}

validateString(cwd, 'options.cwd');

Expand Down Expand Up @@ -719,7 +723,7 @@ function run(options = kEmptyObject) {
globalSetupPath,
};
const root = createTestTree(rootTestOptions, globalOptions);
let testFiles = files ?? createTestFileList(globPatterns, cwd);
let testFiles = files ?? createTestFileList(globPatterns, cwd, testFilesGlob);
const { isTestRunner } = globalOptions;

if (shard) {
Expand All @@ -737,6 +741,7 @@ function run(options = kEmptyObject) {
inspectPort,
testNamePatterns,
testSkipPatterns,
testFilesGlob,
hasFiles: files != null,
globPatterns,
only,
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ function parseCommandLine() {
const watch = getOptionValue('--watch');
const timeout = getOptionValue('--test-timeout') || Infinity;
const rerunFailuresFilePath = getOptionValue('--test-rerun-failures');
const testFilesGlob = getOptionValue('--test-files-glob') || null;
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
let globalSetupPath;
Expand Down Expand Up @@ -311,7 +312,7 @@ function parseCommandLine() {
if (!coverageExcludeGlobs || coverageExcludeGlobs.length === 0) {
// TODO(pmarchini): this default should follow something similar to c8 defaults
// Default exclusions should be also exported to be used by other tools / users
coverageExcludeGlobs = [kDefaultPattern];
coverageExcludeGlobs = [testFilesGlob || kDefaultPattern];
}
coverageIncludeGlobs = getOptionValue('--test-coverage-include');

Expand Down Expand Up @@ -357,6 +358,7 @@ function parseCommandLine() {
globalSetupPath,
shard,
sourceMaps,
testFilesGlob,
testNamePatterns,
testSkipPatterns,
timeout,
Expand Down
8 changes: 8 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,14 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::test_name_pattern,
kAllowedInEnvvar,
OptionNamespaces::kTestRunnerNamespace);
AddOption("--test-files-glob",
"set the default glob pattern for matching test files (default: "
"'**/{test,test/**/*,test-*,*[._-]test}.{<extensions>}' where "
"<extensions> is 'js,mjs,cjs' or 'js,mjs,cjs,ts,mts,cts' when using"
" --experimental-strip-types)",
&EnvironmentOptions::test_files_glob,
kAllowedInEnvvar,
OptionNamespaces::kTestRunnerNamespace);
AddOption("--test-reporter",
"report test output using the given reporter",
&EnvironmentOptions::test_reporter,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class EnvironmentOptions : public Options {
bool test_runner_module_mocks = false;
bool test_runner_update_snapshots = false;
std::vector<std::string> test_name_pattern;
std::string test_files_glob;
std::vector<std::string> test_reporter;
std::string test_rerun_failures_path;
std::vector<std::string> test_reporter_destination;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/test-runner/custom-files-glob/index-test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
const test = require('node:test');

test('index-test.cjs should not run');
4 changes: 4 additions & 0 deletions test/fixtures/test-runner/custom-files-glob/index-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
const test = require('node:test');

test('index-test.js should not run');
4 changes: 4 additions & 0 deletions test/fixtures/test-runner/custom-files-glob/index-test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
import test from 'node:test';

test('index-test.mjs should not run');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
const test = require('node:test');

test('index-test.spec.cjs this should pass');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
const test = require('node:test');

test('index-test.spec.js this should pass');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
import test from 'node:test';

test('index-test.spec.mjs this should pass');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const test = require('node:test');

// 'as string' ensures that type stripping actually occurs
test('typescript-test.cts should not run' as string);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { test } from 'node:test';

// 'as string' ensures that type stripping actually occurs
test('typescript-test.mts should not run' as string);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const test = require('node:test');

// 'as string' ensures that type stripping actually occurs
test('typescript-test.spec.cts this should pass' as string);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { test } from 'node:test';

// 'as string' ensures that type stripping actually occurs
test('typescript-test.spec.mts this should pass' as string);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const test = require('node:test');

// 'as string' ensures that type stripping actually occurs
test('typescript-test.spec.ts this should pass' as string);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const test = require('node:test');

// 'as string' ensures that type stripping actually occurs
test('typescript-test.ts this should pass' as string);
Loading