Skip to content

Commit

Permalink
Merge pull request #8 from iambumblehead/added-test-throw-error-when-…
Browse files Browse the repository at this point in the history
…mock-path-not-found

added test, throw error when mock path not found
  • Loading branch information
iambumblehead authored Apr 21, 2021
2 parents 153524f + 1ccafa7 commit d859066
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 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.7 _Apr.20.2021_
* add test, throw error if mocked module path is not found
* 0.3.6 _Apr.19.2021_
* throw error if mocked module path is not found
* 0.3.5 _Apr.18.2021_
Expand Down
132 changes: 130 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "esmock",
"version": "0.3.6",
"version": "0.3.7",
"license": "MIT",
"readmeFilename": "README.md",
"description": "mock esm modules for unit-tests",
Expand Down Expand Up @@ -53,6 +53,7 @@
"babel-eslint": "^10.1.0",
"eslint": "^7.17.0",
"form-urlencoded": "^4.2.1",
"import-local": "3.0.2",
"sinon": "^10.0.0"
},
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions spec/esmock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ test('should throw error if local file not found', async t => {
});
});

test('should throw error if local definition file not found', async t => {
await t.throwsAsync(() => esmock('./local/not/found.js', {
'./local/not/found.js' : {
createString : () => 'test string'
}
}), {
message : /not a valid path: \".\/local\/not\/found.js\" \(used by/
});
});

test('should mock a module', async t => {
const main = await esmock('./local/mainUtil.js', {
Expand Down
6 changes: 5 additions & 1 deletion src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ const esmockModulesCreate = async (pathCallee, pathModule, esmockKey, defs, keys

const mockedPathFull = esmockCacheResolvedPathGetCreate(pathCallee, keys[0]);
if (!mockedPathFull) {
throw new Error('not a valid path ' + pathCallee + ', ' + keys[0]);
pathCallee = pathCallee
.replace(/^\/\//, '')
.replace(process.cwd(), '.')
.replace(process.env.HOME, '~');
throw new Error(`not a valid path: "${keys[0]}" (used by ${pathCallee})`);
}

mocks.push(await esmockModuleCreate(
Expand Down

0 comments on commit d859066

Please sign in to comment.