Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove condition preventing core moduleIds from resolver #252

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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
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