From 71a40a078d38e35b187fa1f9e37057c69eccb5ca Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 19 Apr 2021 11:35:14 -0700 Subject: [PATCH] throw error if mocked module path not found --- README.md | 2 ++ package-lock.json | 4 ++-- package.json | 2 +- spec/esmock.spec.js | 11 +++++++++++ src/esmockModule.js | 4 ++++ 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7beba185..798c18cb 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ test('should do global instance mocks —third parameter', async t => { ### changelog + * 0.3.6 _Apr.19.2021_ + * throw error if mocked module path is not found * 0.3.5 _Apr.18.2021_ * added gitlab actions npm test: node 12.x, 14.x and 15.x * 0.3.3 _Apr.13.2021_ diff --git a/package-lock.json b/package-lock.json index b25bb71a..a654b27d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "esmock", - "version": "0.3.1", + "version": "0.3.5", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "0.3.1", + "version": "0.3.5", "license": "MIT", "dependencies": { "resolvewithplus": "^0.2.0" diff --git a/package.json b/package.json index 8cd8d924..c6741bb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esmock", - "version": "0.3.5", + "version": "0.3.6", "license": "MIT", "readmeFilename": "README.md", "description": "mock esm modules for unit-tests", diff --git a/spec/esmock.spec.js b/spec/esmock.spec.js index 2ae4eb2f..e99b5e72 100644 --- a/spec/esmock.spec.js +++ b/spec/esmock.spec.js @@ -24,6 +24,17 @@ test('should mock a local file', async t => { t.is(main(), 'main string, test string'); }); +test('should throw error if local file not found', async t => { + await t.throwsAsync(() => esmock('./local/not/found.js', { + './local/mainUtil.js' : { + createString : () => 'test string' + } + }), { + message : 'modulePath not found: "./local/not/found.js"' + }); +}); + + test('should mock a module', async t => { const main = await esmock('./local/mainUtil.js', { 'form-urlencoded' : () => 'mock encode' diff --git a/src/esmockModule.js b/src/esmockModule.js index 436b3917..7dcfd302 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -145,6 +145,10 @@ const esmockModuleMock = async (calleePath, modulePath, defs, gdefs, opt) => { const esmockGlobalKeys = await esmockModulesCreate( calleePath, pathModuleFull, esmockKey, gdefs, Object.keys(gdefs)); + if (pathModuleFull === null) { + throw new Error(`modulePath not found: "${modulePath}"`); + } + const esmockCacheKey = `file://${pathModuleFull}?` + 'key=:esmockKey?esmockGlobals=:esmockGlobals#esmockModuleKeys=:moduleKeys' .replace(/:esmockKey/, esmockKey)