Skip to content

Commit

Permalink
test_runner: pass harness object as option to root test
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig authored and nodejs-github-bot committed Aug 15, 2024
1 parent 1abdd66 commit ecaeaf8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
76 changes: 39 additions & 37 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,42 @@ let globalRoot;
testResources.set(reporterScope.asyncId(), reporterScope);

function createTestTree(options = kEmptyObject) {
globalRoot = setup(new Test({ __proto__: null, ...options, name: '<root>' }));
const globalOptions = parseCommandLine();
const harness = {
__proto__: null,
allowTestsToRun: false,
bootstrapPromise: resolvedPromise,
watching: false,
config: globalOptions,
coverage: null,
resetCounters() {
harness.counters = {
__proto__: null,
all: 0,
failed: 0,
passed: 0,
cancelled: 0,
skipped: 0,
todo: 0,
topLevel: 0,
suites: 0,
};
},
counters: null,
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
teardown: null,
snapshotManager: null,
};

harness.resetCounters();
globalRoot = new Test({
__proto__: null,
...options,
harness,
name: '<root>',
});
setupProcessState(globalRoot, globalOptions, harness);
globalRoot.startTime = hrtime();
return globalRoot;
}

Expand Down Expand Up @@ -127,15 +162,7 @@ function collectCoverage(rootTest, coverage) {
return summary;
}

function setup(root) {
if (root.startTime !== null) {
return root;
}

// Parse the command line options before the hook is enabled. We don't want
// global input validation errors to end up in the uncaughtException handler.
const globalOptions = parseCommandLine();

function setupProcessState(root, globalOptions) {
const hook = createHook({
__proto__: null,
init(asyncId, type, triggerAsyncId, resource) {
Expand Down Expand Up @@ -195,33 +222,8 @@ function setup(root) {
process.on('SIGTERM', terminationHandler);
}

root.harness = {
__proto__: null,
allowTestsToRun: false,
bootstrapPromise: resolvedPromise,
watching: false,
coverage: FunctionPrototypeBind(collectCoverage, null, root, coverage),
resetCounters() {
root.harness.counters = {
__proto__: null,
all: 0,
failed: 0,
passed: 0,
cancelled: 0,
skipped: 0,
todo: 0,
topLevel: 0,
suites: 0,
};
},
counters: null,
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
teardown: exitHandler,
snapshotManager: null,
};
root.harness.resetCounters();
root.startTime = hrtime();
return root;
root.harness.coverage = FunctionPrototypeBind(collectCoverage, null, root, coverage);
root.harness.teardown = exitHandler;
}

function lazyBootstrapRoot() {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class Test extends AsyncResource {
this.timeout = kDefaultTimeout;
this.entryFile = entryFile;
this.root = this;
this.harness = options.harness;
this.hooks = {
__proto__: null,
before: [],
Expand All @@ -416,6 +417,7 @@ class Test extends AsyncResource {
this.timeout = parent.timeout;
this.entryFile = parent.entryFile;
this.root = parent.root;
this.harness = null;
this.hooks = {
__proto__: null,
before: [],
Expand Down Expand Up @@ -480,7 +482,6 @@ class Test extends AsyncResource {
);

this.fn = fn;
this.harness = null; // Configured on the root test by the test harness.
this.mock = null;
this.plan = null;
this.expectedAssertions = plan;
Expand Down

0 comments on commit ecaeaf8

Please sign in to comment.