diff options
author | Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> | 2022-02-18 19:55:58 +0100 |
---|---|---|
committer | Danielle Adams <adamzdanielle@gmail.com> | 2022-04-23 22:26:07 -0400 |
commit | 9b8c927d29ab4c81b0f96b594577a852090095cd (patch) | |
tree | 4fb08e08662e350972c09c4fa9d621e2b64a7ad4 /test | |
parent | c2298890225df45930d99d7f88d4df73f4f03ddf (diff) | |
download | node-new-9b8c927d29ab4c81b0f96b594577a852090095cd.tar.gz |
esm: remove erroneous `context.parentURL` property passed to `load` hook
PR-URL: https://github.com/nodejs/node/pull/41975
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/es-module/test-esm-loader-hooks.mjs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs new file mode 100644 index 0000000000..57a203342a --- /dev/null +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -0,0 +1,76 @@ +// Flags: --expose-internals +import { mustCall } from '../common/index.mjs'; +import esmLoaderModule from 'internal/modules/esm/loader'; +import assert from 'assert'; + +const { ESMLoader } = esmLoaderModule; + +/** + * Verify custom hooks are called with appropriate arguments. + */ +{ + const esmLoader = new ESMLoader(); + + const originalSpecifier = 'foo/bar'; + const importAssertions = Object.assign( + Object.create(null), + { type: 'json' }, + ); + const parentURL = 'file:///entrypoint.js'; + const resolvedURL = 'file:///foo/bar.js'; + const suggestedFormat = 'test'; + + function resolve(specifier, context, defaultResolve) { + assert.strictEqual(specifier, originalSpecifier); + // Ensure `context` has all and only the properties it's supposed to + assert.deepStrictEqual(Object.keys(context), [ + 'conditions', + 'importAssertions', + 'parentURL', + ]); + assert.ok(Array.isArray(context.conditions)); + assert.deepStrictEqual(context.importAssertions, importAssertions); + assert.strictEqual(context.parentURL, parentURL); + assert.strictEqual(typeof defaultResolve, 'function'); + + return { + format: suggestedFormat, + url: resolvedURL, + }; + } + + function load(resolvedURL, context, defaultLoad) { + assert.strictEqual(resolvedURL, resolvedURL); + assert.ok(new URL(resolvedURL)); + // Ensure `context` has all and only the properties it's supposed to + assert.deepStrictEqual(Object.keys(context), [ + 'format', + 'importAssertions', + ]); + assert.strictEqual(context.format, suggestedFormat); + assert.deepStrictEqual(context.importAssertions, importAssertions); + assert.strictEqual(typeof defaultLoad, 'function'); + + // This doesn't matter (just to avoid errors) + return { + format: 'module', + source: '', + }; + } + + const customLoader = { + // Ensure ESMLoader actually calls the custom hooks + resolve: mustCall(resolve), + load: mustCall(load), + }; + + esmLoader.addCustomLoaders(customLoader); + + // Manually trigger hooks (since ESMLoader is not actually running) + const job = await esmLoader.getModuleJob( + originalSpecifier, + parentURL, + importAssertions, + ); + await job.modulePromise; +} |