From b00d78cfb78f8d0fb14404f92439bf942619697e Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 9 Sep 2022 05:09:29 -0700 Subject: [PATCH] use shared descriptive error message --- README.md | 5 +-- src/esmockModule.js | 49 ++++++++++++---------------- tests/tests-node/esmock.node.test.js | 2 +- tests/tests-uvu/esmock.uvu.spec.js | 3 +- 4 files changed, 24 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 4e65f708..bea24971 100644 --- a/README.md +++ b/README.md @@ -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 + '🧂' diff --git a/src/esmockModule.js b/src/esmockModule.js index 0b053e9b..e5331ee3 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -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) @@ -91,15 +90,12 @@ 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, @@ -107,7 +103,7 @@ const esmockModuleCreate = async (esmockKey, key, mockPathFull, mockDef, opt) => mockExportNames ? 'exportNames=' + mockExportNames : 'exportNone' ].join('&') - esmockCacheSet(mockModuleKey, mockDefinitionFinal) + esmockCacheSet(mockModuleKey, def) return mockModuleKey } @@ -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)) @@ -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' diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index b3c8cd1b..24fb2b0c 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -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/ }) }) diff --git a/tests/tests-uvu/esmock.uvu.spec.js b/tests/tests-uvu/esmock.uvu.spec.js index 85baf828..560b1899 100644 --- a/tests/tests-uvu/esmock.uvu.spec.js +++ b/tests/tests-uvu/esmock.uvu.spec.js @@ -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')) } })