Skip to content

Commit 16fca17

Browse files
committed
test_runner: add config to harness
this commit changes the way test configurations are handled. config is now set initially on harness and never read from argv again
1 parent c40c41c commit 16fca17

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

lib/internal/main/test_runner.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
runnerConcurrency,
2828
shard,
2929
watchMode,
30+
...rest
3031
} = parseCommandLine();
3132

3233
let concurrency = runnerConcurrency;
@@ -47,6 +48,7 @@ const options = {
4748
timeout: perFileTimeout,
4849
shard,
4950
globPatterns: ArrayPrototypeSlice(process.argv, 1),
51+
...rest,
5052
};
5153
debug('test runner configuration:', options);
5254
run(options).on('test:fail', (data) => {

lib/internal/test_runner/harness.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const {
33
ArrayPrototypeForEach,
44
FunctionPrototypeBind,
5+
ObjectAssign,
56
PromiseResolve,
67
SafeMap,
78
} = primordials;
@@ -34,8 +35,8 @@ let globalRoot;
3435

3536
testResources.set(reporterScope.asyncId(), reporterScope);
3637

37-
function createTestTree(options = kEmptyObject) {
38-
globalRoot = setup(new Test({ __proto__: null, ...options, name: '<root>' }));
38+
function createTestTree(options = kEmptyObject, config) {
39+
globalRoot = setup(new Test({ __proto__: null, ...options, name: '<root>' }, config ?? parseCommandLine()));
3940
return globalRoot;
4041
}
4142

@@ -134,7 +135,7 @@ function setup(root) {
134135

135136
// Parse the command line options before the hook is enabled. We don't want
136137
// global input validation errors to end up in the uncaughtException handler.
137-
const globalOptions = parseCommandLine();
138+
const globalOptions = root.harness.config;
138139

139140
const hook = createHook({
140141
__proto__: null,
@@ -195,7 +196,7 @@ function setup(root) {
195196
process.on('SIGTERM', terminationHandler);
196197
}
197198

198-
root.harness = {
199+
ObjectAssign(root.harness, {
199200
__proto__: null,
200201
allowTestsToRun: false,
201202
bootstrapPromise: resolvedPromise,
@@ -218,7 +219,7 @@ function setup(root) {
218219
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
219220
teardown: exitHandler,
220221
snapshotManager: null,
221-
};
222+
});
222223
root.harness.resetCounters();
223224
root.startTime = hrtime();
224225
return root;

lib/internal/test_runner/runner.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,25 @@ function run(options = kEmptyObject) {
560560
throw new ERR_INVALID_ARG_TYPE(name, ['string', 'RegExp'], value);
561561
});
562562
}
563-
564-
const root = createTestTree({ __proto__: null, concurrency, timeout, signal });
563+
const root = createTestTree(
564+
{ __proto__: null, concurrency, timeout, signal },
565+
{
566+
__proto__: null,
567+
forceExit,
568+
perFileTimeout: timeout || Infinity,
569+
runnerConcurrency: concurrency,
570+
shard,
571+
sourceMaps: options.sourceMaps,
572+
testOnlyFlag: options.isTestRunner ? false : only,
573+
testNamePatterns,
574+
testSkipPatterns,
575+
updateSnapshots: options.updateSnapshots,
576+
watchMode: watch,
577+
isTestRunner: options.isTestRunner || false,
578+
globPatterns,
579+
destinations: options.destinations || [],
580+
},
581+
);
565582

566583
if (process.env.NODE_TEST_CONTEXT !== undefined) {
567584
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');

lib/internal/test_runner/test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const {
4545
createDeferredCallback,
4646
countCompletedTest,
4747
isTestFailureError,
48-
parseCommandLine,
4948
} = require('internal/test_runner/utils');
5049
const {
5150
createDeferredPromise,
@@ -79,14 +78,6 @@ const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
7978
const kUnwrapErrors = new SafeSet()
8079
.add(kTestCodeFailure).add(kHookFailure)
8180
.add('uncaughtException').add('unhandledRejection');
82-
const {
83-
forceExit,
84-
sourceMaps,
85-
testNamePatterns,
86-
testSkipPatterns,
87-
testOnlyFlag,
88-
updateSnapshots,
89-
} = parseCommandLine();
9081
let kResistStopPropagation;
9182
let assertObj;
9283
let findSourceMap;
@@ -132,7 +123,7 @@ function lazyAssertObject(harness) {
132123
const { getOptionValue } = require('internal/options');
133124
if (getOptionValue('--experimental-test-snapshots')) {
134125
const { SnapshotManager } = require('internal/test_runner/snapshot');
135-
harness.snapshotManager = new SnapshotManager(updateSnapshots);
126+
harness.snapshotManager = new SnapshotManager(harness.config.updateSnapshots);
136127
assertObj.set('snapshot', harness.snapshotManager.createAssert());
137128
}
138129
}
@@ -360,7 +351,7 @@ class Test extends AsyncResource {
360351
outerSignal;
361352
#reportedSubtest;
362353

363-
constructor(options) {
354+
constructor(options, config) {
364355
super('Test');
365356

366357
let { fn, name, parent } = options;
@@ -388,7 +379,7 @@ class Test extends AsyncResource {
388379
if (parent === null) {
389380
this.concurrency = 1;
390381
this.nesting = 0;
391-
this.only = testOnlyFlag;
382+
this.only = config?.testOnlyFlag;
392383
this.reporter = new TestsStream();
393384
this.runOnlySubtests = this.only;
394385
this.childNumber = 0;
@@ -430,6 +421,7 @@ class Test extends AsyncResource {
430421
this.parent.filteredSubtestCount++;
431422
}
432423

424+
const { testOnlyFlag } = config ?? this.root.harness.config;
433425
if (testOnlyFlag && only === false) {
434426
fn = noop;
435427
}
@@ -501,6 +493,11 @@ class Test extends AsyncResource {
501493
this.waitingOn = 0;
502494
this.finished = false;
503495

496+
if (name === '<root>' && config != null) {
497+
this.harness = { __proto__: null, config };
498+
}
499+
500+
const { testOnlyFlag } = config ?? this.root.harness.config;
504501
if (!testOnlyFlag && (only || this.runOnlySubtests)) {
505502
const warning =
506503
"'only' and 'runOnly' require the --test-only command-line option.";
@@ -517,7 +514,7 @@ class Test extends AsyncResource {
517514
file: loc[2],
518515
};
519516

520-
if (sourceMaps === true) {
517+
if (config?.sourceMaps === true) {
521518
const map = lazyFindSourceMap(this.loc.file);
522519
const entry = map?.findEntry(this.loc.line - 1, this.loc.column - 1);
523520

@@ -535,6 +532,7 @@ class Test extends AsyncResource {
535532
}
536533

537534
willBeFiltered() {
535+
const { testOnlyFlag, testNamePatterns, testSkipPatterns } = this.root?.harness?.config || {};
538536
if (testOnlyFlag && !this.only) return true;
539537

540538
if (testNamePatterns && !testMatchesPattern(this, testNamePatterns)) {
@@ -905,7 +903,7 @@ class Test extends AsyncResource {
905903
// This helps catch any asynchronous activity that occurs after the tests
906904
// have finished executing.
907905
this.postRun();
908-
} else if (forceExit) {
906+
} else if (this.harness?.config.forceExit) {
909907
// This is the root test, and all known tests and hooks have finished
910908
// executing. If the user wants to force exit the process regardless of
911909
// any remaining ref'ed handles, then do that now. It is theoretically
@@ -1147,6 +1145,7 @@ class Suite extends Test {
11471145
constructor(options) {
11481146
super(options);
11491147

1148+
const { testNamePatterns, testSkipPatterns, testOnlyFlag } = this.root?.harness?.config || {};
11501149
if (testNamePatterns !== null && testSkipPatterns !== null && !options.skip) {
11511150
this.fn = options.fn || this.fn;
11521151
this.skipped = false;
@@ -1175,6 +1174,7 @@ class Suite extends Test {
11751174
}
11761175

11771176
postBuild() {
1177+
const { testNamePatterns, testSkipPatterns, testOnlyFlag } = this.root?.harness?.config || {};
11781178
this.buildPhaseFinished = true;
11791179
if (this.filtered && this.filteredSubtestCount !== this.subtests.length) {
11801180
// A suite can transition from filtered to unfiltered based on the

0 commit comments

Comments
 (0)