Skip to content

Commit 00192a6

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 eaebfab commit 00192a6

21 files changed

+282
-25
lines changed

doc/api/cli.md

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

2511+
### `--test-files-glob=glob`
2512+
2513+
<!-- YAML
2514+
added: REPLACEME
2515+
-->
2516+
2517+
> Stability: 1.0 - Early development
2518+
2519+
Override the default test file glob patterns. This option is ignored if
2520+
positional `arguments` are provided as well.
2521+
2522+
See [running tests from the command line][] for more information on the
2523+
default patterns.
2524+
25112525
### `--test-force-exit`
25122526

25132527
<!-- YAML
@@ -3435,6 +3449,7 @@ one is included in the list below.
34353449
* `--test-coverage-functions`
34363450
* `--test-coverage-include`
34373451
* `--test-coverage-lines`
3452+
* `--test-files-glob`
34383453
* `--test-global-setup`
34393454
* `--test-isolation`
34403455
* `--test-name-pattern`

doc/api/test.md

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

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

470471
* `**/*.test.{cjs,mjs,js}`
471472
* `**/*-test.{cjs,mjs,js}`
@@ -3925,6 +3926,7 @@ Can be used to abort test subtasks when the test has been aborted.
39253926
[`--test-concurrency`]: cli.md#--test-concurrency
39263927
[`--test-coverage-exclude`]: cli.md#--test-coverage-exclude
39273928
[`--test-coverage-include`]: cli.md#--test-coverage-include
3929+
[`--test-files-glob`]: cli.md#--test-files-globglob
39283930
[`--test-name-pattern`]: cli.md#--test-name-pattern
39293931
[`--test-only`]: cli.md#--test-only
39303932
[`--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
@@ -395,6 +395,9 @@
395395
"test-coverage-lines": {
396396
"type": "number"
397397
},
398+
"test-files-glob": {
399+
"type": "string"
400+
},
398401
"test-global-setup": {
399402
"type": "string"
400403
},
@@ -638,6 +641,9 @@
638641
"test-coverage-lines": {
639642
"type": "number"
640643
},
644+
"test-files-glob": {
645+
"type": "string"
646+
},
641647
"test-force-exit": {
642648
"type": "boolean"
643649
},

lib/internal/test_runner/runner.js

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

114114
let kResistStopPropagation;
115115

116-
function createTestFileList(patterns, cwd) {
116+
function createTestFileList(patterns, cwd, defaultPattern) {
117117
const hasUserSuppliedPattern = patterns != null;
118118
if (!patterns || patterns.length === 0) {
119-
patterns = [kDefaultPattern];
119+
patterns = [defaultPattern || kDefaultPattern];
120120
}
121121
const glob = new Glob(patterns, {
122122
__proto__: null,
@@ -499,7 +499,7 @@ function watchFiles(testFiles, opts) {
499499
// Watch for changes in current filtered files
500500
watcher.on('changed', ({ owners, eventType }) => {
501501
if (!opts.hasFiles && (eventType === 'rename' || eventType === 'change')) {
502-
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd);
502+
const updatedTestFiles = createTestFileList(opts.globPatterns, opts.cwd, opts.testFilesGlob);
503503
const newFileName = ArrayPrototypeFind(updatedTestFiles, (x) => !ArrayPrototypeIncludes(testFiles, x));
504504
const previousFileName = ArrayPrototypeFind(testFiles, (x) => !ArrayPrototypeIncludes(updatedTestFiles, x));
505505

@@ -577,6 +577,7 @@ function run(options = kEmptyObject) {
577577
globalSetupPath,
578578
only,
579579
globPatterns,
580+
testFilesGlob,
580581
coverage = false,
581582
lineCoverage = 0,
582583
branchCoverage = 0,
@@ -607,6 +608,9 @@ function run(options = kEmptyObject) {
607608
if (globPatterns != null) {
608609
validateArray(globPatterns, 'options.globPatterns');
609610
}
611+
if (testFilesGlob != null) {
612+
validateString(testFilesGlob, 'options.testFilesGlob');
613+
}
610614

611615
validateString(cwd, 'options.cwd');
612616

@@ -705,7 +709,7 @@ function run(options = kEmptyObject) {
705709
globalSetupPath,
706710
};
707711
const root = createTestTree(rootTestOptions, globalOptions);
708-
let testFiles = files ?? createTestFileList(globPatterns, cwd);
712+
let testFiles = files ?? createTestFileList(globPatterns, cwd, testFilesGlob);
709713
const { isTestRunner } = globalOptions;
710714

711715
if (shard) {
@@ -723,6 +727,7 @@ function run(options = kEmptyObject) {
723727
inspectPort,
724728
testNamePatterns,
725729
testSkipPatterns,
730+
testFilesGlob,
726731
hasFiles: files != null,
727732
globPatterns,
728733
only,

lib/internal/test_runner/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ function parseCommandLine() {
202202
const updateSnapshots = getOptionValue('--test-update-snapshots');
203203
const watch = getOptionValue('--watch');
204204
const timeout = getOptionValue('--test-timeout') || Infinity;
205+
const testFilesGlob = getOptionValue('--test-files-glob') || null;
205206
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
206207
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
207208
let globalSetupPath;
@@ -295,7 +296,7 @@ function parseCommandLine() {
295296
if (!coverageExcludeGlobs || coverageExcludeGlobs.length === 0) {
296297
// TODO(pmarchini): this default should follow something similar to c8 defaults
297298
// Default exclusions should be also exported to be used by other tools / users
298-
coverageExcludeGlobs = [kDefaultPattern];
299+
coverageExcludeGlobs = [testFilesGlob || kDefaultPattern];
299300
}
300301
coverageIncludeGlobs = getOptionValue('--test-coverage-include');
301302

@@ -338,6 +339,7 @@ function parseCommandLine() {
338339
globalSetupPath,
339340
shard,
340341
sourceMaps,
342+
testFilesGlob,
341343
testNamePatterns,
342344
testSkipPatterns,
343345
timeout,

src/node_options.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,14 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
828828
&EnvironmentOptions::test_name_pattern,
829829
kAllowedInEnvvar,
830830
OptionNamespaces::kTestRunnerNamespace);
831+
AddOption("--test-files-glob",
832+
"set the default glob pattern for matching test files (default: "
833+
"'**/{test,test/**/*,test-*,*[._-]test}.{<extensions>}' where "
834+
"<extensions> is 'js,mjs,cjs' or 'js,mjs,cjs,ts,mts,cts' when using"
835+
" --experimental-strip-types)",
836+
&EnvironmentOptions::test_files_glob,
837+
kAllowedInEnvvar,
838+
OptionNamespaces::kTestRunnerNamespace);
831839
AddOption("--test-reporter",
832840
"report test output using the given reporter",
833841
&EnvironmentOptions::test_reporter,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class EnvironmentOptions : public Options {
197197
bool test_runner_module_mocks = false;
198198
bool test_runner_update_snapshots = false;
199199
std::vector<std::string> test_name_pattern;
200+
std::string test_files_glob;
200201
std::vector<std::string> test_reporter;
201202
std::vector<std::string> test_reporter_destination;
202203
std::string test_global_setup_path;
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');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
import test from 'node:test';
3+
4+
test('index-test.mjs should not run');

0 commit comments

Comments
 (0)