Skip to content

Commit a04eeff

Browse files
GeoffreyBoothaduh95
authored andcommitted
test: use registerHooks() over module.register()
Port the tests that registered async module customization hooks, via `--loader`, `--experimental-loader` or `module.register()`, only as a means of exercising other features to use `module.registerHooks()` instead. This keeps the coverage for those features independent of the async module customization hooks, which are being removed. Tests that exist only to exercise the async module customization hooks themselves are left as they are. Assisted-by: Claude Signed-off-by: Geoffrey Booth <webadmin@geoffreybooth.com> PR-URL: #67074 Refs: #62395 Refs: #62401 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 2d7c6c1 commit a04eeff

18 files changed

Lines changed: 120 additions & 106 deletions

‎test/es-module/test-esm-assertionless-json-import.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/assertionless-json-import.mjs
1+
// Flags: --import ./test/fixtures/es-module-loaders/assertionless-json-import.mjs
22
'use strict';
33
const common = require('../common');
44
const assert = require('assert');

‎test/es-module/test-esm-detect-ambiguous.mjs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,27 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
147147
}
148148

149149
it('should not hint wrong format in resolve hook', async () => {
150-
// eslint-disable-next-line no-unassigned-vars
151-
let writeSync;
152150
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
153151
'--no-warnings',
154-
'--loader',
155-
`data:text/javascript,import { writeSync } from "node:fs"; export ${encodeURIComponent(
156-
async function resolve(s, c, next) {
157-
const result = await next(s, c);
158-
writeSync(1, result.format + '\n');
159-
return result;
160-
}
161-
)}`,
152+
'--import',
153+
'data:text/javascript,' + encodeURIComponent(`
154+
import { writeSync } from 'node:fs';
155+
import { registerHooks } from 'node:module';
156+
registerHooks({
157+
resolve(specifier, context, nextResolve) {
158+
const result = nextResolve(specifier, context);
159+
writeSync(1, result.format + '\\n');
160+
return result;
161+
},
162+
});
163+
`),
162164
fixtures.path('es-modules/package-without-type/noext-esm'),
163165
]);
164166

165167
assert.strictEqual(stderr, '');
166168
assert.strictEqual(stdout, 'null\nexecuted\n');
167169
assert.strictEqual(code, 0);
168170
assert.strictEqual(signal, null);
169-
170171
});
171172
});
172173

‎test/es-module/test-esm-import-meta-resolve.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
9494
const result = await spawnPromisified(execPath, [
9595
'--no-warnings',
9696
'--input-type=module',
97-
'--import', 'data:text/javascript,import{register}from"node:module";register("data:text/javascript,")',
97+
'--import', 'data:text/javascript,' + encodeURIComponent(
98+
'import { registerHooks } from "node:module";' +
99+
'registerHooks({ resolve: (s, c, next) => next(s, c) });',
100+
),
98101
'--eval',
99102
'console.log(import.meta.resolve(new URL("http://example.com")))',
100103
]);

‎test/es-module/test-esm-virtual-json.mjs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import '../common/index.mjs';
22
import * as fixtures from '../common/fixtures.mjs';
3-
import { register } from 'node:module';
3+
import { registerHooks } from 'node:module';
44
import assert from 'node:assert';
55

