Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor test-esm-loader-hooks for easier debugging #49131

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -564,19 +567,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 @@ -607,7 +605,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 @@ -645,18 +646,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 @@ -665,20 +662,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 @@ -691,24 +684,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