From 20ea899a76952e31cb5fdf463589a36d9fa3c0dd Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 13 Oct 2023 11:56:47 -0700 Subject: [PATCH 1/2] remove condition preventing core moduleIds from resolver --- CHANGELOG.md | 2 ++ package.json | 2 +- src/esmockModule.js | 9 ++------- .../tests-node/esmock.node.resolver-custom.test.js | 13 ++++++++----- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db2fe8b..5ca4b75 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/253) 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\/\.\./, '') } From 1548792be63679f2eb5a477a00c013d18dd415c7 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 13 Oct 2023 12:02:26 -0700 Subject: [PATCH 2/2] update CHANGELOG link --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ca4b75..eb26338 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # changelog * 2.5.4 _Oct.13.2023_ - * [remove condition](https://github.com/iambumblehead/esmock/pull/253) to not-call resolver with builtin moduleId + * [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