Skip to content

Commit c9a86ea

Browse files
wrap initialize hook test-cases in describe (like the rest)
1 parent 4dfabcc commit c9a86ea

File tree

1 file changed

+136
-130
lines changed

1 file changed

+136
-130
lines changed

test/es-module/test-esm-loader-hooks.mjs

Lines changed: 136 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -438,149 +438,155 @@ describe('Loader hooks', { concurrency: true }, () => {
438438
assert.strictEqual(signal, null);
439439
});
440440

441-
it('should invoke `initialize` correctly', async () => {
442-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
443-
'--no-warnings',
444-
'--experimental-loader',
445-
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
446-
'--input-type=module',
447-
'--eval',
448-
'import os from "node:os";',
449-
]);
441+
describe('initialize hook', () => {
442+
it('should invoke `initialize` correctly', async () => {
443+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
444+
'--no-warnings',
445+
'--experimental-loader',
446+
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
447+
'--input-type=module',
448+
'--eval',
449+
'import os from "node:os";',
450+
]);
450451

451-
assert.strictEqual(stderr, '');
452-
assert.deepStrictEqual(stdout.split('\n'), ['hooks initialize 1', '']);
453-
assert.strictEqual(code, 0);
454-
assert.strictEqual(signal, null);
455-
});
452+
assert.strictEqual(stderr, '');
453+
assert.deepStrictEqual(stdout.split('\n'), ['hooks initialize 1', '']);
454+
assert.strictEqual(code, 0);
455+
assert.strictEqual(signal, null);
456+
});
456457

457-
it('should allow communicating with loader via `register` ports', async () => {
458-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
459-
'--no-warnings',
460-
'--input-type=module',
461-
'--eval',
462-
`
463-
import {MessageChannel} from 'node:worker_threads';
464-
import {register} from 'node:module';
465-
import {once} from 'node:events';
466-
const {port1, port2} = new MessageChannel();
467-
port1.on('message', (msg) => {
468-
console.log('message', msg);
469-
});
470-
const result = register(
471-
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize-port.mjs'))},
472-
{data: port2, transferList: [port2]},
473-
);
474-
console.log('register', result);
475-
476-
const timeout = setTimeout(() => {}, 2**31 - 1); // to keep the process alive.
477-
await Promise.all([
478-
once(port1, 'message').then(() => once(port1, 'message')),
479-
import('node:os'),
480-
]);
481-
clearTimeout(timeout);
482-
port1.close();
483-
`,
484-
]);
458+
it('should allow communicating with loader via `register` ports', async () => {
459+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
460+
'--no-warnings',
461+
'--input-type=module',
462+
'--eval',
463+
`
464+
import {MessageChannel} from 'node:worker_threads';
465+
import {register} from 'node:module';
466+
import {once} from 'node:events';
467+
const {port1, port2} = new MessageChannel();
468+
port1.on('message', (msg) => {
469+
console.log('message', msg);
470+
});
471+
const result = register(
472+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize-port.mjs'))},
473+
{data: port2, transferList: [port2]},
474+
);
475+
console.log('register', result);
476+
477+
const timeout = setTimeout(() => {}, 2**31 - 1); // to keep the process alive.
478+
await Promise.all([
479+
once(port1, 'message').then(() => once(port1, 'message')),
480+
import('node:os'),
481+
]);
482+
clearTimeout(timeout);
483+
port1.close();
484+
`,
485+
]);
485486

486-
assert.strictEqual(stderr, '');
487-
assert.deepStrictEqual(stdout.split('\n'), [ 'register ok',
488-
'message initialize',
489-
'message resolve node:os',
490-
'' ]);
487+
assert.strictEqual(stderr, '');
488+
assert.deepStrictEqual(stdout.split('\n'), [
489+
'register ok',
490+
'message initialize',
491+
'message resolve node:os',
492+
'',
493+
]);
491494

492-
assert.strictEqual(code, 0);
493-
assert.strictEqual(signal, null);
494-
});
495+
assert.strictEqual(code, 0);
496+
assert.strictEqual(signal, null);
497+
});
495498

496-
it('should `register` from cjs', async () => {
497-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
498-
'--no-warnings',
499-
'--input-type=commonjs',
500-
'--eval',
501-
`
502-
'use strict';
503-
const {register} = require('node:module');
504-
register(
505-
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))},
506-
);
507-
register(
508-
${JSON.stringify(fixtures.fileURL('es-module-loaders/loader-load-foo-or-42.mjs'))},
509-
);
510-
511-
import('node:os').then((result) => {
512-
console.log(JSON.stringify(result));
513-
});
514-
`,
515-
]);
499+
it('should `register` from cjs', async () => {
500+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
501+
'--no-warnings',
502+
'--input-type=commonjs',
503+
'--eval',
504+
`
505+
'use strict';
506+
const {register} = require('node:module');
507+
register(
508+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))},
509+
);
510+
register(
511+
${JSON.stringify(fixtures.fileURL('es-module-loaders/loader-load-foo-or-42.mjs'))},
512+
);
513+
514+
import('node:os').then((result) => {
515+
console.log(JSON.stringify(result));
516+
});
517+
`,
518+
]);
516519

