Skip to content

Commit

Permalink
test_runner: add config to harness
Browse files Browse the repository at this point in the history
this commit changes the way test configurations are handled.
config is now set initially on harness and never read from argv again
  • Loading branch information
atlowChemi committed Jul 29, 2024
1 parent c40c41c commit b0d4934
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
2 changes: 2 additions & 0 deletions lib/internal/main/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
runnerConcurrency,
shard,
watchMode,
...rest
} = parseCommandLine();

let concurrency = runnerConcurrency;
Expand All @@ -47,6 +48,7 @@ const options = {
timeout: perFileTimeout,
shard,
globPatterns: ArrayPrototypeSlice(process.argv, 1),
...rest,
};
debug('test runner configuration:', options);
run(options).on('test:fail', (data) => {
Expand Down
11 changes: 6 additions & 5 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const {
ArrayPrototypeForEach,
FunctionPrototypeBind,
ObjectAssign,
PromiseResolve,
SafeMap,
} = primordials;
Expand Down Expand Up @@ -34,8 +35,8 @@ let globalRoot;

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

function createTestTree(options = kEmptyObject) {
globalRoot = setup(new Test({ __proto__: null, ...options, name: '<root>' }));
function createTestTree(options = kEmptyObject, config) {
globalRoot = setup(new Test({ __proto__: null, ...options, name: '<root>' }, config ?? parseCommandLine()));
return globalRoot;
}

Expand Down Expand Up @@ -134,7 +135,7 @@ function setup(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();
const globalOptions = root.harness.config;

const hook = createHook({
__proto__: null,
Expand Down Expand Up @@ -195,7 +196,7 @@ function setup(root) {
process.on('SIGTERM', terminationHandler);
}

root.harness = {
ObjectAssign(root.harness, {
__proto__: null,
allowTestsToRun: false,
bootstrapPromise: resolvedPromise,
Expand All @@ -218,7 +219,7 @@ function setup(root) {
shouldColorizeTestFiles: shouldColorizeTestFiles(globalOptions.destinations),
teardown: exitHandler,
snapshotManager: null,
};
});
root.harness.resetCounters();
root.startTime = hrtime();
return root;
Expand Down
21 changes: 19 additions & 2 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,25 @@ function run(options = kEmptyObject) {
throw new ERR_INVALID_ARG_TYPE(name, ['string', 'RegExp'], value);
});
}

const root = createTestTree({ __proto__: null, concurrency, timeout, signal });
const root = createTestTree(
{ __proto__: null, concurrency, timeout, signal },
{
__proto__: null,
forceExit,
perFileTimeout: timeout || Infinity,
runnerConcurrency: concurrency,
shard,
sourceMaps: options.sourceMaps,
testOnlyFlag: options.isTestRunner ? false : only,
testNamePatterns,
testSkipPatterns,
updateSnapshots: options.updateSnapshots,
watchMode: watch,
isTestRunner: options.isTestRunner || false,
globPatterns,
destinations: options.destinations || [],
},
);

if (process.env.NODE_TEST_CONTEXT !== undefined) {
process.emitWarning('node:test run() is being called recursively within a test file. skipping running files.');
Expand Down
28 changes: 14 additions & 14 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const {
createDeferredCallback,
countCompletedTest,
isTestFailureError,
parseCommandLine,
} = require('internal/test_runner/utils');
const {
createDeferredPromise,
Expand Down Expand Up @@ -79,14 +78,6 @@ const kHookNames = ObjectSeal(['before', 'after', 'beforeEach', 'afterEach']);
const kUnwrapErrors = new SafeSet()
.add(kTestCodeFailure).add(kHookFailure)
.add('uncaughtException').add('unhandledRejection');
const {
forceExit,
sourceMaps,
testNamePatterns,
testSkipPatterns,
testOnlyFlag,
updateSnapshots,
} = parseCommandLine();
let kResistStopPropagation;
let assertObj;
let findSourceMap;
Expand Down Expand Up @@ -132,7 +123,7 @@ function lazyAssertObject(harness) {
const { getOptionValue } = require('internal/options');
if (getOptionValue('--experimental-test-snapshots')) {
const { SnapshotManager } = require('internal/test_runner/snapshot');
harness.snapshotManager = new SnapshotManager(updateSnapshots);
harness.snapshotManager = new SnapshotManager(harness.config.updateSnapshots);
assertObj.set('snapshot', harness.snapshotManager.createAssert());
}
}
Expand Down Expand Up @@ -360,7 +351,7 @@ class Test extends AsyncResource {
outerSignal;
#reportedSubtest;

constructor(options) {
constructor(options, config) {
super('Test');

let { fn, name, parent } = options;
Expand Down Expand Up @@ -388,7 +379,7 @@ class Test extends AsyncResource {
if (parent === null) {
this.concurrency = 1;
this.nesting = 0;
this.only = testOnlyFlag;
this.only = config?.testOnlyFlag;
this.reporter = new TestsStream();
this.runOnlySubtests = this.only;
this.childNumber = 0;
Expand Down Expand Up @@ -430,6 +421,7 @@ class Test extends AsyncResource {
this.parent.filteredSubtestCount++;
}

const { testOnlyFlag } = config ?? this.root.harness.config;
if (testOnlyFlag && only === false) {
fn = noop;
}
Expand Down Expand Up @@ -501,6 +493,11 @@ class Test extends AsyncResource {
this.waitingOn = 0;
this.finished = false;

if (name === '<root>' && config != null) {
this.harness = { __proto__: null, config };
}

const { testOnlyFlag } = config ?? this.root.harness.config;
if (!testOnlyFlag && (only || this.runOnlySubtests)) {
const warning =
"'only' and 'runOnly' require the --test-only command-line option.";
Expand All @@ -517,7 +514,7 @@ class Test extends AsyncResource {
file: loc[2],
};

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

Expand All @@ -535,6 +532,7 @@ class Test extends AsyncResource {
}

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

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

const { testNamePatterns, testSkipPatterns, testOnlyFlag } = this.root?.harness?.config || kEmptyObject;
if (testNamePatterns !== null && testSkipPatterns !== null && !options.skip) {
this.fn = options.fn || this.fn;
this.skipped = false;
Expand Down Expand Up @@ -1175,6 +1174,7 @@ class Suite extends Test {
}

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

0 comments on commit b0d4934

Please sign in to comment.