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/local/strictest.js b/tests/local/strictest.js index a4558bb2..30d13100 100644 --- a/tests/local/strictest.js +++ b/tests/local/strictest.js @@ -1,4 +1,4 @@ -import { log } from "console"; -import { basename } from "path"; +import { log } from "console" +import { basename } from "path" -log(basename(import.meta.url)); +log(basename(import.meta.url)) diff --git a/tests/tests-node/esmock.node.test.js b/tests/tests-node/esmock.node.test.js index 388485c2..8b7904ff 100644 --- a/tests/tests-node/esmock.node.test.js +++ b/tests/tests-node/esmock.node.test.js @@ -437,14 +437,14 @@ test('should throw error when strict mock definition not found', async () => { test('should error when "strictest" called with defs: {}', async () => { await assert.rejects(async () => esmock.strictest( '../local/strictest.js', {} - )); -}); + )) +}) test('should error when "strictest" called with defs: undefined', async () => { await assert.rejects(async () => esmock.strictest( '../local/strictest.js' - )); -}); + )) +}) test('should error when "strictest" mock tree module not mocked', async () => { const strictestTree = await esmock.strictest(