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 <moshe@atlow.co.il> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
1 parent c377394 commit eaec2b5

File tree

5 files changed

+93
-15
lines changed

5 files changed

+93
-15
lines changed
Lines changed: 6 additions & 0 deletions
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

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
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'));
Lines changed: 68 additions & 0 deletions
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

Lines changed: 13 additions & 9 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy