From 4427f0acbbbec6dc64719eef1119e7b6753760a1 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 14:15:29 -0700 Subject: [PATCH 01/11] add support for custom resolvers --- src/esmockModule.js | 14 ++++++- tests/local/customResolverChild.js | 7 ++++ tests/local/customResolverParent.js | 5 +++ .../esmock.node.loader-custom.test.js | 39 +++++++++++++++++++ tests/tests-node/esmock.node.test.js | 30 +++++++++++++- 5 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 tests/local/customResolverChild.js create mode 100644 tests/local/customResolverParent.js create mode 100644 tests/tests-node/esmock.node.loader-custom.test.js diff --git a/src/esmockModule.js b/src/esmockModule.js index 6f47d054..938cab66 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -108,6 +108,14 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => { return mockModuleKey } +const esmockCustomResolvers = (id, parent, resolvers) => { + if (!Array.isArray(resolvers) || !resolvers.length) + return null + + return resolvers[0](id, parent) + || esmockCustomResolvers(id, parent, resolvers.slice(1)) +} + const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { ids = ids || Object.keys(defs) id = ids[0] @@ -115,7 +123,8 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { if (!id) return mocks - const fileURL = resolvewith(id, parent) + const fileURL = esmockCustomResolvers(id, parent, opt.resolvers) + || resolvewith(id, parent) if (!fileURL && opt.isModuleNotFoundError !== false && id !== 'import') throw esmockErr.errModuleIdNotFound(id, parent) @@ -125,7 +134,8 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { } const esmockModule = async (moduleId, parent, defs, gdefs, opt) => { - const moduleFileURL = resolvewith(moduleId, parent) + const moduleFileURL = esmockCustomResolvers(moduleId, parent, opt.resolvers) + || resolvewith(moduleId, parent) if (!moduleFileURL) throw esmockErr.errModuleIdNotFound(moduleId, parent) diff --git a/tests/local/customResolverChild.js b/tests/local/customResolverChild.js new file mode 100644 index 00000000..15c80096 --- /dev/null +++ b/tests/local/customResolverChild.js @@ -0,0 +1,7 @@ +const isMocked = false +const isCustomResolverChild = true + +export default { + isCustomResolverChild, + isMocked +} diff --git a/tests/local/customResolverParent.js b/tests/local/customResolverParent.js new file mode 100644 index 00000000..32f1c7f9 --- /dev/null +++ b/tests/local/customResolverParent.js @@ -0,0 +1,5 @@ +import child from 'RESOLVECUSTOM' + +export { + child +} diff --git a/tests/tests-node/esmock.node.loader-custom.test.js b/tests/tests-node/esmock.node.loader-custom.test.js new file mode 100644 index 00000000..a918609a --- /dev/null +++ b/tests/tests-node/esmock.node.loader-custom.test.js @@ -0,0 +1,39 @@ +import path from 'path' +import test from 'node:test' +import assert from 'node:assert/strict' +import module from 'node:module' +import esmock from 'esmock' + +function resolverCustom (moduleId, parent) { + return /RESOLVECUSTOM$/.test(moduleId) && new URL( + path.resolve( + new URL(parent).pathname, + '../../local/customResolverChild.js'), + parent + ).href +} + +async function resolve (specifier, context, next) { + return next( + specifier === 'RESOLVECUSTOM' + ? resolverCustom(specifier, context.parentURL) + : specifier, context) +} + +module.register(` +data:text/javascript, +import path from 'node:path'; +${encodeURIComponent(resolverCustom)} +export ${encodeURIComponent(resolve)}`.slice(1)) + +test('should use custom resolver', async () => { + const customResolverParent = await esmock( + '../local/customResolverParent.js', {}, { + RESOLVECUSTOM: ({ isMocked: true }) + }, { + resolvers: [resolverCustom] + }) + + assert.ok(customResolverParent.child.isCustomResolverChild) + assert.ok(customResolverParent.child.isMocked) +}) diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index bd502ebb..dc8ff3af 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -4,7 +4,7 @@ import assert from 'node:assert/strict' import esmock from 'esmock' import sinon from 'sinon' import esmockCache from '../../src/esmockCache.js' - +/* test('should mock node:process', async () => { // has direct and in-direct calls to `process.cwd()` const thingBeingTested = await esmock('../local/usesNodeProcess.js', {}, { @@ -536,3 +536,31 @@ test('should mock scoped package, @aws-sdk/client-s3 (deep)', async () => { assert.strictEqual(scopedClientS3.mocked, 'mock client') }) +*/ +test('should use custom resolver', async () => { + const customResolverParent = await esmock( + '../local/customResolverParent.js', {}, { + 'form-urlencoded': ({ isMocked: true }) + }, { + resolvers: [ + (moduleId, parent) => { + const pathParent = new URL(parent).pathname + const pathChild = path.resolve( + pathParent, '../../local/customResolverChild.js') + + if (/CUSTOMLOADER$/.test(moduleId)) { + console.log('resolved path', new URL(pathChild, parent).href) + } + + return /CUSTOMLOADER$/.test(moduleId) + ? new URL(pathChild, parent).href + // ? path.join(path.dirname(parent), '../local/customResolverChild.js') + : null + } + ] + }) + + console.log('child', customResolverParent.child) + assert.ok(customResolverParent.child.isCustomResolverChild) + assert.ok(customResolverParent.child.isMocked) +}) From c4c5825dbf7d9f4446bf447b5c87f4524441a5b1 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 14:17:26 -0700 Subject: [PATCH 02/11] restore esmock.node.test --- tests/tests-node/esmock.node.test.js | 30 +--------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index dc8ff3af..bd502ebb 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -4,7 +4,7 @@ import assert from 'node:assert/strict' import esmock from 'esmock' import sinon from 'sinon' import esmockCache from '../../src/esmockCache.js' -/* + test('should mock node:process', async () => { // has direct and in-direct calls to `process.cwd()` const thingBeingTested = await esmock('../local/usesNodeProcess.js', {}, { @@ -536,31 +536,3 @@ test('should mock scoped package, @aws-sdk/client-s3 (deep)', async () => { assert.strictEqual(scopedClientS3.mocked, 'mock client') }) -*/ -test('should use custom resolver', async () => { - const customResolverParent = await esmock( - '../local/customResolverParent.js', {}, { - 'form-urlencoded': ({ isMocked: true }) - }, { - resolvers: [ - (moduleId, parent) => { - const pathParent = new URL(parent).pathname - const pathChild = path.resolve( - pathParent, '../../local/customResolverChild.js') - - if (/CUSTOMLOADER$/.test(moduleId)) { - console.log('resolved path', new URL(pathChild, parent).href) - } - - return /CUSTOMLOADER$/.test(moduleId) - ? new URL(pathChild, parent).href - // ? path.join(path.dirname(parent), '../local/customResolverChild.js') - : null - } - ] - }) - - console.log('child', customResolverParent.child) - assert.ok(customResolverParent.child.isCustomResolverChild) - assert.ok(customResolverParent.child.isMocked) -}) From 6b95f8fb221b1aeac3aac4bd9f49a05f091b5803 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 14:21:37 -0700 Subject: [PATCH 03/11] update CHANGELOG and increment version --- CHANGELOG.md | 3 ++- package.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a0ec1a0..db2fe8b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # changelog * 2.5.3 _Oct.12.2023_ - * [update resolver](https://github.com/iambumblehead/esmock/pull/243) to latest version, slightly faster with fewer loops + * [update resolver](https://github.com/iambumblehead/esmock/pull/250) to latest version, slightly faster with fewer loops + * [add support for resolver](https://github.com/iambumblehead/esmock/pull/251) configuration option * 2.5.2 _Oct.06.2023_ * [update resolver](https://github.com/iambumblehead/esmock/pull/243) to improve module resolution. See resolvewithplus tags [v2.0.6](https://github.com/iambumblehead/resolvewithplus/releases/tag/v2.0.6) and [v2.0.7.](https://github.com/iambumblehead/resolvewithplus/releases/tag/v2.0.7) * **resolve "exports" before "main".** The [spec says:](https://nodejs.org/api/packages.html#package-entry-points) _the "exports" field takes precedence over "main" in supported versions of Node.js._ The updated resolver correctly returns "main" before "exports" (older resolver did not). diff --git a/package.json b/package.json index fc6d6983..41d110f3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.5.2", + "version": "2.5.3", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import and globals mocking for unit tests", From df65ae272cc824dc05b405dcb08b04506d2b0106 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 15:00:17 -0700 Subject: [PATCH 04/11] return custom resolved path in way that is platform-agnostic way, so that windows environment tests will pass --- tests/tests-node/esmock.node.loader-custom.test.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/tests-node/esmock.node.loader-custom.test.js b/tests/tests-node/esmock.node.loader-custom.test.js index a918609a..0c6ee6f7 100644 --- a/tests/tests-node/esmock.node.loader-custom.test.js +++ b/tests/tests-node/esmock.node.loader-custom.test.js @@ -5,12 +5,8 @@ import module from 'node:module' import esmock from 'esmock' function resolverCustom (moduleId, parent) { - return /RESOLVECUSTOM$/.test(moduleId) && new URL( - path.resolve( - new URL(parent).pathname, - '../../local/customResolverChild.js'), - parent - ).href + return /RESOLVECUSTOM$/.test(moduleId) && parent + .replace(/\/tests\/.*$/, '/tests/local/customResolverChild.js') } async function resolve (specifier, context, next) { @@ -22,7 +18,6 @@ async function resolve (specifier, context, next) { module.register(` data:text/javascript, -import path from 'node:path'; ${encodeURIComponent(resolverCustom)} export ${encodeURIComponent(resolve)}`.slice(1)) From 478f47ac77e07ca3453ca1f1bcdea3b7a2a2e6b1 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 15:02:44 -0700 Subject: [PATCH 05/11] remove unused path import --- tests/tests-node/esmock.node.loader-custom.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/tests-node/esmock.node.loader-custom.test.js b/tests/tests-node/esmock.node.loader-custom.test.js index 0c6ee6f7..8be0149c 100644 --- a/tests/tests-node/esmock.node.loader-custom.test.js +++ b/tests/tests-node/esmock.node.loader-custom.test.js @@ -1,4 +1,3 @@ -import path from 'path' import test from 'node:test' import assert from 'node:assert/strict' import module from 'node:module' From 5402e09e6fd0be9e3891b65131b2fb4d72daff82 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 15:27:06 -0700 Subject: [PATCH 06/11] skip custom resolver test if module.register not defined --- tests/tests-node/esmock.node.loader-custom.test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/tests-node/esmock.node.loader-custom.test.js b/tests/tests-node/esmock.node.loader-custom.test.js index 8be0149c..03216adf 100644 --- a/tests/tests-node/esmock.node.loader-custom.test.js +++ b/tests/tests-node/esmock.node.loader-custom.test.js @@ -15,12 +15,16 @@ async function resolve (specifier, context, next) { : specifier, context) } -module.register(` +const loader = ` data:text/javascript, ${encodeURIComponent(resolverCustom)} -export ${encodeURIComponent(resolve)}`.slice(1)) +export ${encodeURIComponent(resolve)}`.slice(1) test('should use custom resolver', async () => { + if (!module.register) + return assert.ok('skip test') + + module.register(loader) const customResolverParent = await esmock( '../local/customResolverParent.js', {}, { RESOLVECUSTOM: ({ isMocked: true }) From 63a4a3b29a804e8d61e56af15803936dd89909ee Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 15:28:17 -0700 Subject: [PATCH 07/11] rename test loader-custom to resolver-custom --- ....loader-custom.test.js => esmock.node.resolver-custom.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/tests-node/{esmock.node.loader-custom.test.js => esmock.node.resolver-custom.test.js} (100%) diff --git a/tests/tests-node/esmock.node.loader-custom.test.js b/tests/tests-node/esmock.node.resolver-custom.test.js similarity index 100% rename from tests/tests-node/esmock.node.loader-custom.test.js rename to tests/tests-node/esmock.node.resolver-custom.test.js From e3e805194f38e1f3cd914d49df65202d0fc2ee42 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 12 Oct 2023 16:06:37 -0700 Subject: [PATCH 08/11] change interface to use { resolver } rather than { resolvers } --- src/esmockArgs.js | 4 +++- src/esmockModule.js | 14 ++------------ .../esmock.node.resolver-custom.test.js | 17 ++++++++++++++--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/esmockArgs.js b/src/esmockArgs.js index 41cf4ad3..5be05cc2 100644 --- a/src/esmockArgs.js +++ b/src/esmockArgs.js @@ -1,3 +1,5 @@ +import resolver from 'resolvewithplus' + // extracts path or fileurl from stack, // ' at (/root/test.js:11:31)' -> /root/test.js // ' at Object.handler (file:///D:/a/test.js:11:31)' -> file:///D:/a/test.js @@ -17,7 +19,7 @@ export default (arg, optsextra) => { (new Error).stack.split('\n')[3].replace(stackpathre, '$2'), ...arg.slice(1) ] - arg[4] = {...arg[4], ...optsextra} + arg[4] = { resolver, ...arg[4], ...optsextra} return arg } diff --git a/src/esmockModule.js b/src/esmockModule.js index 938cab66..0a5cbb50 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -108,14 +108,6 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => { return mockModuleKey } -const esmockCustomResolvers = (id, parent, resolvers) => { - if (!Array.isArray(resolvers) || !resolvers.length) - return null - - return resolvers[0](id, parent) - || esmockCustomResolvers(id, parent, resolvers.slice(1)) -} - const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { ids = ids || Object.keys(defs) id = ids[0] @@ -123,8 +115,7 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { if (!id) return mocks - const fileURL = esmockCustomResolvers(id, parent, opt.resolvers) - || resolvewith(id, parent) + const fileURL = opt.resolver(id, parent) if (!fileURL && opt.isModuleNotFoundError !== false && id !== 'import') throw esmockErr.errModuleIdNotFound(id, parent) @@ -134,8 +125,7 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { } const esmockModule = async (moduleId, parent, defs, gdefs, opt) => { - const moduleFileURL = esmockCustomResolvers(moduleId, parent, opt.resolvers) - || resolvewith(moduleId, parent) + const moduleFileURL = opt.resolver(moduleId, parent) if (!moduleFileURL) throw esmockErr.errModuleIdNotFound(moduleId, parent) diff --git a/tests/tests-node/esmock.node.resolver-custom.test.js b/tests/tests-node/esmock.node.resolver-custom.test.js index 03216adf..31b7ba69 100644 --- a/tests/tests-node/esmock.node.resolver-custom.test.js +++ b/tests/tests-node/esmock.node.resolver-custom.test.js @@ -4,8 +4,19 @@ import module from 'node:module' import esmock from 'esmock' function resolverCustom (moduleId, parent) { - return /RESOLVECUSTOM$/.test(moduleId) && parent - .replace(/\/tests\/.*$/, '/tests/local/customResolverChild.js') + parent = parent.replace(/\/tests\/.*$/, '/tests/tests-node/') + + // This logic looks unusual because of constraints here. This function must: + // * must work at windows, where path.join and path.resolve cause issues + // * must be string-serializable, no external funcions + // * must resolve these moduleIds to corresponding, existing filepaths + // * '../local/customResolverParent.js', + // * 'RESOLVECUSTOM/ + return ( + /RESOLVECUSTOM$/.test(moduleId) + ? parent + '../local/customResolverChild.js' + : parent + moduleId + ).replace(/\/tests-node\/\.\./, '') } async function resolve (specifier, context, next) { @@ -29,7 +40,7 @@ test('should use custom resolver', async () => { '../local/customResolverParent.js', {}, { RESOLVECUSTOM: ({ isMocked: true }) }, { - resolvers: [resolverCustom] + resolver: resolverCustom }) assert.ok(customResolverParent.child.isCustomResolverChild) From adfea89779cc7314ccb29b85b0169c3efb19c9aa Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 13 Oct 2023 10:40:25 -0700 Subject: [PATCH 09/11] added Resolver Type definition from @koshic --- src/esmock.d.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/esmock.d.ts b/src/esmock.d.ts index 520c2829..03135a7f 100644 --- a/src/esmock.d.ts +++ b/src/esmock.d.ts @@ -1,9 +1,11 @@ type MockMap = { [specifier: string]: any } +type Resolver = (id: string, parent: string) => string type Options = { strict?: boolean | undefined, purge?: boolean | undefined, - isModuleNotFoundError?: boolean | undefined + isModuleNotFoundError?: boolean | undefined, + resolver?: Resolver | undefined } type MockFunction = { @@ -90,5 +92,6 @@ export { strictest, type MockFunction, type MockMap, - type Options + type Options, + type Resolver } From f99db9dc36958b1e87c313fa52d2fc354db793ff Mon Sep 17 00:00:00 2001 From: Vladimir Grenaderov Date: Fri, 13 Oct 2023 17:58:08 +0000 Subject: [PATCH 10/11] fix Resolver Type definition --- src/esmock.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmock.d.ts b/src/esmock.d.ts index 03135a7f..7bbabfd8 100644 --- a/src/esmock.d.ts +++ b/src/esmock.d.ts @@ -1,5 +1,5 @@ type MockMap = { [specifier: string]: any } -type Resolver = (id: string, parent: string) => string +type Resolver = (id: string, parent: string) => string | null type Options = { strict?: boolean | undefined, From 4dd23ae5ae145806ee76fe8b196286f37ee2802c Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 13 Oct 2023 11:00:58 -0700 Subject: [PATCH 11/11] do not call custom resolver with builtin moduleId --- src/esmockModule.js | 14 +++++++---- tests/local/customResolverParent.js | 6 ++++- .../esmock.node.resolver-custom.test.js | 24 ++++++++++++++++--- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 0a5cbb50..01771844 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -17,6 +17,9 @@ const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/ const nextId = ((id = 0) => () => ++id)() const objProto = Object.getPrototypeOf({}) const isPlainObj = o => Object.getPrototypeOf(o) === objProto +const iscoremodule = resolvewith.iscoremodule +const protocolNodeRe = /^node:/ +const addprotocolnode = p => protocolNodeRe.test(p) ? p : `node:${p}` // assigning the object to its own prototypal inheritor can error, eg // 'Cannot assign to read only property \'F_OK\' of object \'#\'' @@ -46,7 +49,7 @@ const esmockModuleApply = (defLive, def, fileURL) => { // if safe, an extra "default.default" is added for compatibility with // babel-generated dist cjs files which also define "default.default" - if (!resolvewith.iscoremodule(fileURL) && Object.isExtensible(def.default)) + if (!iscoremodule(fileURL) && Object.isExtensible(def.default)) def.default.default = def.default return def @@ -59,7 +62,7 @@ const esmockModuleIsESM = (fileURL, isesm) => { if (typeof isesm === 'boolean') return isesm - isesm = !resolvewith.iscoremodule(fileURL) + isesm = !iscoremodule(fileURL) && isDirPathRe.test(fileURL) && esmockIsESMRe.test(fs.readFileSync(fileURL, 'utf-8')) @@ -108,6 +111,9 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => { return mockModuleKey } +const esmockResolve = (id, parent, opt) => ( + iscoremodule(id) ? addprotocolnode(id) : opt.resolver(id, parent)) + const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { ids = ids || Object.keys(defs) id = ids[0] @@ -115,7 +121,7 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { if (!id) return mocks - const fileURL = opt.resolver(id, parent) + const fileURL = esmockResolve(id, parent, opt) if (!fileURL && opt.isModuleNotFoundError !== false && id !== 'import') throw esmockErr.errModuleIdNotFound(id, parent) @@ -125,7 +131,7 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { } const esmockModule = async (moduleId, parent, defs, gdefs, opt) => { - const moduleFileURL = opt.resolver(moduleId, parent) + const moduleFileURL = esmockResolve(moduleId, parent, opt) if (!moduleFileURL) throw esmockErr.errModuleIdNotFound(moduleId, parent) diff --git a/tests/local/customResolverParent.js b/tests/local/customResolverParent.js index 32f1c7f9..e6e5cb6d 100644 --- a/tests/local/customResolverParent.js +++ b/tests/local/customResolverParent.js @@ -1,5 +1,9 @@ import child from 'RESOLVECUSTOM' +import path from 'node:path' + +const pathbasenameresult = path.basename('/the/very/happy-dog.png') export { - child + child, + pathbasenameresult } diff --git a/tests/tests-node/esmock.node.resolver-custom.test.js b/tests/tests-node/esmock.node.resolver-custom.test.js index 31b7ba69..84b9f854 100644 --- a/tests/tests-node/esmock.node.resolver-custom.test.js +++ b/tests/tests-node/esmock.node.resolver-custom.test.js @@ -26,16 +26,15 @@ async function resolve (specifier, context, next) { : specifier, context) } -const loader = ` +module.register && module.register(` data:text/javascript, ${encodeURIComponent(resolverCustom)} -export ${encodeURIComponent(resolve)}`.slice(1) +export ${encodeURIComponent(resolve)}`.slice(1)) test('should use custom resolver', async () => { if (!module.register) return assert.ok('skip test') - module.register(loader) const customResolverParent = await esmock( '../local/customResolverParent.js', {}, { RESOLVECUSTOM: ({ isMocked: true }) @@ -46,3 +45,22 @@ test('should use custom resolver', async () => { assert.ok(customResolverParent.child.isCustomResolverChild) assert.ok(customResolverParent.child.isMocked) }) + +test('should not call custom resover with builtin moduleIds', async () => { + if (!module.register) + return assert.ok('skip test') + + const customResolverParent = await esmock( + '../local/customResolverParent.js', {}, { + RESOLVECUSTOM: ({ isMocked: true }), + path: { basename: () => 'basenametest' } + }, { + resolver: resolverCustom + }) + + assert.ok(customResolverParent.child.isCustomResolverChild) + assert.ok(customResolverParent.child.isMocked) + assert.strictEqual( + customResolverParent.pathbasenameresult, + 'basenametest') +})