From f91a8e053444c7bb5e4159d974b165e6eb544ce6 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 13 Dec 2021 20:34:41 -0800 Subject: [PATCH 1/2] handle encoded whitespace at moduleLoader.mjs --- CHANGELOG.md | 3 +++ package.json | 4 ++-- spec/ava/esmock.ava.spec.js | 10 ++++++++++ spec/local/main.js | 4 ++++ spec/local/space in path/wild-file.js | 1 + src/esmockLoader.mjs | 6 ++++-- 6 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 spec/local/space in path/wild-file.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f9469aa1..0b544580 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # changelog + * 1.7.0 _Dec.13.2021_ + * re-use predefined regexps (faster) + * resolve encoded whitespace bug at module loader * 1.6.5 _Dec.06.2021_ * add size and downloads badges to README * edits to README diff --git a/package.json b/package.json index be561873..9a8e8224 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esmock", - "version": "1.6.6", + "version": "1.7.0", "license": "MIT", "readmeFilename": "README.md", "description": "provides native ESM import mocking for unit tests", @@ -46,7 +46,7 @@ "rewire" ], "dependencies": { - "resolvewithplus": "^0.4.2" + "resolvewithplus": "^0.5.0" }, "devDependencies": { "uvu": "0.5.2", diff --git a/spec/ava/esmock.ava.spec.js b/spec/ava/esmock.ava.spec.js index a6598923..4161d83b 100644 --- a/spec/ava/esmock.ava.spec.js +++ b/spec/ava/esmock.ava.spec.js @@ -342,3 +342,13 @@ test('should merge "default" value, when safe', async t => { t.is(mockMainA(), mockMainB()); }); + +test('should not error when mocked file has space in path', async t => { + const main = await esmock('../local/main.js', { + '../local/space in path/wild-file.js' : { + default : 'tamed' + } + }); + + t.is(main.wild, 'tamed'); +}); diff --git a/spec/local/main.js b/spec/local/main.js index c2bb28bf..7deffd9d 100644 --- a/spec/local/main.js +++ b/spec/local/main.js @@ -5,6 +5,8 @@ import { } from './mainUtilNamedExports.js'; import { readPath, readSync } from './usesCoreModule.js'; +import wildfile from './space in path/wild-file.js'; + import { createString, causeRuntimeError @@ -34,3 +36,5 @@ export default () => { ? 'main string and mocked export, ' + createString() : 'main string, ' + createString(); }; + +export const wild = wildfile; diff --git a/spec/local/space in path/wild-file.js b/spec/local/space in path/wild-file.js new file mode 100644 index 00000000..7ee35ff3 --- /dev/null +++ b/spec/local/space in path/wild-file.js @@ -0,0 +1 @@ +export default 'success'; diff --git a/src/esmockLoader.mjs b/src/esmockLoader.mjs index e644df88..930a26a8 100644 --- a/src/esmockLoader.mjs +++ b/src/esmockLoader.mjs @@ -15,11 +15,12 @@ const urlDummy = 'file:///' + path .join(path.dirname(url.fileURLToPath(import.meta.url)), 'esmock.js') .replace(/^\//, ''); +const esmockGlobalsAndAfterRe = /\?esmockGlobals=.*/; const esMockGlobalsAndBeforeRe = /.*\?esmockGlobals=/; const esmockModuleKeysRe = /#esmockModuleKeys/; const exportNamesRe = /.*exportNames=(.*)/; +const whitespaceRe = /%20/g; const esmockKeyRe = /esmockKey=\d*/; -const esmockGlobalsAndAfterRe = /\?esmockGlobals=.*/; const withHashRe = /[^#]*#/; const isesmRe = /isesm=true/; @@ -37,8 +38,9 @@ const resolve = async (specifier, context, defaultResolve) => { return defaultResolve(specifier, context, defaultResolve); const resolved = defaultResolve(specifier, context, defaultResolve); + const resolvedurl = resolved.url.replace(whitespaceRe, ' '); const moduleKeyRe = new RegExp( - '.*(' + resolved.url + '\\?' + esmockKeyParam + '[^#]*).*'); + '.*(' + resolvedurl + '\\?' + esmockKeyParam + '[^#]*).*'); const [ keyUrl, keys ] = esmockKeyLong.split(esmockModuleKeysRe); const moduleGlobals = keyUrl.replace(esMockGlobalsAndBeforeRe, ''); From 996ae9f3d97a2d04d4b361efcc7dab0c6306f15b Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 13 Dec 2021 21:15:44 -0800 Subject: [PATCH 2/2] use encodeURI, per aladdin-add --- package.json | 4 ++-- src/esmockLoader.mjs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9a8e8224..3c86bb4d 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "rewire" ], "dependencies": { - "resolvewithplus": "^0.5.0" + "resolvewithplus": "^0.5.1" }, "devDependencies": { "uvu": "0.5.2", @@ -56,7 +56,7 @@ "sinon": "^12.0.1" }, "scripts": { - "test-ava": "ava --node-arguments=\"--loader=./src/esmockLoader.mjs\" ./spec/ava/*spec.js", + "test-ava": "ava --node-arguments=\"--loader=./src/esmockLoader.mjs\" ./spec/ava/*.spec.js", "test-uvu": "node --no-warnings --loader=./src/esmockLoader.mjs ./node_modules/uvu/bin.js ./spec/uvu/", "test": "npm run test-ava && npm run test-uvu", "test-no-warn": "ava --node-arguments=\"--loader=./src/esmockLoader.mjs --no-warnings\"", diff --git a/src/esmockLoader.mjs b/src/esmockLoader.mjs index 930a26a8..1377a48f 100644 --- a/src/esmockLoader.mjs +++ b/src/esmockLoader.mjs @@ -19,7 +19,6 @@ const esmockGlobalsAndAfterRe = /\?esmockGlobals=.*/; const esMockGlobalsAndBeforeRe = /.*\?esmockGlobals=/; const esmockModuleKeysRe = /#esmockModuleKeys/; const exportNamesRe = /.*exportNames=(.*)/; -const whitespaceRe = /%20/g; const esmockKeyRe = /esmockKey=\d*/; const withHashRe = /[^#]*#/; const isesmRe = /isesm=true/; @@ -38,7 +37,7 @@ const resolve = async (specifier, context, defaultResolve) => { return defaultResolve(specifier, context, defaultResolve); const resolved = defaultResolve(specifier, context, defaultResolve); - const resolvedurl = resolved.url.replace(whitespaceRe, ' '); + const resolvedurl = decodeURI(resolved.url); const moduleKeyRe = new RegExp( '.*(' + resolvedurl + '\\?' + esmockKeyParam + '[^#]*).*');