Skip to content

Commit eaec2b5

Browse files
JakobJingleheimerRafaelGSS
authored andcommitted
test: fix dangling promise in test_runner no isolation test setup
PR-URL: #57595 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Pietro Marchini <[email protected]>
1 parent c377394 commit eaec2b5

File tree

5 files changed

+93
-15
lines changed

5 files changed

+93
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const test = require('node:test');
2+
3+
test.before(() => console.log('before(): global'));
4+
test.beforeEach(() => console.log('beforeEach(): global'));
5+
test.after(() => console.log('after(): global'));
6+
test.afterEach(() => console.log('afterEach(): global'));

test/fixtures/test-runner/no-isolation/global-hooks.js

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from 'node:test';
2+
3+
test.before(() => console.log('before(): global'));
4+
test.beforeEach(() => console.log('beforeEach(): global'));
5+
test.after(() => console.log('after(): global'));
6+
test.afterEach(() => console.log('afterEach(): global'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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-isolation=none',
8+
];
9+
10+
const testFiles = [
11+
fixtures.path('test-runner', 'no-isolation', 'one.test.js'),
12+
fixtures.path('test-runner', 'no-isolation', 'two.test.js'),
13+
];
14+
15+
const order = [
16+
'before(): global',
17+
18+
'before one: <root>',
19+
'suite one',
20+
21+
'before two: <root>',
22+
'suite two',
23+
24+
'beforeEach(): global',
25+
'beforeEach one: suite one - test',
26+
'beforeEach two: suite one - test',
27+
28+
'suite one - test',
29+
'afterEach(): global',
30+
'afterEach one: suite one - test',
31+
'afterEach two: suite one - test',
32+
33+
'before suite two: suite two',
34+
'beforeEach(): global',
35+
'beforeEach one: suite two - test',
36+
'beforeEach two: suite two - test',
37+
38+
'suite two - test',
39+
'afterEach(): global',
40+
'afterEach one: suite two - test',
41+
'afterEach two: suite two - test',
42+
43+
'after(): global',
44+
'after one: <root>',
45+
'after two: <root>',
46+
].join('\n');
47+
48+
/**
49+
* TODO: The `--require` flag is processed in `loadPreloadModules` (process/pre_execution.js) BEFORE
50+
* the root test is created by the test runner. This causes a global `before` hook to register (and
51+
* run) but then the root test-case is created, causing the "subsequent" hooks to get lost. This
52+
* behaviour (CJS route only) is different from the ESM route, where test runner explicitly handles
53+
* `--import` in `root.runInAsyncScope` (test_runner/runner.js).
54+
* @see https://github.com/nodejs/node/pull/57595#issuecomment-2770724492
55+
* @see https://github.com/nodejs/node/issues/57728
56+
* Moved from test/parallel/test-runner-no-isolation-hooks.mjs
57+
*/
58+
test('use --require to define global hooks', async (t) => {
59+
const { stdout } = await common.spawnPromisified(process.execPath, [
60+
...testArguments,
61+
'--require', fixtures.path('test-runner', 'no-isolation', 'global-hooks.cjs'),
62+
...testFiles,
63+
]);
64+
65+
const testHookOutput = stdout.split('\n▶')[0];
66+
67+
t.assert.equal(testHookOutput, order);
68+
});

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,28 @@ const order = [
4343
'after(): global',
4444
'after one: <root>',
4545
'after two: <root>',
46-
];
46+
].join('\n');
4747

48-
test('Using --require to define global hooks works', async (t) => {
49-
const spawned = await common.spawnPromisified(process.execPath, [
48+
test('use --import (CJS) to define global hooks', async (t) => {
49+
const { stdout } = await common.spawnPromisified(process.execPath, [
5050
...testArguments,
51-
'--require', fixtures.path('test-runner', 'no-isolation', 'global-hooks.js'),
51+
'--import', fixtures.fileURL('test-runner', 'no-isolation', 'global-hooks.cjs'),
5252
...testFiles,
5353
]);
5454

55-
t.assert.ok(spawned.stdout.includes(order.join('\n')));
55+
const testHookOutput = stdout.split('\n▶')[0];
56+
57+
t.assert.equal(testHookOutput, order);
5658
});
5759

58-
test('Using --import to define global hooks works', async (t) => {
59-
const spawned = await common.spawnPromisified(process.execPath, [
60+
test('use --import (ESM) to define global hooks', async (t) => {
61+
const { stdout } = await common.spawnPromisified(process.execPath, [
6062
...testArguments,
61-
'--import', fixtures.fileURL('test-runner', 'no-isolation', 'global-hooks.js'),
63+
'--import', fixtures.fileURL('test-runner', 'no-isolation', 'global-hooks.mjs'),
6264
...testFiles,
6365
]);
6466

65-
t.assert.ok(spawned.stdout.includes(order.join('\n')));
67+
const testHookOutput = stdout.split('\n▶')[0];
68+
69+
t.assert.equal(testHookOutput, order);
6670
});

0 commit comments

Comments
 (0)