Skip to content

Commit

Permalink
Merge pull request #252 from iambumblehead/remove-condition-preventin…
Browse files Browse the repository at this point in the history
…g-coremoduleids-from-resolver

remove condition preventing core moduleIds from resolver
  • Loading branch information
iambumblehead authored Oct 13, 2023
2 parents d3ca335 + 1548792 commit 7f03aea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 2 additions & 7 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 \'#<Object>\''
Expand Down Expand Up @@ -111,17 +109,14 @@ 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]
mocks = mocks || []

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)

Expand All @@ -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)

Expand Down
13 changes: 8 additions & 5 deletions tests/tests-node/esmock.node.resolver-custom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\/\.\./, '')
}

Expand Down

0 comments on commit 7f03aea

Please sign in to comment.