Skip to content

Commit f9472b6

Browse files
committed
test_runner: unify --require and --import behavior when isolation none
1 parent 52d95f5 commit f9472b6

File tree

5 files changed

+40
-75
lines changed

5 files changed

+40
-75
lines changed

lib/internal/main/test_runner.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ const {
55
} = primordials;
66

77
const {
8-
prepareMainThreadExecution,
98
markBootstrapComplete,
9+
prepareTestRunnerMainExecution,
1010
} = require('internal/process/pre_execution');
1111
const { isUsingInspector } = require('internal/util/inspector');
1212
const { run } = require('internal/test_runner/runner');
1313
const { parseCommandLine } = require('internal/test_runner/utils');
14+
const { getOptionValue } = require('internal/options');
1415
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
1516
let debug = require('internal/util/debuglog').debuglog('test_runner', (fn) => {
1617
debug = fn;
1718
});
1819

19-
prepareMainThreadExecution(false);
20+
const isTestIsolationNone = getOptionValue('--test-isolation') === 'none';
21+
22+
// We set initializeModules to false as we want to load user modules in the test runner run function
23+
// if we are running with --test-isolation=none
24+
prepareTestRunnerMainExecution(!isTestIsolationNone);
2025
markBootstrapComplete();
2126

2227
const options = parseCommandLine();

lib/internal/process/pre_execution.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ function prepareMainThreadExecution(expandArgv1 = false, initializeModules = tru
5050
});
5151
}
5252

53+
function prepareTestRunnerMainExecution(loadUserModules = true) {
54+
return prepareExecution({
55+
expandArgv1: false,
56+
initializeModules: true,
57+
isMainThread: true,
58+
forceDefaultLoader: !loadUserModules,
59+
});
60+
}
61+
5362
function prepareWorkerThreadExecution() {
5463
prepareExecution({
5564
expandArgv1: false,
@@ -87,7 +96,7 @@ function prepareShadowRealmExecution() {
8796
}
8897

8998
function prepareExecution(options) {
90-
const { expandArgv1, initializeModules, isMainThread } = options;
99+
const { expandArgv1, initializeModules, isMainThread, forceDefaultLoader } = options;
91100

92101
refreshRuntimeOptions();
93102

@@ -147,7 +156,7 @@ function prepareExecution(options) {
147156
}
148157

149158
if (initializeModules) {
150-
setupUserModules();
159+
setupUserModules(forceDefaultLoader);
151160
}
152161

153162
return mainEntry;
@@ -712,6 +721,7 @@ module.exports = {
712721
prepareMainThreadExecution,
713722
prepareWorkerThreadExecution,
714723
prepareShadowRealmExecution,
724+
prepareTestRunnerMainExecution,
715725
markBootstrapComplete,
716726
loadPreloadModules,
717727
initializeFrozenIntrinsics,

lib/internal/test_runner/runner.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,15 @@ function run(options = kEmptyObject) {
765765
SafePromiseAllReturnVoid([root.harness.bootstrapPromise, promise]) :
766766
promise;
767767

768-
const userImports = getOptionValue('--import');
769-
for (let i = 0; i < userImports.length; i++) {
770-
await cascadedLoader.import(userImports[i], parentURL, kEmptyObject);
768+
// We need to setup the user modules in the test runner if we are running with
769+
// --test-isolation=none and --test in order to avoid loading the user modules
770+
// before the creation of the root test.
771+
const userModules = [
772+
...getOptionValue('--require'),
773+
...getOptionValue('--import'),
774+
];
775+
for (let i = 0; i < userModules.length; i++) {
776+
await cascadedLoader.import(userModules[i], parentURL, kEmptyObject);
771777
}
772778

773779
for (let i = 0; i < testFiles.length; ++i) {

test/known_issues/test-runner-no-isolation-hooks.mjs

Lines changed: 0 additions & 68 deletions
This file was deleted.

test/parallel/test-runner-no-isolation-hooks.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ test('use --import (ESM) to define global hooks', async (t) => {
6868

6969
t.assert.equal(testHookOutput, order);
7070
});
71+
72+
test('use --require to define global hooks', async (t) => {
73+
const { stdout } = await common.spawnPromisified(process.execPath, [
74+
...testArguments,
75+
'--require', fixtures.path('test-runner', 'no-isolation', 'global-hooks.cjs'),
76+
...testFiles,
77+
]);
78+
79+
const testHookOutput = stdout.split('\n▶')[0];
80+
81+
t.assert.equal(testHookOutput, order);
82+
});

0 commit comments

Comments
 (0)