Skip to content

test: fix dangling promise in test_runner no isolation test setup #57595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/fixtures/test-runner/no-isolation/global-hooks.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const test = require('node:test');

test.before(() => console.log('before(): global'));
test.beforeEach(() => console.log('beforeEach(): global'));
test.after(() => console.log('after(): global'));
test.afterEach(() => console.log('afterEach(): global'));
6 changes: 0 additions & 6 deletions test/fixtures/test-runner/no-isolation/global-hooks.js

This file was deleted.

6 changes: 6 additions & 0 deletions test/fixtures/test-runner/no-isolation/global-hooks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'node:test';

test.before(() => console.log('before(): global'));
test.beforeEach(() => console.log('beforeEach(): global'));
test.after(() => console.log('after(): global'));
test.afterEach(() => console.log('afterEach(): global'));
68 changes: 68 additions & 0 deletions test/known_issues/test-runner-no-isolation-hooks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { test } from 'node:test';

const testArguments = [
'--test',
'--test-isolation=none',
];

const testFiles = [
fixtures.path('test-runner', 'no-isolation', 'one.test.js'),
fixtures.path('test-runner', 'no-isolation', 'two.test.js'),
];

const order = [
'before(): global',

'before one: <root>',
'suite one',

'before two: <root>',
'suite two',

'beforeEach(): global',
'beforeEach one: suite one - test',
'beforeEach two: suite one - test',

'suite one - test',
'afterEach(): global',
'afterEach one: suite one - test',
'afterEach two: suite one - test',

'before suite two: suite two',
'beforeEach(): global',
'beforeEach one: suite two - test',
'beforeEach two: suite two - test',

'suite two - test',
'afterEach(): global',
'afterEach one: suite two - test',
'afterEach two: suite two - test',

'after(): global',
'after one: <root>',
'after two: <root>',
].join('\n');

/**
* TODO: The `--require` flag is processed in `loadPreloadModules` (process/pre_execution.js) BEFORE
* the root test is created by the test runner. This causes a global `before` hook to register (and
* run) but then the root test-case is created, causing the "subsequent" hooks to get lost. This
* behaviour (CJS route only) is different from the ESM route, where test runner explicitly handles
* `--import` in `root.runInAsyncScope` (test_runner/runner.js).
* @see https://github.com/nodejs/node/pull/57595#issuecomment-2770724492
* @see https://github.com/nodejs/node/issues/57728
* Moved from test/parallel/test-runner-no-isolation-hooks.mjs
*/
test('use --require to define global hooks', async (t) => {
const { stdout } = await common.spawnPromisified(process.execPath, [
...testArguments,
'--require', fixtures.path('test-runner', 'no-isolation', 'global-hooks.cjs'),
...testFiles,
]);

const testHookOutput = stdout.split('\n▶')[0];

t.assert.equal(testHookOutput, order);
});
22 changes: 13 additions & 9 deletions test/parallel/test-runner-no-isolation-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,28 @@ const order = [
'after(): global',
'after one: <root>',
'after two: <root>',
];
].join('\n');

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

t.assert.ok(spawned.stdout.includes(order.join('\n')));
const testHookOutput = stdout.split('\n▶')[0];

t.assert.equal(testHookOutput, order);
});

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

t.assert.ok(spawned.stdout.includes(order.join('\n')));
const testHookOutput = stdout.split('\n▶')[0];

t.assert.equal(testHookOutput, order);
});
Loading