Skip to content

Commit 36da793

Browse files
avivkellercjihrig
authored andcommitted
test_runner: allow --import with no isolation
Co-Authored-By: Colin Ihrig <[email protected]> PR-URL: #54697 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent e38e305 commit 36da793

File tree

5 files changed

+112
-14
lines changed

5 files changed

+112
-14
lines changed

lib/internal/test_runner/runner.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const { spawn } = require('child_process');
3434
const { finished } = require('internal/streams/end-of-stream');
3535
const { resolve } = require('path');
3636
const { DefaultDeserializer, DefaultSerializer } = require('v8');
37+
const { getOptionValue } = require('internal/options');
3738
const { Interface } = require('internal/readline/interface');
3839
const { deserializeError } = require('internal/error_serdes');
3940
const { Buffer } = require('buffer');
@@ -697,6 +698,11 @@ function run(options = kEmptyObject) {
697698

698699
root.harness.bootstrapPromise = promise;
699700

701+
const userImports = getOptionValue('--import');
702+
for (let i = 0; i < userImports.length; i++) {
703+
await cascadedLoader.import(userImports[i], parentURL, kEmptyObject);
704+
}
705+
700706
for (let i = 0; i < testFiles.length; ++i) {
701707
const testFile = testFiles[i];
702708
const fileURL = pathToFileURL(testFile);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import('node:test').then((test) => {
2+
test.before(() => console.log('before(): global'));
3+
test.beforeEach(() => console.log('beforeEach(): global'));
4+
test.after(() => console.log('after(): global'));
5+
test.afterEach(() => console.log('afterEach(): global'));
6+
});

test/fixtures/test-runner/no-isolation/one.test.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,35 @@ const { before, beforeEach, after, afterEach, test, suite } = require('node:test
33

44
globalThis.GLOBAL_ORDER = [];
55

6+
function record(data) {
7+
globalThis.GLOBAL_ORDER.push(data);
8+
console.log(data);
9+
}
10+
611
before(function() {
7-
GLOBAL_ORDER.push(`before one: ${this.name}`);
12+
record(`before one: ${this.name}`);
813
});
914

1015
beforeEach(function() {
11-
GLOBAL_ORDER.push(`beforeEach one: ${this.name}`);
16+
record(`beforeEach one: ${this.name}`);
1217
});
1318

1419
after(function() {
15-
GLOBAL_ORDER.push(`after one: ${this.name}`);
20+
record(`after one: ${this.name}`);
1621
});
1722

1823
afterEach(function() {
19-
GLOBAL_ORDER.push(`afterEach one: ${this.name}`);
24+
record(`afterEach one: ${this.name}`);
2025
});
2126

2227
suite('suite one', function() {
23-
GLOBAL_ORDER.push(this.name);
28+
record(this.name);
2429

2530
test('suite one - test', { only: true }, function() {
26-
GLOBAL_ORDER.push(this.name);
31+
record(this.name);
2732
});
2833
});
2934

3035
test('test one', function() {
31-
GLOBAL_ORDER.push(this.name);
36+
record(this.name);
3237
});
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
'use strict';
22
const { before, beforeEach, after, afterEach, test, suite } = require('node:test');
33

4+
function record(data) {
5+
globalThis.GLOBAL_ORDER.push(data);
6+
console.log(data);
7+
}
8+
49
before(function() {
5-
GLOBAL_ORDER.push(`before two: ${this.name}`);
10+
record(`before two: ${this.name}`);
611
});
712

813
beforeEach(function() {
9-
GLOBAL_ORDER.push(`beforeEach two: ${this.name}`);
14+
record(`beforeEach two: ${this.name}`);
1015
});
1116

1217
after(function() {
13-
GLOBAL_ORDER.push(`after two: ${this.name}`);
18+
record(`after two: ${this.name}`);
1419
});
1520

1621
afterEach(function() {
17-
GLOBAL_ORDER.push(`afterEach two: ${this.name}`);
22+
record(`afterEach two: ${this.name}`);
1823
});
1924

2025
suite('suite two', function() {
21-
GLOBAL_ORDER.push(this.name);
26+
record(this.name);
2227

2328
before(function() {
24-
GLOBAL_ORDER.push(`before suite two: ${this.name}`);
29+
record(`before suite two: ${this.name}`);
2530
});
2631

2732
test('suite two - test', { only: true }, function() {
28-
GLOBAL_ORDER.push(this.name);
33+
record(this.name);
2934
});
3035
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as common from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { test } from 'node:test';
4+
5+
const testArguments = [
6+
'--test',
7+
'--test-reporter=spec',
8+
'--experimental-test-isolation=none',
9+
];
10+
11+
const testFiles = [
12+
fixtures.path('test-runner', 'no-isolation', 'one.test.js'),
13+
fixtures.path('test-runner', 'no-isolation', 'two.test.js'),
14+
];
15+
16+
const order = [
17+
'before(): global',
18+
19+
'before one: <root>',
20+
'suite one',
21+
22+
'before two: <root>',
23+
'suite two',
24+
25+
'beforeEach(): global',
26+
'beforeEach one: suite one - test',
27+
'beforeEach two: suite one - test',
28+
29+
'suite one - test',
30+
'afterEach(): global',
31+
'afterEach one: suite one - test',
32+
'afterEach two: suite one - test',
33+
34+
'beforeEach(): global',
35+
'beforeEach one: test one',
36+
'beforeEach two: test one',
37+
'test one',
38+
39+
'afterEach(): global',
40+
'afterEach one: test one',
41+
'afterEach two: test one',
42+
43+
'before suite two: suite two',
44+
'beforeEach(): global',
45+
'beforeEach one: suite two - test',
46+
'beforeEach two: suite two - test',
47+
48+
'suite two - test',
49+
'afterEach(): global',
50+
'afterEach one: suite two - test',
51+
'afterEach two: suite two - test',
52+
53+
'after(): global',
54+
'after one: <root>',
55+
'after two: <root>',
56+
];
57+
58+
test('Using --require to define global hooks works', async (t) => {
59+
const spawned = await common.spawnPromisified(process.execPath, [
60+
...testArguments,
61+
'--require', fixtures.path('test-runner', 'no-isolation', 'global-hooks.js'),
62+
...testFiles,
63+
]);
64+
65+
t.assert.ok(spawned.stdout.includes(order.join('\n')));
66+
});
67+
68+
test('Using --import to define global hooks works', async (t) => {
69+
const spawned = await common.spawnPromisified(process.execPath, [
70+
...testArguments,
71+
'--import', fixtures.fileURL('test-runner', 'no-isolation', 'global-hooks.js'),
72+
...testFiles,
73+
]);
74+
75+
t.assert.ok(spawned.stdout.includes(order.join('\n')));
76+
});

0 commit comments

Comments
 (0)