Skip to content

Commit 3a42c8e

Browse files
committed
test: refactor test-esm-loader-hooks for easier debugging
- Always check stderr before stdout as the former would contain error information. - Always match the full stdout to avoid surprises. - Use `deepStrictEqual` when appropriate to get more informative test failures. - Remove leading slashes from relative paths/URLs to not confuse them with absolute paths. - Remove unnecessary `--no-warnings` flag.
1 parent 9fc5700 commit 3a42c8e

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

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

+25-34
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('Loader hooks', { concurrency: true }, () => {
1010
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
1111
'--no-warnings',
1212
'--experimental-loader',
13-
fixtures.fileURL('/es-module-loaders/hooks-input.mjs'),
14-
fixtures.path('/es-modules/json-modules.mjs'),
13+
fixtures.fileURL('es-module-loaders/hooks-input.mjs'),
14+
fixtures.path('es-modules/json-modules.mjs'),
1515
]);
1616

1717
assert.strictEqual(stderr, '');
@@ -23,6 +23,8 @@ describe('Loader hooks', { concurrency: true }, () => {
2323
assert.match(lines[1], /{"source":{"type":"Buffer","data":\[.*\]},"format":"module","shortCircuit":true}/);
2424
assert.match(lines[2], /{"url":"file:\/\/\/.*\/experimental\.json","format":"test","shortCircuit":true}/);
2525
assert.match(lines[3], /{"source":{"type":"Buffer","data":\[.*\]},"format":"json","shortCircuit":true}/);
26+
assert.strictEqual(lines[4], '');
27+
assert.strictEqual(lines.length, 5);
2628
});
2729

2830
it('are called with all expected arguments using register function', async () => {
@@ -32,8 +34,8 @@ describe('Loader hooks', { concurrency: true }, () => {
3234
'--input-type=module',
3335
'--eval',
3436
"import { register } from 'node:module';" +
35-
`register(${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-input.mjs'))});` +
36-
`await import(${JSON.stringify(fixtures.fileURL('/es-modules/json-modules.mjs'))});`,
37+
`register(${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-input.mjs'))});` +
38+
`await import(${JSON.stringify(fixtures.fileURL('es-modules/json-modules.mjs'))});`,
3739
]);
3840

3941
assert.strictEqual(stderr, '');
@@ -45,6 +47,8 @@ describe('Loader hooks', { concurrency: true }, () => {
4547
assert.match(lines[1], /{"source":{"type":"Buffer","data":\[.*\]},"format":"module","shortCircuit":true}/);
4648
assert.match(lines[2], /{"url":"file:\/\/\/.*\/experimental\.json","format":"test","shortCircuit":true}/);
4749
assert.match(lines[3], /{"source":{"type":"Buffer","data":\[.*\]},"format":"json","shortCircuit":true}/);
50+
assert.strictEqual(lines[4], '');
51+
assert.strictEqual(lines.length, 5);
4852
});
4953

5054
describe('should handle never-settling hooks in ESM files', { concurrency: true }, () => {
@@ -392,7 +396,6 @@ describe('Loader hooks', { concurrency: true }, () => {
392396

393397
it('should handle symbol', async () => {
394398
const { code, signal, stdout } = await spawnPromisified(execPath, [
395-
'--no-warnings',
396399
'--experimental-loader',
397400
'data:text/javascript,throw Symbol("symbol descriptor")',
398401
fixtures.path('empty.js'),
@@ -564,19 +567,14 @@ describe('Loader hooks', { concurrency: true }, () => {
564567
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
565568
'--no-warnings',
566569
'--experimental-loader',
567-
fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'),
570+
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
568571
'--input-type=module',
569572
'--eval',
570573
'import os from "node:os";',
571574
]);
572575

573-
const lines = stdout.trim().split('\n');
574-
575-
assert.strictEqual(lines.length, 1);
576-
assert.strictEqual(lines[0], 'hooks initialize 1');
577-
578576
assert.strictEqual(stderr, '');
579-
577+
assert.deepStrictEqual(stdout.split('\n'), ['hooks initialize 1', '']);
580578
assert.strictEqual(code, 0);
581579
assert.strictEqual(signal, null);
582580
});
@@ -607,7 +605,10 @@ describe('Loader hooks', { concurrency: true }, () => {
607605
]);
608606

609607
assert.strictEqual(stderr, '');
610-
assert.deepStrictEqual(stdout.split('\n'), ['register ok', 'message initialize', 'message resolve node:os', '']);
608+
assert.deepStrictEqual(stdout.split('\n'), [ 'register ok',
609+
'message initialize',
610+
'message resolve node:os',
611+
'' ]);
611612

612613
assert.strictEqual(code, 0);
613614
assert.strictEqual(signal, null);
@@ -645,18 +646,14 @@ describe('Loader hooks', { concurrency: true }, () => {
645646
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
646647
'--no-warnings',
647648
'--require',
648-
fixtures.path('/es-module-loaders/register-loader.cjs'),
649+
fixtures.path('es-module-loaders/register-loader.cjs'),
649650
'--input-type=module',
650651
'--eval',
651652
'import "node:os";',
652653
]);
653654

654-
const lines = stdout.split('\n');
655-
656-
assert.strictEqual(lines[0], 'resolve passthru');
657-
658655
assert.strictEqual(stderr, '');
659-
656+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', 'resolve passthru', '']);
660657
assert.strictEqual(code, 0);
661658
assert.strictEqual(signal, null);
662659
});
@@ -665,20 +662,16 @@ describe('Loader hooks', { concurrency: true }, () => {
665662
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
666663
'--no-warnings',
667664
'--import',
668-
fixtures.fileURL('/es-module-loaders/register-loader.mjs'),
665+
fixtures.fileURL('es-module-loaders/register-loader.mjs'),
669666
'--input-type=module',
670667
'--eval',
671668
`
672669
import 'node:os';
673670
`,
674671
]);
675672

676-
const lines = stdout.split('\n');
677-
678-
assert.strictEqual(lines[0], 'resolve passthru');
679-
680673
assert.strictEqual(stderr, '');
681-
674+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', '']);
682675
assert.strictEqual(code, 0);
683676
assert.strictEqual(signal, null);
684677
});
@@ -691,24 +684,22 @@ describe('Loader hooks', { concurrency: true }, () => {
691684
`
692685
import {register} from 'node:module';
693686
console.log('result', register(
694-
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
687+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
695688
));
696689
console.log('result', register(
697-
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
690+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
698691
));
699692
700693
await import('node:os');
701694
`,
702695
]);
703696

704-
const lines = stdout.split('\n');
705-
706-
assert.strictEqual(lines[0], 'result 1');
707-
assert.strictEqual(lines[1], 'result 2');
708-
assert.strictEqual(lines[2], 'hooks initialize 1');
709-
assert.strictEqual(lines[3], 'hooks initialize 2');
710-
711697
assert.strictEqual(stderr, '');
698+
assert.deepStrictEqual(stdout.split('\n'), [ 'result 1',
699+
'result 2',
700+
'hooks initialize 1',
701+
'hooks initialize 2',
702+
'' ]);
712703
assert.strictEqual(code, 0);
713704
assert.strictEqual(signal, null);
714705
});

0 commit comments

Comments
 (0)