6-
async function resolve(referrer, context, next) {
7-
const result = await next(referrer, context);
6+
function resolve(referrer, context, next) {
7+
const result = next(referrer, context);
88
const url = new URL(result.url);
99
url.searchParams.set('randomSeed', Math.random());
1010
result.url = url.href;
@@ -22,7 +22,7 @@ function load(url, context, next) {
2222
return next(url, context);
2323
}
2424

25-
register(`data:text/javascript,export ${encodeURIComponent(resolve)};export ${encodeURIComponent(load)}`);
25+
registerHooks({ resolve, load });
2626

2727
assert.notDeepStrictEqual(
2828
await import(fixtures.fileURL('empty.json'), { with: { type: 'json' } }),
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
import { registerHooks } from 'node:module';
2+
13
const DATA_URL_PATTERN = /^data:application\/json(?:[^,]*?)(;base64)?,([\s\S]*)$/;
24
const JSON_URL_PATTERN = /^[^?]+\.json(\?[^#]*)?(#.*)?$/;
35

4-
export async function resolve(specifier, context, next) {
5-
const noAttributesSpecified = context.importAttributes.type == null;
6+
function resolve(specifier, context, next) {
7+
// Hooks also run for require() calls, which have no import attributes.
8+
if (context.importAttributes === undefined) {
9+
return next(specifier, context);
10+
}
611

7-
// Mutation from resolve hook should be discarded.
8-
context.importAttributes.type = 'whatever';
12+
const noAttributesSpecified = context.importAttributes.type == null;
913

1014
// This fixture assumes that no other resolve hooks in the chain will error on invalid import attributes
1115
// (as defaultResolve doesn't).
12-
const result = await next(specifier, context);
16+
const result = next(specifier, {
17+
...context,
18+
importAttributes: { ...context.importAttributes, type: 'whatever' },
19+
});
1320

1421
if (noAttributesSpecified &&
1522
(DATA_URL_PATTERN.test(result.url) || JSON_URL_PATTERN.test(result.url))) {
@@ -22,3 +29,5 @@ export async function resolve(specifier, context, next) {
2229

2330
return result;
2431
}
32+
33+
registerHooks({ resolve });

test/fixtures/es-module-shadow-realm/custom-loaders.js renamed to test/fixtures/es-module-shadow-realm/custom-hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// This fixture is used to test that custom loaders are not enabled in the ShadowRealm.
1+
// This fixture is used to test that customization hooks are not enabled in the ShadowRealm.
22

33
'use strict';
44
const assert = require('assert');
55

66
async function workInChildProcess() {
7-
// Assert that the process is running with a custom loader.
7+
// Assert that the process is running with custom hooks.
88
const moduleNamespace = await import('file:///42.mjs');
99
assert.strictEqual(moduleNamespace.default, 42);
1010

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const { registerHooks } = require('node:module');
3+
4+
registerHooks({
5+
resolve(specifier, context, nextResolve) {
6+
if (specifier.startsWith('file:///') && specifier.includes('42')) {
7+
return { shortCircuit: true, url: specifier };
8+
}
9+
return nextResolve(specifier, context);
10+
},
11+
load(url, context, nextLoad) {
12+
if (url.startsWith('file:///') && url.includes('42')) {
13+
return { shortCircuit: true, format: 'module', source: 'export default 42' };
14+
}
15+
return nextLoad(url, context);
16+
},
17+
});

‎test/fixtures/module-hooks/register-logger-async-hooks.mjs‎

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { registerHooks } from 'node:module';
2+
import { writeSync } from 'node:fs';
3+
4+
registerHooks({
5+
resolve(specifier, context, nextResolve) {
6+
writeSync(1, `resolve ${specifier} from ${context.parentURL}\n`);
7+
return nextResolve(specifier, context);
8+
},
9+
load(url, context, nextLoad) {
10+
writeSync(1, `load ${url}\n`);
11+
return nextLoad(url, context);
12+
},
13+
});

‎test/fixtures/test-runner/coverage-loader/hooks.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { registerHooks } from 'node:module';
2+
13
const sources = {
24
// Virtual file. Doesn't exist on disk
35
"virtual.js": `
@@ -16,10 +18,12 @@ import{describe,it}from"node:test";import assert from"node:assert";import{sum}fr
1618
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJtYXBwaW5ncyI6ImtIQUFPLFNBQVMsT0FBUSxFQUFHLENBQ3pCLEdBQUksQ0FBQyxFQUFFLE1BQU8sS0FBUSxPQUFPLE1BQVEsUUFBUSxFQUFHLE1BQU0sSUFBSSxNQUFNLGNBQWMsRUFDOUUsT0FBTyxFQUFFLE9BQU8sQ0FBQyxJQUFLLE1BQVEsSUFBTSxHQUFHLENBQ3pDLENBSGdCIiwibmFtZXMiOltdLCJpZ25vcmVMaXN0IjpbXSwic291cmNlcyI6WyIuL3N1bS50cyJdLCJzb3VyY2VzQ29udGVudCI6W251bGxdfQ==`,
1719
};
1820

19-
export async function load(url, context, nextLoad) {
21+
function load(url, context, nextLoad) {
2022
const file = url.split('/').at(-1);
2123
if (sources[file] !== undefined) {
2224
return { format: "module", source: sources[file], shortCircuit: true };
2325
}
2426
return nextLoad(url, context);
2527
}
28+
29+
registerHooks({ load });

0 commit comments

Comments
 (0)