Skip to content

Commit 1c020cc

Browse files
aduh95targos
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: nodejs#49131 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 586bb8f commit 1c020cc

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

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

+25-35
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 }, () => {
@@ -340,7 +344,6 @@ describe('Loader hooks', { concurrency: true }, () => {
340344

341345
it('should handle symbol', async () => {
342346
const { code, signal, stdout } = await spawnPromisified(execPath, [
343-
'--no-warnings',
344347
'--experimental-loader',
345348
'data:text/javascript,throw Symbol("symbol descriptor")',
346349
fixtures.path('empty.js'),
@@ -524,19 +527,14 @@ describe('Loader hooks', { concurrency: true }, () => {
524527
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
525528
'--no-warnings',
526529
'--experimental-loader',
527-
fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'),
530+
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
528531
'--input-type=module',
529532
'--eval',
530533
'import os from "node:os";',
531534
]);
532535

533-
const lines = stdout.trim().split('\n');
534-
535-
assert.strictEqual(lines.length, 1);
536-
assert.strictEqual(lines[0], 'hooks initialize 1');
537-
538536
assert.strictEqual(stderr, '');
539-
537+
assert.deepStrictEqual(stdout.split('\n'), ['hooks initialize 1', '']);
540538
assert.strictEqual(code, 0);
541539
assert.strictEqual(signal, null);
542540
});
@@ -567,7 +565,10 @@ describe('Loader hooks', { concurrency: true }, () => {
567565
]);
568566

569567
assert.strictEqual(stderr, '');
570-
assert.deepStrictEqual(stdout.split('\n'), ['register ok', 'message initialize', 'message resolve node:os', '']);
568+
assert.deepStrictEqual(stdout.split('\n'), [ 'register ok',
569+
'message initialize',
570+
'message resolve node:os',
571+
'' ]);
571572

572573
assert.strictEqual(code, 0);
573574
assert.strictEqual(signal, null);
@@ -605,18 +606,14 @@ describe('Loader hooks', { concurrency: true }, () => {
605606
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
606607
'--no-warnings',
607608
'--require',
608-
fixtures.path('/es-module-loaders/register-loader.cjs'),
609+
fixtures.path('es-module-loaders/register-loader.cjs'),
609610
'--input-type=module',
610611
'--eval',
611612
'import "node:os";',
612613
]);
613614

614-
const lines = stdout.split('\n');
615-
616-
assert.strictEqual(lines[0], 'resolve passthru');
617-
618615
assert.strictEqual(stderr, '');
619-
616+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', 'resolve passthru', '']);
620617
assert.strictEqual(code, 0);
621618
assert.strictEqual(signal, null);
622619
});
@@ -625,20 +622,16 @@ describe('Loader hooks', { concurrency: true }, () => {
625622
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
626623
'--no-warnings',
627624
'--import',
628-
fixtures.fileURL('/es-module-loaders/register-loader.mjs'),
625+
fixtures.fileURL('es-module-loaders/register-loader.mjs'),
629626
'--input-type=module',
630627
'--eval',
631628
`
632629
import 'node:os';
633630
`,
634631
]);
635632

636-
const lines = stdout.split('\n');
637-
638-
assert.strictEqual(lines[0], 'resolve passthru');
639-
640633
assert.strictEqual(stderr, '');
641-
634+
assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', '']);
642635
assert.strictEqual(code, 0);
643636
assert.strictEqual(signal, null);
644637
});
@@ -651,25 +644,22 @@ describe('Loader hooks', { concurrency: true }, () => {
651644
`
652645
import {register} from 'node:module';
653646
console.log('result', register(
654-
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
647+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
655648
));
656649
console.log('result', register(
657-
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
650+
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
658651
));
659652
660653
await import('node:os');
661654
`,
662655
]);
663656

664-
const lines = stdout.split('\n');
665-
666-
assert.strictEqual(lines[0], 'result 1');
667-
assert.strictEqual(lines[1], 'result 2');
668-
assert.strictEqual(lines[2], 'hooks initialize 1');
669-
assert.strictEqual(lines[3], 'hooks initialize 2');
670-
671657
assert.strictEqual(stderr, '');
672-
658+
assert.deepStrictEqual(stdout.split('\n'), [ 'result 1',
659+
'result 2',
660+
'hooks initialize 1',
661+
'hooks initialize 2',
662+
'' ]);
673663
assert.strictEqual(code, 0);
674664
assert.strictEqual(signal, null);
675665
});

0 commit comments

Comments
 (0)