517-
assert.strictEqual(stderr, '');
518-
assert.deepStrictEqual(stdout.split('\n').sort(), ['hooks initialize 1', '{"default":"foo"}', ''].sort());
520+
assert.strictEqual(stderr, '');
521+
assert.deepStrictEqual(stdout.split('\n').sort(), ['hooks initialize 1', '{"default":"foo"}', ''].sort());
519522

520-
assert.strictEqual(code, 0);
521-
assert.strictEqual(signal, null);
522-
});
523+
assert.strictEqual(code, 0);
524+
assert.strictEqual(signal, null);
525+
});
523526

524-
it('`register` should work with `require`', async () => {
525-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
526-
'--no-warnings',
527-
'--require',
528-
fixtures.path('es-module-loaders/register-loader.cjs'),
529-
'--input-type=module',
530-
'--eval',
531-
'import "node:os";',
532-
]);
527+
it('`register` should work with `require`', async () => {
528+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
529+
'--no-warnings',
530+
'--require',
531+
fixtures.path('es-module-loaders/register-loader.cjs'),
532+
'--input-type=module',
533+
'--eval',
534+
'import "node:os";',
535+
]);
533536

534-
assert.strictEqual(stderr, '');
535-
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', 'resolve passthru', '']);
536-
assert.strictEqual(code, 0);
537-
assert.strictEqual(signal, null);
538-
});
537+
assert.strictEqual(stderr, '');
538+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', 'resolve passthru', '']);
539+
assert.strictEqual(code, 0);
540+
assert.strictEqual(signal, null);
541+
});
539542

540-
it('`register` should work with `import`', async () => {
541-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
542-
'--no-warnings',
543-
'--import',
544-
fixtures.fileURL('es-module-loaders/register-loader.mjs'),
545-
'--input-type=module',
546-
'--eval',
547-
`
548-
import 'node:os';
549-
`,
550-
]);
543+
it('`register` should work with `import`', async () => {
544+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
545+
'--no-warnings',
546+
'--import',
547+
fixtures.fileURL('es-module-loaders/register-loader.mjs'),
548+
'--input-type=module',
549+
'--eval',
550+
`
551+
import 'node:os';
552+
`,
553+
]);
551554

552-
assert.strictEqual(stderr, '');
553-
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', '']);
554-
assert.strictEqual(code, 0);
555-
assert.strictEqual(signal, null);
556-
});
555+
assert.strictEqual(stderr, '');
556+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', '']);
557+
assert.strictEqual(code, 0);
558+
assert.strictEqual(signal, null);
559+
});
557560

558-
it('should execute `initialize` in sequence', async () => {
559-
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
560-
'--no-warnings',
561-
'--input-type=module',
562-
'--eval',
563-
`
564-
import {register} from 'node:module';
565-
console.log('result', register(
566-
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
567-
));
568-
console.log('result', register(
569-
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
570-
));
571-
572-
await import('node:os');
573-
`,
574-
]);
561+
it('should execute `initialize` in sequence', async () => {
562+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
563+
'--no-warnings',
564+
'--input-type=module',
565+
'--eval',
566+
`
567+
import {register} from 'node:module';
568+
console.log('result', register(
569+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
570+
));
571+
console.log('result', register(
572+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
573+
));
574+
575+
await import('node:os');
576+
`,
577+
]);
575578

576-
assert.strictEqual(stderr, '');
577-
assert.deepStrictEqual(stdout.split('\n'), [ 'hooks initialize 1',
578-
'result 1',
579-
'hooks initialize 2',
580-
'result 2',
581-
'' ]);
582-
assert.strictEqual(code, 0);
583-
assert.strictEqual(signal, null);
579+
assert.strictEqual(stderr, '');
580+
assert.deepStrictEqual(stdout.split('\n'), [
581+
'hooks initialize 1',
582+
'result 1',
583+
'hooks initialize 2',
584+
'result 2',
585+
'',
586+
]);
587+
assert.strictEqual(code, 0);
588+
assert.strictEqual(signal, null);
589+
});
584590
});
585591

586592
it('should use CJS loader to respond to require.resolve calls by default', async () => {

0 commit comments

Comments
 (0)