Skip to content

Commit

Permalink
Merge pull request #30 from iambumblehead/throw-error-if-loader-not-used
Browse files Browse the repository at this point in the history
throw error if loader is not used
  • Loading branch information
iambumblehead authored Dec 1, 2021
2 parents fae566a + 8b07ef7 commit 64c3f5f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ test('should mock "await import()" using esmock.p', async t => {
<summary>changelog</summary>
<br/>

* 1.4.0 _Nov.30.2021_
* throw error if esmock is called without --loader=esmock
* 1.3.3 _Nov.28.2021_
* update quick-start README to include phrase 'unit test'
* 1.3.2 _Nov.27.2021_
Expand Down
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": "1.3.3",
"version": "1.4.0",
"license": "MIT",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
Expand Down
8 changes: 8 additions & 0 deletions spec/esmock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ test('should mock a local file', async t => {
t.is(main(), 'main string, test string');
});

test.serial('should throw error if !esmockloader', async t => {
esmock.esmockloader = false;
await t.throwsAsync(() => esmock('./to/module.js'), {
message : 'process must be started with --loader=esmock'
});
esmock.esmockloader = true;
});

test('should throw error if local file not found', async t => {
await t.throwsAsync(() => esmock('./local/not/found.js', {
'./local/mainUtil.js' : {
Expand Down
7 changes: 5 additions & 2 deletions src/esmock.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import {

const esmock = async (modulePath, mockDefs, globalDefs, opt = {}, err) => {
const calleePath = (err || new Error()).stack.split('\n')[2]
.replace(/^.*file:\/\//, '') // rm everything before filepathfe
.replace(/^.*file:\/\//, '') // rm every before filepath
.replace(/:[\d]*:[\d]*.*$/, '') // rm line and row number
.replace(/^.*:/, ''); // rm windows-style drive locations
.replace(/^.*:/, ''); // rm windows-style drive location

if (!esmock.esmockloader)
throw new Error('process must be started with --loader=esmock');

const modulePathKey = await esmockModuleMock(
calleePath, modulePath, mockDefs || {}, globalDefs || {}, opt);
Expand Down
1 change: 1 addition & 0 deletions src/esmockLoader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import url from 'url';

import esmock from './esmock.js';
esmock.esmockloader = true;
export default esmock;

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

0 comments on commit 64c3f5f

Please sign in to comment.