Skip to content

Commit

Permalink
Merge pull request #42 from iambumblehead/handle-encoded-whitespace-a…
Browse files Browse the repository at this point in the history
…t-moduleLoader

handle encoded whitespace at moduleLoader.mjs
  • Loading branch information
iambumblehead authored Dec 14, 2021
2 parents 91b2570 + 996ae9f commit 855ccb9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -46,7 +46,7 @@
"rewire"
],
"dependencies": {
"resolvewithplus": "^0.4.2"
"resolvewithplus": "^0.5.1"
},
"devDependencies": {
"uvu": "0.5.2",
Expand All @@ -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\"",
Expand Down
10 changes: 10 additions & 0 deletions spec/ava/esmock.ava.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
4 changes: 4 additions & 0 deletions spec/local/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,3 +36,5 @@ export default () => {
? 'main string and mocked export, ' + createString()
: 'main string, ' + createString();
};

export const wild = wildfile;
1 change: 1 addition & 0 deletions spec/local/space in path/wild-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'success';
5 changes: 3 additions & 2 deletions src/esmockLoader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ 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 esmockKeyRe = /esmockKey=\d*/;
const esmockGlobalsAndAfterRe = /\?esmockGlobals=.*/;
const withHashRe = /[^#]*#/;
const isesmRe = /isesm=true/;

Expand All @@ -37,8 +37,9 @@ const resolve = async (specifier, context, defaultResolve) => {
return defaultResolve(specifier, context, defaultResolve);

const resolved = defaultResolve(specifier, context, defaultResolve);
const resolvedurl = decodeURI(resolved.url);
const moduleKeyRe = new RegExp(
'.*(' + resolved.url + '\\?' + esmockKeyParam + '[^#]*).*');
'.*(' + resolvedurl + '\\?' + esmockKeyParam + '[^#]*).*');

const [ keyUrl, keys ] = esmockKeyLong.split(esmockModuleKeysRe);
const moduleGlobals = keyUrl.replace(esMockGlobalsAndBeforeRe, '');
Expand Down

0 comments on commit 855ccb9

Please sign in to comment.