Skip to content

Commit

Permalink
Merge pull request #34 from iambumblehead/resolve-bug-no-loader-no-error
Browse files Browse the repository at this point in the history
update files add test
  • Loading branch information
iambumblehead authored Dec 3, 2021
2 parents 435bfd6 + 658d562 commit c761b99
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ esmock

**esmock provides native ESM import mocking for unit tests.** Use the examples below as a quick-start guide or use a [descriptive and more friendly esmock guide here.][10]


[10]: https://github.com/iambumblehead/esmock/wiki/How-to-use-esmock
[10]: https://github.com/iambumblehead/esmock/wiki
[0]: http://www.bumblehead.com "bumblehead"
[1]: https://github.com/iambumblehead/esmock/workflows/nodejs-ci/badge.svg "nodejs-ci pipeline"
[2]: https://github.com/iambumblehead/esmock "esmock"
Expand All @@ -27,8 +26,8 @@ esmock
`esmock` has the following signature
``` javascript
await esmock(
'./to/module.js', // path to the target module being tested
{ ...childmocks }, // mocked definitions imported by the target module
'./to/module.js', // path to target module being tested
{ ...childmocks }, // mocked definitions imported by target module
{ ...globalmocks } // mocked definitions imported everywhere else
);
```
Expand Down Expand Up @@ -66,12 +65,12 @@ test('should do global instance mocks —third parameter', async t => {
});

test('should mock "await import()" using esmock.p', async t => {
// when esmock.p is used, mock definitions are not deleted from cache
const doAwaitImport = await esmock.p('../src/awaitImportEslint.mjs', {
// using esmock.p, mock definitions are not deleted from cache
const doAwaitImport = await esmock.p('../src/awaitImportLint.js', {
eslint : { ESLint : config => config }
});

// the cached definition is there when import is called
// cached mock definition is there when import is called
t.is(await doAwaitImport('config'), 'config');

esmock.purge(doAwaitImport); // clear the cache, if you wish
Expand All @@ -83,6 +82,10 @@ test('should mock "await import()" using esmock.p', async t => {
<summary>changelog</summary>
<br/>

* 1.6.1 _Dec.03.2021_
* adds test verifying deep stacktrace has small path file:///
* resolve bug, '--loader=esmoc' not-found error not thrown
* small README edits, update link to wiki (use Home as default)
* 1.6.0 _Dec.02.2021_
* reduce file url length (improve readability of stacktrace)
* 1.5.0 _Dec.01.2021_
Expand Down
22 changes: 22 additions & 0 deletions spec/esmock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,25 @@ test('should have small querystring in stacktrace filename', async t => {

t.pass();
});

test('should have small querystring in stacktrace filename, deep', async t => {
const {
causeRuntimeErrorFromImportedFile
} = await esmock('./local/main.js', {}, {
'./local/mainUtil.js' : {
causeRuntimeError : () => {
t.nonexistantmethod();
}
}
});

try {
causeRuntimeErrorFromImportedFile();
} catch (e) {
t.true(
e.stack.split('\n')
.every(line => !line.includes('?') || /\?esmk=\d/.test(line)));
}

t.pass();
});
8 changes: 7 additions & 1 deletion spec/local/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
} from './mainUtilNamedExports.js';
import { readPath, readSync } from './usesCoreModule.js';

import { createString } from './mainUtil.js';
import {
createString,
causeRuntimeError
} from './mainUtil.js';

if (typeof basename !== 'function') {
throw new Error('import basename failed');
Expand All @@ -23,6 +26,9 @@ export const readTemplateFile = path => {
return readSync(path);
};

export const causeRuntimeErrorFromImportedFile = () => (
causeRuntimeError());

export default () => {
return /mocked/.test(mainUtilNamedExportOne)
? 'main string and mocked export, ' + createString()
Expand Down
7 changes: 6 additions & 1 deletion src/esmockLoader.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import process from 'process';
import path from 'path';
import url from 'url';

import esmock from './esmock.js';
global.esmockloader = true;

if (process.execArgv.some(args => (
args.startsWith('--loader=') && args.includes('esmock'))))
global.esmockloader = true;

export default esmock;

// ex, file:///path/to/esmock,
Expand Down

0 comments on commit c761b99

Please sign in to comment.