|
| 1 | +// Flags: --expose-internals |
| 2 | +import{mustCall}from'../common/index.mjs'; |
| 3 | +importesmLoaderModulefrom'internal/modules/esm/loader'; |
| 4 | +importassertfrom'assert'; |
| 5 | + |
| 6 | +const{ ESMLoader }=esmLoaderModule; |
| 7 | + |
| 8 | +/** |
| 9 | + * Verify custom hooks are called with appropriate arguments. |
| 10 | + */ |
| 11 | +{ |
| 12 | +constesmLoader=newESMLoader(); |
| 13 | + |
| 14 | +constoriginalSpecifier='foo/bar'; |
| 15 | +constimportAssertions=Object.assign( |
| 16 | +Object.create(null), |
| 17 | +{type: 'json'}, |
| 18 | +); |
| 19 | +constparentURL='file:///entrypoint.js'; |
| 20 | +constresolvedURL='file:///foo/bar.js'; |
| 21 | +constsuggestedFormat='test'; |
| 22 | + |
| 23 | +functionresolve(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(typeofdefaultResolve,'function'); |
| 35 | + |
| 36 | +return{ |
| 37 | +format: suggestedFormat, |
| 38 | +url: resolvedURL, |
| 39 | +}; |
| 40 | +} |
| 41 | + |
| 42 | +functionload(resolvedURL,context,defaultLoad){ |
| 43 | +assert.strictEqual(resolvedURL,resolvedURL); |
| 44 | +assert.ok(newURL(resolvedURL)); |
| 45 | +// Ensure `context` has all and only the properties it's supposed to |
| 46 | +assert.deepStrictEqual(Object.keys(context),[ |
| 47 | +'format', |
| 48 | +'importAssertions', |
| 49 | +]); |
| 50 | +assert.strictEqual(context.format,suggestedFormat); |
| 51 | +assert.deepStrictEqual(context.importAssertions,importAssertions); |
| 52 | +assert.strictEqual(typeofdefaultLoad,'function'); |
| 53 | + |
| 54 | +// This doesn't matter (just to avoid errors) |
| 55 | +return{ |
| 56 | +format: 'module', |
| 57 | +source: '', |
| 58 | +}; |
| 59 | +} |
| 60 | + |
| 61 | +constcustomLoader={ |
| 62 | +// Ensure ESMLoader actually calls the custom hooks |
| 63 | +resolve: mustCall(resolve), |
| 64 | +load: mustCall(load), |
| 65 | +}; |
| 66 | + |
| 67 | +esmLoader.addCustomLoaders(customLoader); |
| 68 | + |
| 69 | +// Manually trigger hooks (since ESMLoader is not actually running) |
| 70 | +constjob=awaitesmLoader.getModuleJob( |
| 71 | +originalSpecifier, |
| 72 | +parentURL, |
| 73 | +importAssertions, |
| 74 | +); |
| 75 | +awaitjob.modulePromise; |
| 76 | +} |
0 commit comments