Skip to content

Commit

Permalink
add support for custom resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Oct 12, 2023
1 parent dd97845 commit 4427f0a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,23 @@ 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 = resolvewith(id, parent)
const fileURL = esmockCustomResolvers(id, parent, opt.resolvers)
|| resolvewith(id, parent)
if (!fileURL && opt.isModuleNotFoundError !== false && id !== 'import')
throw esmockErr.errModuleIdNotFound(id, parent)

Expand All @@ -125,7 +134,8 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => {
}

const esmockModule = async (moduleId, parent, defs, gdefs, opt) => {
const moduleFileURL = resolvewith(moduleId, parent)
const moduleFileURL = esmockCustomResolvers(moduleId, parent, opt.resolvers)
|| resolvewith(moduleId, parent)
if (!moduleFileURL)
throw esmockErr.errModuleIdNotFound(moduleId, parent)

Expand Down
7 changes: 7 additions & 0 deletions tests/local/customResolverChild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const isMocked = false
const isCustomResolverChild = true

export default {
isCustomResolverChild,
isMocked
}
5 changes: 5 additions & 0 deletions tests/local/customResolverParent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import child from 'RESOLVECUSTOM'

export {
child
}
39 changes: 39 additions & 0 deletions tests/tests-node/esmock.node.loader-custom.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import path from 'path'
import test from 'node:test'
import assert from 'node:assert/strict'
import module from 'node:module'
import esmock from 'esmock'

function resolverCustom (moduleId, parent) {
return /RESOLVECUSTOM$/.test(moduleId) && new URL(
path.resolve(
new URL(parent).pathname,
'../../local/customResolverChild.js'),
parent
).href
}

async function resolve (specifier, context, next) {
return next(
specifier === 'RESOLVECUSTOM'
? resolverCustom(specifier, context.parentURL)
: specifier, context)
}

module.register(`
data:text/javascript,
import path from 'node:path';
${encodeURIComponent(resolverCustom)}
export ${encodeURIComponent(resolve)}`.slice(1))

test('should use custom resolver', async () => {
const customResolverParent = await esmock(
'../local/customResolverParent.js', {}, {
RESOLVECUSTOM: ({ isMocked: true })
}, {
resolvers: [resolverCustom]
})

assert.ok(customResolverParent.child.isCustomResolverChild)
assert.ok(customResolverParent.child.isMocked)
})
30 changes: 29 additions & 1 deletion tests/tests-node/esmock.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from 'node:assert/strict'
import esmock from 'esmock'
import sinon from 'sinon'
import esmockCache from '../../src/esmockCache.js'

/*
test('should mock node:process', async () => {
// has direct and in-direct calls to `process.cwd()`
const thingBeingTested = await esmock('../local/usesNodeProcess.js', {}, {
Expand Down Expand Up @@ -536,3 +536,31 @@ test('should mock scoped package, @aws-sdk/client-s3 (deep)', async () => {
assert.strictEqual(scopedClientS3.mocked, 'mock client')
})
*/
test('should use custom resolver', async () => {
const customResolverParent = await esmock(
'../local/customResolverParent.js', {}, {
'form-urlencoded': ({ isMocked: true })
}, {
resolvers: [
(moduleId, parent) => {
const pathParent = new URL(parent).pathname
const pathChild = path.resolve(
pathParent, '../../local/customResolverChild.js')

if (/CUSTOMLOADER$/.test(moduleId)) {
console.log('resolved path', new URL(pathChild, parent).href)
}

return /CUSTOMLOADER$/.test(moduleId)
? new URL(pathChild, parent).href
// ? path.join(path.dirname(parent), '../local/customResolverChild.js')
: null
}
]
})

console.log('child', customResolverParent.child)
assert.ok(customResolverParent.child.isCustomResolverChild)
assert.ok(customResolverParent.child.isMocked)
})

0 comments on commit 4427f0a

Please sign in to comment.