Skip to content

Commit 13df8ca

Browse files
committed
test: add --test-files-glob flag
Overrides the default `kDefaultPattern` for test file globs to allow for default file pattern matching, while still allowing other options or single, targeted file names for testing. This also might resolve issues with the defaults when using strip-types since the defaults can, currently, only be overridden using the positional `arguments` (see #56546). Fixes: #51384 Ref: #56546
1 parent c6316f9 commit 13df8ca

22 files changed

+283
-25
lines changed

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,20 @@ added: v22.8.0
25912591
Require a minimum percent of covered lines. If code coverage does not reach
25922592
the threshold specified, the process will exit with code `1`.
25932593

2594+
### `--test-files-glob=glob`
2595+
2596+
<!-- YAML
2597+
added: REPLACEME
2598+
-->
2599+
2600+
> Stability: 1.0 - Early development
2601+
2602+
Override the default test file glob patterns. This option is ignored if
2603+
positional `arguments` are provided as well.
2604+
2605+
See [running tests from the command line][] for more information on the
2606+
default patterns.
2607+
25942608
### `--test-force-exit`
25952609

25962610
<!-- YAML
@@ -3571,6 +3585,7 @@ one is included in the list below.
35713585
* `--test-coverage-functions`
35723586
* `--test-coverage-include`
35733587
* `--test-coverage-lines`
3588+
* `--test-files-glob`
35743589
* `--test-global-setup`
35753590
* `--test-isolation`
35763591
* `--test-name-pattern`

β€Ždoc/api/test.mdβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ The Node.js test runner can be invoked from the command line by passing the
505505
node --test
506506
```
507507

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

510511
* `**/*.test.{cjs,mjs,js}`
511512
* `**/*-test.{cjs,mjs,js}`
@@ -4000,6 +4001,7 @@ Can be used to abort test subtasks when the test has been aborted.
40004001
[`--test-concurrency`]: cli.md#--test-concurrency
40014002
[`--test-coverage-exclude`]: cli.md#--test-coverage-exclude
40024003
[`--test-coverage-include`]: cli.md#--test-coverage-include
4004+
[`--test-files-glob`]: cli.md#--test-files-globglob
40034005
[`--test-name-pattern`]: cli.md#--test-name-pattern
40044006
[`--test-only`]: cli.md#--test-only
40054007
[`--test-reporter-destination`]: cli.md#--test-reporter-destination

β€Ždoc/node-config-schema.jsonβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@
398398
"test-coverage-lines": {
399399
"type": "number"
400400
},
401+
"test-files-glob": {
402+
"type": "string"
403+
},
401404
"test-global-setup": {
402405
"type": "string"
403406
},
@@ -653,6 +656,9 @@
653656
"test-coverage-lines": {
654657
"type": "number"
655658
},
659+
"test-files-glob": {
660+
"type": "string"
661+
},
656662
"test-force-exit": {
657663
"type": "boolean"
658664
},

β€Ždoc/node.1β€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ A glob pattern that only includes matching files in the coverage report
476476
.It Fl -test-coverage-lines Ns = Ns Ar threshold
477477
Require a minimum threshold for line coverage (0 - 100).
478478
.
479+
.It Fl -test-files-glob
480+
A glob pattern that configures the test runner to only run tests whose filename
481+
matches the provided glob.
482+
.
479483
.It Fl -test-force-exit
480484
Configures the test runner to exit the process once all known tests have
481485
finished executing even if the event loop would otherwise remain active.

β€Žlib/internal/test_runner/runner.jsβ€Ž

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ const kCanceledTests = new SafeSet()
116116

117117
let kResistStopPropagation;
118118

119-
function createTestFileList(patterns, cwd) {
119+
function createTestFileList(patterns, cwd, defaultPattern) {
120120
const hasUserSuppliedPattern = patterns != null;
121121
if (!patterns || patterns.length === 0) {
122-
patterns = [kDefaultPattern];
122+
patterns = [defaultPattern || kDefaultPattern];
123123
}
124124
const glob = new Glob(patterns, {
125125
__proto__: null,
@@ -507,7 +507,7 @@ function watchFiles(testFiles, opts) {
507507
// Watch for changes in current filtered files
508508
watcher.on('changed', ({ owners, eventType }) => {
509509
if (!opts.hasFiles && (eventType === 'rename' || eventType === 'change')) {
510-
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd);
510+
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd, opts.testFilesGlob);
511511
const newFileName = ArrayPrototypeFind(updatedTestFiles, (x) => !ArrayPrototypeIncludes(testFiles, x));
512512
const previousFileName = ArrayPrototypeFind(testFiles, (x) => !ArrayPrototypeIncludes(updatedTestFiles, x));
513513

@@ -585,6 +585,7 @@ function run(options = kEmptyObject) {
585585
globalSetupPath,
586586
only,
587587
globPatterns,
588+
testFilesGlob,
588589
coverage = false,
589590
lineCoverage = 0,
590591
branchCoverage = 0,
@@ -616,6 +617,9 @@ function run(options = kEmptyObject) {
616617
if (globPatterns != null) {
617618
validateArray(globPatterns, 'options.globPatterns');
618619
}
620+
if (testFilesGlob != null) {
621+
validateString(testFilesGlob, 'options.testFilesGlob');
622+
}
619623

620624
validateString(cwd, 'options.cwd');
621625

@@ -719,7 +723,7 @@ function run(options = kEmptyObject) {
719723
globalSetupPath,
720724
};
721725
const root = createTestTree(rootTestOptions, globalOptions);
722-
let testFiles = files ?? createTestFileList(globPatterns, cwd);
726+
let testFiles = files ?? createTestFileList(globPatterns, cwd, testFilesGlob);
723727
const { isTestRunner } = globalOptions;
724728

725729
if (shard) {
@@ -737,6 +741,7 @@ function run(options = kEmptyObject) {
737741
inspectPort,
738742
testNamePatterns,
739743
testSkipPatterns,
744+
testFilesGlob,
740745
hasFiles: files != null,
741746
globPatterns,
742747
only,

β€Žlib/internal/test_runner/utils.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ function parseCommandLine() {
218218
const watch = getOptionValue('--watch');
219219
const timeout = getOptionValue('--test-timeout') || Infinity;
220220
const rerunFailuresFilePath = getOptionValue('--test-rerun-failures');
221+
const testFilesGlob = getOptionValue('--test-files-glob') || null;
221222
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
222223
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
223224
let globalSetupPath;
@@ -311,7 +312,7 @@ function parseCommandLine() {
311312
if (!coverageExcludeGlobs || coverageExcludeGlobs.length === 0) {
312313
// TODO(pmarchini): this default should follow something similar to c8 defaults
313314
// Default exclusions should be also exported to be used by other tools / users
314-
coverageExcludeGlobs = [kDefaultPattern];
315+
coverageExcludeGlobs = [testFilesGlob || kDefaultPattern];
315316
}
316317
coverageIncludeGlobs = getOptionValue('--test-coverage-include');
317318

@@ -357,6 +358,7 @@ function parseCommandLine() {
357358
globalSetupPath,
358359
shard,
359360
sourceMaps,
361+
testFilesGlob,
360362
testNamePatterns,
361363
testSkipPatterns,
362364
timeout,

β€Žsrc/node_options.ccβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
884884
&EnvironmentOptions::test_name_pattern,
885885
kAllowedInEnvvar,
886886
OptionNamespaces::kTestRunnerNamespace);
887+
AddOption("--test-files-glob",
888+
"set the default glob pattern for matching test files",
889+
&EnvironmentOptions::test_files_glob,
890+
kAllowedInEnvvar,
891+
OptionNamespaces::kTestRunnerNamespace);
887892
AddOption("--test-reporter",
888893
"report test output using the given reporter",
889894
&EnvironmentOptions::test_reporter,

β€Žsrc/node_options.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class EnvironmentOptions : public Options {
199199
bool test_runner_module_mocks = false;
200200
bool test_runner_update_snapshots = false;
201201
std::vector<std::string> test_name_pattern;
202+
std::string test_files_glob;
202203
std::vector<std::string> test_reporter;
203204
std::string test_rerun_failures_path;
204205
std::vector<std::string> test_reporter_destination;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const test = require('node:test');
3+
4+
test('index-test.cjs should not run');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const test = require('node:test');
3+
4+
test('index-test.js should not run');

0 commit comments

Comments
Β (0)