diff --git a/CHANGELOG.md b/CHANGELOG.md index db2fe8b..eb26338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # changelog + * 2.5.4 _Oct.13.2023_ + * [remove condition](https://github.com/iambumblehead/esmock/pull/252) to not-call resolver with builtin moduleId * 2.5.3 _Oct.12.2023_ * [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 diff --git a/package.json b/package.json index 41d110f..6070676 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "esmock", "type": "module", - "version": "2.5.3", + "version": "2.5.4", "license": "ISC", "readmeFilename": "README.md", "description": "provides native ESM import and globals mocking for unit tests", diff --git a/src/esmockModule.js b/src/esmockModule.js index 0177184..3adc68d 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -18,8 +18,6 @@ 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 \'#\'' @@ -111,9 +109,6 @@ 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] @@ -121,7 +116,7 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { if (!id) return mocks - const fileURL = esmockResolve(id, parent, opt) + const fileURL = opt.resolver(id, parent) if (!fileURL && opt.isModuleNotFoundError !== false && id !== 'import') throw esmockErr.errModuleIdNotFound(id, parent) @@ -131,7 +126,7 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { } const esmockModule = async (moduleId, parent, defs, gdefs, opt) => { - const moduleFileURL = esmockResolve(moduleId, parent, opt) + 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 84b9f85..d06bac3 100644 --- a/tests/tests-node/esmock.node.resolver-custom.test.js +++ b/tests/tests-node/esmock.node.resolver-custom.test.js @@ -4,18 +4,21 @@ import module from 'node:module' import esmock from 'esmock' function resolverCustom (moduleId, parent) { - 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/ + if (/(node:)?path/.test(moduleId)) + return 'node:path' + return ( - /RESOLVECUSTOM$/.test(moduleId) - ? parent + '../local/customResolverChild.js' - : parent + moduleId + parent.replace(/\/tests\/.*$/, '/tests/tests-node/') + ( + /RESOLVECUSTOM$/.test(moduleId) + ? '../local/customResolverChild.js' + : moduleId + ) ).replace(/\/tests-node\/\.\./, '') }