Skip to content

Commit ecaeaf8

Browse files
cjihrignodejs-github-bot
authored andcommitted
test_runner: pass harness object as option to root test
This commit initializes the root harness object before the root test and passes the harness as an option to the root test constructor. This commit also attaches the global configuration to the harness. This will allow the parseCommandLine() call in test.js to be removed, as those values are now available via the root test. PR-URL: #54353 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 1abdd66 commit ecaeaf8

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

lib/internal/test_runner/harness.js

+39-37
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,42 @@ let globalRoot;
3535
testResources.set(reporterScope.asyncId(), reporterScope);
3636

3737
function createTestTree(options = kEmptyObject) {
38-
globalRoot = setup(new Test({ __proto__: null, ...options, name: '<root>' }));
38+
const globalOptions = parseCommandLine();
39+
const harness = {
40+
__proto__: null,
41+
allowTestsToRun: false,
42+
bootstrapPromise: resolvedPromise,
43+
watching: false,
44+
config: globalOptions,
45+
coverage: null,
46+
resetCounters() {
47+
harness.counters = {
48+
__proto__: null,
49+
all: 0,
50+
failed: 0,
51+
passed: 0,
52+
cancelled: 0,
53+
skipped: 0,
54+
todo: 0,
55+
topLevel: 0,
56+
suites: 0,
57+
};
58+
},
59+
counters: null,
60+
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
61+
teardown: null,
62+
snapshotManager: null,
63+
};
64+
65+
harness.resetCounters();
66+
globalRoot = new Test({
67+
__proto__: null,
68+
...options,
69+
harness,
70+
name: '<root>',
71+
});
72+
setupProcessState(globalRoot, globalOptions, harness);
73+
globalRoot.startTime = hrtime();
3974
return globalRoot;
4075
}
4176

@@ -127,15 +162,7 @@ function collectCoverage(rootTest, coverage) {
127162
return summary;
128163
}
129164

130-
function setup(root) {
131-
if (root.startTime !== null) {
132-
return root;
133-
}
134-
135-
// Parse the command line options before the hook is enabled. We don't want
136-
// global input validation errors to end up in the uncaughtException handler.
137-
const globalOptions = parseCommandLine();
138-
165+
function setupProcessState(root, globalOptions) {
139166
const hook = createHook({
140167
__proto__: null,
141168
init(asyncId, type, triggerAsyncId, resource) {
@@ -195,33 +222,8 @@ function setup(root) {
195222
process.on('SIGTERM', terminationHandler);
196223
}
197224

198-
root.harness = {
199-
__proto__: null,
200-
allowTestsToRun: false,
201-
bootstrapPromise: resolvedPromise,
202-
watching: false,
203-
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
204-
resetCounters() {
205-
root.harness.counters = {
206-
__proto__: null,
207-
all: 0,
208-
failed: 0,
209-
passed: 0,
210-
cancelled: 0,
211-
skipped: 0,
212-
todo: 0,
213-
topLevel: 0,
214-
suites: 0,
215-
};
216-
},
217-
counters: null,
218-
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
219-
teardown: exitHandler,
220-
snapshotManager: null,
221-
};
222-
root.harness.resetCounters();
223-
root.startTime = hrtime();
224-
return root;
225+
root.harness.coverage = FunctionPrototypeBind(collectCoverage, null, root, coverage);
226+
root.harness.teardown = exitHandler;
225227
}
226228

227229
function lazyBootstrapRoot() {

lib/internal/test_runner/test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ class Test extends AsyncResource {
395395
this.timeout = kDefaultTimeout;
396396
this.entryFile = entryFile;
397397
this.root = this;
398+
this.harness = options.harness;
398399
this.hooks = {
399400
__proto__: null,
400401
before: [],
@@ -416,6 +417,7 @@ class Test extends AsyncResource {
416417
this.timeout = parent.timeout;
417418
this.entryFile = parent.entryFile;
418419
this.root = parent.root;
420+
this.harness = null;
419421
this.hooks = {
420422
__proto__: null,
421423
before: [],
@@ -480,7 +482,6 @@ class Test extends AsyncResource {
480482
);
481483

482484
this.fn = fn;
483-
this.harness = null; // Configured on the root test by the test harness.
484485
this.mock = null;
485486
this.plan = null;
486487
this.expectedAssertions = plan;

0 commit comments

Comments
 (0)