diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index 445ceedd968aa2..2ea0128596e25b 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -599,7 +599,7 @@ describe('Loader hooks', { concurrency: true }, () => { ` import {MessageChannel} from 'node:worker_threads'; import {register} from 'node:module'; - import {setTimeout} from 'node:timers/promises'; + import {once} from 'node:events'; const {port1, port2} = new MessageChannel(); port1.on('message', (msg) => { console.log('message', msg); @@ -610,8 +610,12 @@ describe('Loader hooks', { concurrency: true }, () => { ); console.log('register', result); - await import('node:os'); - await setTimeout(99); // delay to limit flakiness + const timeout = setTimeout(() => {}, 2**31 - 1); // to keep the process alive. + await Promise.all([ + once(port1, 'message').then(() => once(port1, 'message')), + import('node:os'), + ]); + clearTimeout(timeout); port1.close(); `, ]); @@ -707,10 +711,10 @@ describe('Loader hooks', { concurrency: true }, () => { ]); assert.strictEqual(stderr, ''); - assert.deepStrictEqual(stdout.split('\n'), [ 'result 1', - 'result 2', - 'hooks initialize 1', + assert.deepStrictEqual(stdout.split('\n'), [ 'hooks initialize 1', + 'result 1', 'hooks initialize 2', + 'result 2', '' ]); assert.strictEqual(code, 0); assert.strictEqual(signal, null); diff --git a/test/fixtures/es-module-loaders/hooks-initialize.mjs b/test/fixtures/es-module-loaders/hooks-initialize.mjs index 646be145503134..ab6f2c50d146e3 100644 --- a/test/fixtures/es-module-loaders/hooks-initialize.mjs +++ b/test/fixtures/es-module-loaders/hooks-initialize.mjs @@ -1,7 +1,8 @@ +import { writeFileSync } from 'node:fs'; + let counter = 0; export async function initialize() { - counter += 1; - console.log('hooks initialize', counter); + writeFileSync(1, `hooks initialize ${++counter}\n`); return counter; }