Skip to content

Commit

Permalink
added error if nothing is mocked at stricted level
Browse files Browse the repository at this point in the history
  • Loading branch information
Swivelgames committed Mar 15, 2023
1 parent 0ef1937 commit e473e3e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/esmockErr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 10 additions & 4 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/local/strictest.js
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 4 additions & 4 deletions tests/tests-node/esmock.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e473e3e

Please sign in to comment.