Skip to content
Open
Show file tree
Hide file tree
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
860 changes: 0 additions & 860 deletions test/es-module/test-esm-loader-hooks.mjs

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/es-module-loaders/loader-delayed-async-load.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function load(a, b, c) {
return new Promise(d => setTimeout(() => d(c(a, b)), 99));
}
4 changes: 4 additions & 0 deletions test/fixtures/es-module-loaders/loader-exit-on-load.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function load(a, b, next) {
if (a === 'data:exit') process.exit(42);
return next(a, b);
}
4 changes: 4 additions & 0 deletions test/fixtures/es-module-loaders/loader-exit-on-resolve.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function resolve(a, b, next) {
if (a === 'exit:') process.exit(42);
return next(a, b);
}
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-exit-top-level.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.exit(42);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export function globalPreload() {}
export function initialize() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function globalPreload() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function globalPreload() {
return '';
}
3 changes: 3 additions & 0 deletions test/fixtures/es-module-loaders/loader-initialize-exit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function initialize() {
process.exit(42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function initialize() {
return new Promise(() => {});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function initialize() {
return Promise.reject();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function initialize() {
throw null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

export async function load(url, context, nextLoad) {
const result = await nextLoad(url, context);
if (url.endsWith('/common/index.js')) {
result.source = '"use strict";module.exports=require("node:module").createRequire(' +
JSON.stringify(url) + ')(' + JSON.stringify(fileURLToPath(url)) + ');\n';
} else if (url.startsWith('file:') && (context.format == null || context.format === 'commonjs')) {
result.source = await readFile(new URL(url));
}
return result;
}
9 changes: 9 additions & 0 deletions test/fixtures/es-module-loaders/loader-load-source-maps.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { readFile } from 'node:fs/promises';

export async function load(url, context, nextLoad) {
const resolved = await nextLoad(url, context);
if (context.format === 'commonjs') {
resolved.source = await readFile(new URL(url));
}
return resolved;
}
28 changes: 28 additions & 0 deletions test/fixtures/es-module-loaders/loader-mixed-opt-in.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createRequire } from 'module';

const require = createRequire(import.meta.url);
const fixtures = require('../../common/fixtures.js');

const fixturesPath = fixtures.path('empty.js');

export function resolve(s, c, n) {
if (s.endsWith('entry-point')) {
return {
shortCircuit: true,
url: 'file:///c:/virtual-entry-point',
format: 'commonjs',
};
}
return n(s, c);
}

export async function load(u, c, n) {
if (u === 'file:///c:/virtual-entry-point') {
return {
shortCircuit: true,
source: `"use strict";require(${JSON.stringify(fixturesPath)});console.log("Hello");`,
format: 'commonjs',
};
}
return n(u, c);
}
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-bigint.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw 1n;
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-boolean.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw true;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw {};
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-error.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('error message');
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-function.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw function fnName() {};
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-null.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw null;
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-number.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw 1;
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-object.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw { fn() {}, symbol: Symbol('symbol'), u: undefined };
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-string.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw 'literal string';
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-symbol.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw Symbol('symbol descriptor');
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-throw-undefined.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw undefined;
7 changes: 7 additions & 0 deletions test/fixtures/es-module-loaders/loader-worker-spawn.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Worker } from 'worker_threads';
import { createRequire } from 'module';

const require = createRequire(import.meta.url);
const fixtures = require('../../common/fixtures.js');

new Worker(fixtures.path('empty.js')).unref();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { register } from 'node:module';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';

const __dirname = dirname(fileURLToPath(import.meta.url));
register(join(__dirname, 'hooks-initialize.mjs'), import.meta.url);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {register} from 'node:module';
import fixtures from '../../common/fixtures.js';

register(
fixtures.fileURL('es-module-loaders/loader-load-foo-or-42.mjs'),
new URL('data:'),
);

import('node:os').then((result) => {
console.log(JSON.stringify(result));
});
11 changes: 11 additions & 0 deletions test/fixtures/module-hooks/register-loader-in-sequence.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {register} from 'node:module';
import fixtures from '../../common/fixtures.js';

console.log('result 1', register(
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs')
));
console.log('result 2', register(
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs')
));

await import('node:os');
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {register} from 'node:module';
import fixtures from '../../common/fixtures.js';

try {
register(fixtures.fileURL('es-module-loaders/loader-initialize-never-settling.mjs'));
} catch (e) {
console.log('caught', e.code);
}
14 changes: 14 additions & 0 deletions test/fixtures/module-hooks/register-loader-with-cjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const {register} = require('node:module');
const fixtures = require('../../common/fixtures.js');

register(
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
);
register(
fixtures.fileURL('es-module-loaders/loader-load-foo-or-42.mjs'),
);

import('node:os').then((result) => {
console.log(JSON.stringify(result));
});
22 changes: 22 additions & 0 deletions test/fixtures/module-hooks/register-loader-with-ports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {MessageChannel} from 'node:worker_threads';
import {register} from 'node:module';
import {once} from 'node:events';
import fixtures from '../../common/fixtures.js';

const {port1, port2} = new MessageChannel();
port1.on('message', (msg) => {
console.log('message', msg);
});
const result = register(
fixtures.fileURL('es-module-loaders/hooks-initialize-port.mjs'),
{data: port2, transferList: [port2]},
);
console.log('register', result);

const timeout = setTimeout(() => {}, 2**31 - 1); // to keep the process alive.
await Promise.all([
once(port1, 'message').then(() => once(port1, 'message')),
import('node:os'),
]);
clearTimeout(timeout);
port1.close();
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Test that loader hooks are called with all expected arguments
import '../common/index.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndAssert } from '../common/child_process.js';

spawnSyncAndAssert(
execPath,
[
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/hooks-input.mjs'),
fixtures.path('es-modules/json-modules.mjs'),
],
{
stdout(output) {
const lines = output.split('\n');
assert.match(lines[0], /{"url":"file:\/\/\/.*\/json-modules\.mjs","format":"test","shortCircuit":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.length, 4);
},
stderr: '',
trim: true,
},
);
31 changes: 31 additions & 0 deletions test/module-hooks/test-async-loader-hooks-called-with-register.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Test that loader hooks are called with all expected arguments using register function
import '../common/index.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndAssert } from '../common/child_process.js';

spawnSyncAndAssert(
execPath,
[
'--no-warnings',
'--experimental-loader=data:text/javascript,',
'--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'))});`,
],
{
stdout(output) {
const lines = output.split('\n');
assert.match(lines[0], /{"url":"file:\/\/\/.*\/json-modules\.mjs","format":"test","shortCircuit":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.length, 4);
},
stderr: '',
trim: true,
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Test that `globalPreload` should not emit warning when `initialize` is supplied

import '../common/index.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndAssert } from '../common/child_process.js';

spawnSyncAndAssert(
execPath,
[
'--experimental-loader',
fixtures.fileURL('es-module-loaders/loader-globalpreload-and-initialize.mjs'),
fixtures.path('empty.js'),
],
{
stderr(output) {
assert.doesNotMatch(output, /`globalPreload` has been removed; use `initialize` instead/);
},
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Test that `globalPreload` should emit warning
import '../common/index.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndAssert } from '../common/child_process.js';

spawnSyncAndAssert(
execPath,
[
'--experimental-loader',
fixtures.fileURL('es-module-loaders/loader-globalpreload-only.mjs'),
'--experimental-loader',
fixtures.fileURL('es-module-loaders/loader-globalpreload-with-return.mjs'),
fixtures.path('empty.js'),
],
{
stderr(output) {
const matches = output.match(/`globalPreload` has been removed; use `initialize` instead/g);
assert.strictEqual(matches.length, 1);
},
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Test that `initialize` should execute in sequence
import '../common/index.mjs';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndAssert } from '../common/child_process.js';

spawnSyncAndAssert(
execPath,
[
'--no-warnings',
fixtures.path('module-hooks/register-loader-in-sequence.mjs'),
],
{
stdout: 'hooks initialize 1\n' +
'result 1 undefined\n' +
'hooks initialize 2\n' +
'result 2 undefined',
trim: true,
stderr: '',
},
);
22 changes: 22 additions & 0 deletions test/module-hooks/test-async-loader-hooks-initialize-invoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Test that `initialize` should be invoked correctly
import '../common/index.mjs';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndAssert } from '../common/child_process.js';

spawnSyncAndAssert(
execPath,
[
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'),
'--input-type=module',
'--eval',
'import os from "node:os";',
],
{
stdout: 'hooks initialize 1',
stderr: '',
trim: true,
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Test that `initialize` returning never-settling promise should be handled
import '../common/index.mjs';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndAssert } from '../common/child_process.js';

spawnSyncAndAssert(
execPath,
[
'--no-warnings',
fixtures.path('module-hooks/register-loader-initialize-never-settling.mjs'),
],
{
stdout: 'caught ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED',
stderr: '',
trim: true,
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Test that it should be fine to call `process.exit` from a `initialize` hook
import '../common/index.mjs';
import { execPath } from 'node:process';
import fixtures from '../common/fixtures.js';
import { spawnSyncAndExit } from '../common/child_process.js';

spawnSyncAndExit(
execPath,
[
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('es-module-loaders/loader-initialize-exit.mjs'),
'--input-type=module',
'--eval',
'import "node:os"',
],
{
status: 42,
signal: null,
stdout: '',
stderr: '',
},
);
Loading
Loading