|
1 |
| -// Flags: --expose-internals |
2 |
| -import { mustCall } from '../common/index.mjs'; |
3 |
| -import esmLoaderModule from 'internal/modules/esm/loader'; |
4 |
| -import assert from 'assert'; |
5 |
| - |
6 |
| -const { ESMLoader } = esmLoaderModule; |
7 |
| - |
8 |
| -/** |
9 |
| - * Verify custom hooks are called with appropriate arguments. |
10 |
| - */ |
11 |
| -{ |
12 |
| - const esmLoader = new ESMLoader(); |
13 |
| - |
14 |
| - const originalSpecifier = 'foo/bar'; |
15 |
| - const importAssertions = { |
16 |
| - __proto__: null, |
17 |
| - type: 'json', |
18 |
| - }; |
19 |
| - const parentURL = 'file:///entrypoint.js'; |
20 |
| - const resolvedURL = 'file:///foo/bar.js'; |
21 |
| - const suggestedFormat = 'test'; |
22 |
| - |
23 |
| - function resolve(specifier, context, defaultResolve) { |
24 |
| - assert.strictEqual(specifier, originalSpecifier); |
25 |
| - // Ensure `context` has all and only the properties it's supposed to |
26 |
| - assert.deepStrictEqual(Object.keys(context), [ |
27 |
| - 'conditions', |
28 |
| - 'importAssertions', |
29 |
| - 'parentURL', |
30 |
| - ]); |
31 |
| - assert.ok(Array.isArray(context.conditions)); |
32 |
| - assert.deepStrictEqual(context.importAssertions, importAssertions); |
33 |
| - assert.strictEqual(context.parentURL, parentURL); |
34 |
| - assert.strictEqual(typeof defaultResolve, 'function'); |
35 |
| - |
36 |
| - return { |
37 |
| - format: suggestedFormat, |
38 |
| - shortCircuit: true, |
39 |
| - url: resolvedURL, |
40 |
| - }; |
41 |
| - } |
42 |
| - |
43 |
| - function load(resolvedURL, context, defaultLoad) { |
44 |
| - assert.strictEqual(resolvedURL, resolvedURL); |
45 |
| - assert.ok(new URL(resolvedURL)); |
46 |
| - // Ensure `context` has all and only the properties it's supposed to |
47 |
| - assert.deepStrictEqual(Object.keys(context), [ |
48 |
| - 'format', |
49 |
| - 'importAssertions', |
| 1 | +import { spawnPromisified } from '../common/index.mjs'; |
| 2 | +import * as fixtures from '../common/fixtures.mjs'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { execPath } from 'node:process'; |
| 5 | +import { describe, it } from 'node:test'; |
| 6 | + |
| 7 | +describe('Loader hooks', () => { |
| 8 | + it('are called with all expected arguments', async () => { |
| 9 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ |
| 10 | + '--no-warnings', |
| 11 | + '--experimental-loader', |
| 12 | + fixtures.fileURL('/es-module-loaders/hooks-input.mjs'), |
| 13 | + fixtures.path('/es-modules/json-modules.mjs'), |
50 | 14 | ]);
|
51 |
| - assert.strictEqual(context.format, suggestedFormat); |
52 |
| - assert.deepStrictEqual(context.importAssertions, importAssertions); |
53 |
| - assert.strictEqual(typeof defaultLoad, 'function'); |
54 |
| - |
55 |
| - // This doesn't matter (just to avoid errors) |
56 |
| - return { |
57 |
| - format: 'module', |
58 |
| - shortCircuit: true, |
59 |
| - source: '', |
60 |
| - }; |
61 |
| - } |
62 |
| - |
63 |
| - const customLoader = [ |
64 |
| - { |
65 |
| - exports: { |
66 |
| - // Ensure ESMLoader actually calls the custom hooks |
67 |
| - resolve: mustCall(resolve), |
68 |
| - load: mustCall(load), |
69 |
| - }, |
70 |
| - url: import.meta.url, |
71 |
| - }, |
72 |
| - ]; |
73 |
| - |
74 |
| - esmLoader.addCustomLoaders(customLoader); |
75 | 15 |
|
76 |
| - // Manually trigger hooks (since ESMLoader is not actually running) |
77 |
| - const job = await esmLoader.getModuleJob( |
78 |
| - originalSpecifier, |
79 |
| - parentURL, |
80 |
| - importAssertions, |
81 |
| - ); |
82 |
| - await job.modulePromise; |
83 |
| -} |
| 16 | + assert.strictEqual(stderr, ''); |
| 17 | + assert.strictEqual(code, 0); |
| 18 | + assert.strictEqual(signal, null); |
| 19 | + |
| 20 | + const lines = stdout.split('\n'); |
| 21 | + assert.match(lines[0], /{"url":"file:\/\/\/.*\/json-modules\.mjs","format":"test","shortCircuit":true}/); |
| 22 | + assert.match(lines[1], /{"source":{"type":"Buffer","data":\[.*\]},"format":"module","shortCircuit":true}/); |
| 23 | + assert.match(lines[2], /{"url":"file:\/\/\/.*\/experimental\.json","format":"test","shortCircuit":true}/); |
| 24 | + assert.match(lines[3], /{"source":{"type":"Buffer","data":\[.*\]},"format":"json","shortCircuit":true}/); |
| 25 | + }); |
| 26 | +}); |
0 commit comments