Skip to content

Commit

Permalink
Merge pull request #146 from iambumblehead/small-changes
Browse files Browse the repository at this point in the history
use shared descriptive error message
  • Loading branch information
iambumblehead authored Sep 9, 2022
2 parents 7d81efc + b00d78c commit 34496ee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ import esmock from 'esmock'
test('should mock packages and local files', async () => {
const cookup = await esmock('../src/cookup.js', {
addpkg: (a, b) => a + b,
'#icon': {
coffee: '',
bacon: '🥓'
},
'#icon': { coffee: '', bacon: '🥓' },
'../src/breakfast.js': {
default: () => ['coffee', 'bacon'],
addSalt: meal => meal + '🧂'
Expand Down
49 changes: 20 additions & 29 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ const isObj = o => typeof o === 'object' && o
const isDefaultDefined = o => isObj(o) && 'default' in o
const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/

const esmockModuleMergeDefault = (defaultLive, defaultMock, merged) => {
const defaultLiveIsObj = isObj(defaultLive)
const defaultMockIsObj = isObj(defaultMock)

if (defaultLiveIsObj && defaultMockIsObj) {
merged = Object.assign({}, defaultLive, defaultMock)
} else if (defaultMock) {
merged = defaultMock
}
const esmockModuleIdNotFoundError = (moduleId, parent) => new Error(
`invalid moduleId: "${moduleId}" (used by :moduleParent)`
.replace(/:moduleParent/, parent
.replace(/^\/\//, '')
.replace(process.cwd(), '.')
.replace(process.env.HOME, '~')))

return merged
}
const esmockModuleMergeDefault = (defaultLive, defaultMock) => (
(isObj(defaultLive) && isObj(defaultMock))
? Object.assign({}, defaultLive, defaultMock)
: defaultMock)

const esmockModuleApply = (definitionLive, definitionMock, definitionPath) => {
const isCorePath = resolvewith.iscoremodule(definitionPath)
Expand Down Expand Up @@ -91,23 +90,20 @@ const esmockModuleImportedPurge = modulePathKey => {

const esmockNextKey = ((key = 0) => () => ++key)()

// eslint-disable-next-line max-len
const esmockModuleCreate = async (esmockKey, key, mockPathFull, mockDef, opt) => {
const isesm = esmockModuleIsESM(mockPathFull)
const originalDefinition = opt.strict || opt.isfound === false
|| await import(mockPathFull)
const mockDefinitionFinal = esmockModuleApply(
originalDefinition, mockDef, mockPathFull)
const mockExportNames = Object.keys(mockDefinitionFinal).sort().join()
const mockModuleKey = `${mockPathFull}?` + [
const esmockModuleCreate = async (esmockKey, key, fileURL, defMock, opt) => {
const isesm = esmockModuleIsESM(fileURL)
const defLive = opt.strict || opt.isfound === false || await import(fileURL)
const def = esmockModuleApply(defLive, defMock, fileURL)
const mockExportNames = Object.keys(def).sort().join()
const mockModuleKey = `${fileURL}?` + [
'esmockKey=' + esmockKey,
'esmockModuleKey=' + key,
'isesm=' + isesm,
opt.isfound === false ? 'notfound=' + key : 'found',
mockExportNames ? 'exportNames=' + mockExportNames : 'exportNone'
].join('&')

esmockCacheSet(mockModuleKey, mockDefinitionFinal)
esmockCacheSet(mockModuleKey, def)

return mockModuleKey
}
Expand All @@ -126,13 +122,8 @@ const esmockModulesCreate = async (parent, moduleFileURL, esmockKey, defs, keys,
opt = Object.assign({ isfound: false }, opt)
}

if (!mockedPathFull) {
parent = parent
.replace(/^\/\//, '')
.replace(process.cwd(), '.')
.replace(process.env.HOME, '~')
throw new Error(`invalid moduleId: "${keys[0]}" (used by ${parent})`)
}
if (!mockedPathFull)
throw esmockModuleIdNotFoundError(keys[0], parent)

mocks.push(await esmockModuleCreate(
esmockKey, keys[0], mockedPathFull, defs[keys[0]], opt))
Expand All @@ -150,7 +141,7 @@ const esmockModuleMock = async (parent, moduleId, defs, gdefs, opt) => {
parent, moduleFileURL, esmockKey, gdefs, Object.keys(gdefs), 0, opt)

if (moduleFileURL === null)
throw new Error(`invalid moduleId: "${moduleId}"`)
throw esmockModuleIdNotFoundError(moduleId, parent)

const esmockKeyLong = moduleFileURL + '?' +
'key=:esmockKey?esmockGlobals=:esmockGlobals#-#esmockModuleKeys=:moduleKeys'
Expand Down
2 changes: 1 addition & 1 deletion tests/tests-node/esmock.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('should throw error if local file not found', async () => {
createString: () => 'test string'
}
}), {
message: 'invalid moduleId: "../local/not/found.js"'
message: /invalid moduleId: "..\/local\/not\/found.js" \(used by/
})
})

Expand Down
3 changes: 2 additions & 1 deletion tests/tests-uvu/esmock.uvu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ test('should throw error if local file not found', async () => {
}
})
} catch (e) {
assert.is(e.message, 'invalid moduleId: "../local/not/found.js"')
assert.ok(e.message.startsWith(
'invalid moduleId: "../local/not/found.js" (used by'))
}
})

Expand Down

0 comments on commit 34496ee

Please sign in to comment.