Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
cjihrig committed Aug 13, 2024
commit 488e5bcf3cf9fcbaab783e5076c2a0b8a9f9172d
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