diff --git a/src/esmockErr.js b/src/esmockErr.js index ef016ef6..a43a590c 100644 --- a/src/esmockErr.js +++ b/src/esmockErr.js @@ -8,8 +8,12 @@ const errMissingLoader = () => new Error('the loader chain process must include esmock. ' + 'start the process using --loader=esmock.') +const errModuleIdNoDefs = (moduleId, parent) => + new Error(`no mocks provided for module: "${moduleId}" (used by ${parent}`) + export default { errModuleIdNotFound, errModuleIdNotMocked, - errMissingLoader + errMissingLoader, + errModuleIdNoDefs } diff --git a/src/esmockModule.js b/src/esmockModule.js index 8763b254..1342e6bb 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -129,12 +129,18 @@ const esmockModule = async (moduleId, parent, defs, gdefs, opt) => { if (!moduleFileURL) throw esmockErr.errModuleIdNotFound(moduleId, parent) + const gkeys = gdefs ? Object.keys(gdefs) : [] + const dkeys = defs ? Object.keys(defs) : [] + if (opt.strict === 3 && !gkeys.length && !dkeys.length) { + throw esmockErr.errModuleIdNoDefs(moduleId, parent) + } + const treeid = typeof opt.id === 'number' ? opt.id : nextId() const treeidspec = `${moduleFileURL}?key=${treeid}&strict=${opt.strict}?` + [ - 'esmkgdefs=' + (gdefs && (await esmockModuleId( - parent, treeid, gdefs, Object.keys(gdefs), opt)).join('#-#') || 0), - 'esmkdefs=', (defs && (await esmockModuleId( - parent, treeid, defs, Object.keys(defs), opt)).join('#-#') || 0) + 'esmkgdefs=' + (gkeys.length && (await esmockModuleId( + parent, treeid, gdefs, gkeys, opt)).join('#-#') || 0), + 'esmkdefs=', (dkeys.length && (await esmockModuleId( + parent, treeid, defs, dkeys, opt)).join('#-#') || 0) ].join('#-#') esmockTreeIdSet(String(treeid), treeidspec) diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index 099f29e4..728520c7 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -434,6 +434,18 @@ test('should throw error when strict mock definition not found', async () => { assert.deepEqual(pathWrapPartial.basename('/dog.png'), 'dog.png') }) +test('should error when "strictest" called with defs: {}', async () => { + await assert.rejects(async () => esmock.strictest( + '../local/importsCoreLocalAndPackage.js', {} + )) +}) + +test('should error when "strictest" called with defs: undefined', async () => { + await assert.rejects(async () => esmock.strictest( + '../local/importsCoreLocalAndPackage.js' + )) +}) + test('should error when "strictest" mock tree module not mocked', async () => { const strictestTree = await esmock.strictest( '../local/importsCoreLocalAndPackage.js', {