Skip to content

Commit 1d17c10

Browse files
aduh95RafaelGSS
authored andcommitted
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. PR-URL: #49131 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 21dc844 commit 1d17c10

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'),
@@ -576,19 +579,14 @@ describe('Loader hooks', { concurrency: true }, () => {
576579
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
577580
'--no-warnings',
578581
'--experimental-loader',
579-
fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'),
582+
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
580583
'--input-type=module',
581584
'--eval',
582585
'import os from "node:os";',
583586
]);
584587

585-
const lines = stdout.trim().split('\n');
586-
587-
assert.strictEqual(lines.length, 1);
588-
assert.strictEqual(lines[0], 'hooks initialize 1');
589-
590588
assert.strictEqual(stderr, '');
591-
589+
assert.deepStrictEqual(stdout.split('\n'), ['hooks initialize 1', '']);
592590
assert.strictEqual(code, 0);
593591
assert.strictEqual(signal, null);
594592
});
@@ -619,7 +617,10 @@ describe('Loader hooks', { concurrency: true }, () => {
619617
]);
620618

621619
assert.strictEqual(stderr, '');
622-
assert.deepStrictEqual(stdout.split('\n'), ['register ok', 'message initialize', 'message resolve node:os', '']);
620+
assert.deepStrictEqual(stdout.split('\n'), [ 'register ok',
621+
'message initialize',
622+
'message resolve node:os',
623+
'' ]);
623624

624625
assert.strictEqual(code, 0);
625626
assert.strictEqual(signal, null);
@@ -657,18 +658,14 @@ describe('Loader hooks', { concurrency: true }, () => {
657658
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
658659
'--no-warnings',
659660
'--require',
660-
fixtures.path('/es-module-loaders/register-loader.cjs'),
661+
fixtures.path('es-module-loaders/register-loader.cjs'),
661662
'--input-type=module',
662663
'--eval',
663664
'import "node:os";',
664665
]);
665666

666-
const lines = stdout.split('\n');
667-
668-
assert.strictEqual(lines[0], 'resolve passthru');
669-
670667
assert.strictEqual(stderr, '');
671-
668+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', 'resolve passthru', '']);
672669
assert.strictEqual(code, 0);
673670
assert.strictEqual(signal, null);
674671
});
@@ -677,20 +674,16 @@ describe('Loader hooks', { concurrency: true }, () => {
677674
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
678675
'--no-warnings',
679676
'--import',
680-
fixtures.fileURL('/es-module-loaders/register-loader.mjs'),
677+
fixtures.fileURL('es-module-loaders/register-loader.mjs'),
681678
'--input-type=module',
682679
'--eval',
683680
`
684681
import 'node:os';
685682
`,
686683
]);
687684

688-
const lines = stdout.split('\n');
689-
690-
assert.strictEqual(lines[0], 'resolve passthru');
691-
692685
assert.strictEqual(stderr, '');
693-
686+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', '']);
694687
assert.strictEqual(code, 0);
695688
assert.strictEqual(signal, null);
696689
});
@@ -703,24 +696,22 @@ describe('Loader hooks', { concurrency: true }, () => {
703696
`
704697
import {register} from 'node:module';
705698
console.log('result', register(
706-
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
699+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
707700
));
708701
console.log('result', register(
709-
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
702+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
710703
));
711704
712705
await import('node:os');
713706
`,
714707
]);
715708

716-
const lines = stdout.split('\n');
717-
718-
assert.strictEqual(lines[0], 'result 1');
719-
assert.strictEqual(lines[1], 'result 2');
720-
assert.strictEqual(lines[2], 'hooks initialize 1');
721-
assert.strictEqual(lines[3], 'hooks initialize 2');
722-
723709
assert.strictEqual(stderr, '');
710+
assert.deepStrictEqual(stdout.split('\n'), [ 'result 1',
711+
'result 2',
712+
'hooks initialize 1',
713+
'hooks initialize 2',
714+
'' ]);
724715
assert.strictEqual(code, 0);
725716
assert.strictEqual(signal, null);
726717
});

0 commit comments

Comments
 (0)