Skip to content

Commit

Permalink
Merge pull request #7 from iambumblehead/throw-error-if-mocked-path-n…
Browse files Browse the repository at this point in the history
…ot-found

throw error if mocked module path not found
  • Loading branch information
iambumblehead authored Apr 19, 2021
2 parents e25a555 + 71a40a0 commit 153524f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 11 additions & 0 deletions spec/esmock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 153524f

Please sign in to comment.