Skip to content

Commit

Permalink
test: refactor test-esm-loader-hooks for easier debugging
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
aduh95 authored Aug 15, 2023
1 parent 9cc7327 commit 1aeb48c
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions test/es-module/test-esm-loader-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('Loader hooks', { concurrency: true }, () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-input.mjs'),
fixtures.path('/es-modules/json-modules.mjs'),
fixtures.fileURL('es-module-loaders/hooks-input.mjs'),
fixtures.path('es-modules/json-modules.mjs'),
]);

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

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

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

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

it('should handle symbol', async () => {
const { code, signal, stdout } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
'data:text/javascript,throw Symbol("symbol descriptor")',
fixtures.path('empty.js'),
Expand Down Expand Up @@ -576,19 +579,14 @@ describe('Loader hooks', { concurrency: true }, () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'),
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
'--input-type=module',
'--eval',
'import os from "node:os";',
]);

const lines = stdout.trim().split('\n');

assert.strictEqual(lines.length, 1);
assert.strictEqual(lines[0], 'hooks initialize 1');

assert.strictEqual(stderr, '');

assert.deepStrictEqual(stdout.split('\n'), ['hooks initialize 1', '']);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
Expand Down Expand Up @@ -619,7 +617,10 @@ describe('Loader hooks', { concurrency: true }, () => {
]);

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout.split('\n'), ['register ok', 'message initialize', 'message resolve node:os', '']);
assert.deepStrictEqual(stdout.split('\n'), [ 'register ok',
'message initialize',
'message resolve node:os',
'' ]);

assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
Expand Down Expand Up @@ -657,18 +658,14 @@ describe('Loader hooks', { concurrency: true }, () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--require',
fixtures.path('/es-module-loaders/register-loader.cjs'),
fixtures.path('es-module-loaders/register-loader.cjs'),
'--input-type=module',
'--eval',
'import "node:os";',
]);

const lines = stdout.split('\n');

assert.strictEqual(lines[0], 'resolve passthru');

assert.strictEqual(stderr, '');

assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', 'resolve passthru', '']);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
Expand All @@ -677,20 +674,16 @@ describe('Loader hooks', { concurrency: true }, () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--import',
fixtures.fileURL('/es-module-loaders/register-loader.mjs'),
fixtures.fileURL('es-module-loaders/register-loader.mjs'),
'--input-type=module',
'--eval',
`
import 'node:os';
`,
]);

const lines = stdout.split('\n');

assert.strictEqual(lines[0], 'resolve passthru');

assert.strictEqual(stderr, '');

assert.deepStrictEqual(stdout.split('\n'), ['resolve passthru', '']);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
Expand All @@ -703,24 +696,22 @@ describe('Loader hooks', { concurrency: true }, () => {
`
import {register} from 'node:module';
console.log('result', register(
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
));
console.log('result', register(
${JSON.stringify(fixtures.fileURL('/es-module-loaders/hooks-initialize.mjs'))}
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
));
await import('node:os');
`,
]);

const lines = stdout.split('\n');

assert.strictEqual(lines[0], 'result 1');
assert.strictEqual(lines[1], 'result 2');
assert.strictEqual(lines[2], 'hooks initialize 1');
assert.strictEqual(lines[3], 'hooks initialize 2');

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout.split('\n'), [ 'result 1',
'result 2',
'hooks initialize 1',
'hooks initialize 2',
'' ]);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
Expand Down

0 comments on commit 1aeb48c

Please sign in to comment.