From 8b07ef7cf63f61c2d1e03975189acc9b9475ebab Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 30 Nov 2021 19:15:14 -0800 Subject: [PATCH] throw error if loader is not used --- README.md | 2 ++ package.json | 2 +- spec/esmock.spec.js | 8 ++++++++ src/esmock.js | 7 +++++-- src/esmockLoader.mjs | 1 + 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2c8a398a..565427ce 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ test('should mock "await import()" using esmock.p', async t => { changelog
+ * 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_ diff --git a/package.json b/package.json index 36ffa301..675f191f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/spec/esmock.spec.js b/spec/esmock.spec.js index 22d2ab92..a547ad87 100644 --- a/spec/esmock.spec.js +++ b/spec/esmock.spec.js @@ -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' : { diff --git a/src/esmock.js b/src/esmock.js index 4ce79ff0..f065c023 100644 --- a/src/esmock.js +++ b/src/esmock.js @@ -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); diff --git a/src/esmockLoader.mjs b/src/esmockLoader.mjs index 9d22f052..1019866e 100644 --- a/src/esmockLoader.mjs +++ b/src/esmockLoader.mjs @@ -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,