Skip to content

Commit

Permalink
change interface to use { resolver } rather than { resolvers }
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Oct 12, 2023
1 parent 63a4a3b commit e3e8051
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/esmockArgs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import resolver from 'resolvewithplus'

// extracts path or fileurl from stack,
// ' at <anonymous> (/root/test.js:11:31)' -> /root/test.js
// ' at Object.handler (file:///D:/a/test.js:11:31)' -> file:///D:/a/test.js
Expand All @@ -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
}
14 changes: 2 additions & 12 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +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]
mocks = mocks || []

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)

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

Expand Down
17 changes: 14 additions & 3 deletions tests/tests-node/esmock.node.resolver-custom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit e3e8051

Please sign in to comment.