Skip to content

Commit

Permalink
Merge pull request #33 from iambumblehead/reduce-qeurystring-in-filepath
Browse files Browse the repository at this point in the history
Reduce qeurystring in filepath
  • Loading branch information
iambumblehead authored Dec 2, 2021
2 parents 32adcfd + b5c7cd3 commit 435bfd6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
README.html
.tern-port
.tern-port
package-lock.json
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ test('should mock "await import()" using esmock.p', async t => {
<summary>changelog</summary>
<br/>

* 1.6.0 _Dec.02.2021_
* reduce file url length (improve readability of stacktrace)
* 1.5.0 _Dec.01.2021_
* resolve bug around error '--loader=esmock' detection
* 1.4.0 _Nov.30.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.5.0",
"version": "1.6.0",
"license": "MIT",
"readmeFilename": "README.md",
"description": "provides native ESM import mocking for unit tests",
Expand Down
14 changes: 13 additions & 1 deletion spec/esmock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ test('mocks inline `async import("name")`', async t => {
filePath : 'filePath'
}));

const [ , key ] = writeJSConfigFile.esmockKey.match(/esmockKey=(\d*)/);
const [ , key ] = writeJSConfigFile.esmockKey.match(/esmk=(\d*)/);
const keyRe = new RegExp(`esmockKey=${key}[^d]`);

const moduleKeys = Object.keys(esmock.esmockCache.mockDefs)
Expand All @@ -293,3 +293,15 @@ test('mocks inline `async import("name")`', async t => {
esmock.purge(writeJSConfigFile);
t.true(moduleKeys.every(mkey => esmock.esmockCache.mockDefs[mkey] === null));
});

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

try {
causeRuntimeError();
} catch (e) {
t.true(/\?esmk=\d/.test(e.stack.split('\n')[1]));
}

t.pass();
});
14 changes: 13 additions & 1 deletion src/esmockCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const esmockCache = {
mockDefs : {}
};

const esmockKeySet = (key, keylong) => (
global.mockKeys[String(key)] = keylong);

const esmockKeyGet = key => (
global.mockKeys[String(key)]);

const esmockCacheSet = (key, mockDef) => (
esmockCache.mockDefs[key] = mockDef);

Expand All @@ -18,12 +24,18 @@ const esmockCacheResolvedPathIsESMGet = mockPathFull => (
const esmockCacheResolvedPathIsESMSet = (mockPathFull, isesm) => (
esmockCache.isESM[mockPathFull] = isesm);

Object.assign(global, { esmockCacheGet });
Object.assign(global, {
esmockCacheGet,
esmockKeyGet,
mockKeys : {}
});

export {
esmockCache,
esmockCacheSet,
esmockCacheGet,
esmockKeySet,
esmockKeyGet,
esmockCacheResolvedPathIsESMGet,
esmockCacheResolvedPathIsESMSet
};
14 changes: 10 additions & 4 deletions src/esmockLoader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ const urlDummy = 'file:///' + path
.replace(/^\//, '');

const resolve = async (specifier, context, defaultResolve) => {
const [ esmockKeyParam ] = (context.parentURL
&& context.parentURL.match(/esmockKey=\d*/) || []);

const { parentURL } = context;
const [ esmockKeyParamSmall ] =
(parentURL && parentURL.match(/\?esmk=\d*/)) || [];
const esmockKeyLong = esmockKeyParamSmall
? global.esmockKeyGet(esmockKeyParamSmall.split('=')[1])
: parentURL;
const [ esmockKeyParam ] =
(esmockKeyLong && esmockKeyLong.match(/esmockKey=\d*/) || []);

if (!esmockKeyParam)
return defaultResolve(specifier, context, defaultResolve);

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

const moduleURLSplitKeys = context.parentURL.split('#esmockModuleKeys=');
const moduleURLSplitKeys = esmockKeyLong.split('#esmockModuleKeys=');
// eslint-disable-next-line prefer-destructuring
const moduleGlobals = moduleURLSplitKeys[0].split('?esmockGlobals=')[1];
const moduleKeyChild = moduleKeyRe.test(moduleURLSplitKeys[1])
Expand Down
13 changes: 10 additions & 3 deletions src/esmockModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from 'path';
import resolvewith from 'resolvewithplus';

import {
esmockKeySet,
esmockKeyGet,
esmockCacheSet,
esmockCacheResolvedPathIsESMGet,
esmockCacheResolvedPathIsESMSet
Expand Down Expand Up @@ -68,7 +70,7 @@ const esmockModuleIsESM = (mockPathFull, isesm) => {
// does not need to lookup default as in "esmockedValue.default"
const esmockModuleImportedSanitize = (importedModule, esmockKey) => {
const importedDefault = 'default' in importedModule && importedModule.default;

if (!/boolean|string|number/.test(typeof importedDefault)) {
// an example of [object Module]: import * as mod from 'fs'; export mod;
return Object.prototype.toString.call(importedDefault) === '[object Module]'
Expand All @@ -81,7 +83,8 @@ const esmockModuleImportedSanitize = (importedModule, esmockKey) => {

const esmockModuleImportedPurge = modulePathKey => {
const purgeKey = key => key === 'null' || esmockCacheSet(key, null);
const [ url, keys ] = modulePathKey.split('#esmockModuleKeys=');
const longKey = esmockKeyGet(modulePathKey.split('esmk=')[1]);
const [ url, keys ] = longKey.split('#esmockModuleKeys=');

String(keys).split('#').forEach(purgeKey);
String(url.split('esmockGlobals=')[1]).split('#').forEach(purgeKey);
Expand Down Expand Up @@ -148,11 +151,15 @@ const esmockModuleMock = async (calleePath, modulePath, defs, gdefs, opt) => {
if (pathModuleFull === null)
throw new Error(`modulePath not found: "${modulePath}"`);

return pathAddProtocol(pathModuleFull, 'file:///') + '?'
const esmockKeyLong = pathAddProtocol(pathModuleFull, 'file:///') + '?'
+ 'key=:esmockKey?esmockGlobals=:esmockGlobals#esmockModuleKeys=:moduleKeys'
.replace(/:esmockKey/, esmockKey)
.replace(/:esmockGlobals/, esmockGlobalKeys.join('#') || 'null')
.replace(/:moduleKeys/, esmockModuleKeys.join('#'));

esmockKeySet(String(esmockKey), esmockKeyLong);

return pathAddProtocol(pathModuleFull, 'file:///') + `?esmk=${esmockKey}`;
};

export {
Expand Down

0 comments on commit 435bfd6

Please sign in to comment.