From 79addf7aba794bebe8262155c0f2faa8631d84c6 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 27 Oct 2021 21:10:49 -0700 Subject: [PATCH 1/2] add load definition to module loader --- src/esmockLoader.mjs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/esmockLoader.mjs b/src/esmockLoader.mjs index 9861f78f..c43a1d6b 100644 --- a/src/esmockLoader.mjs +++ b/src/esmockLoader.mjs @@ -40,6 +40,7 @@ export async function resolve (specifier, context, defaultResolve) { return resolved; } +// supported by node version less than 16.12 export async function getSource (url, context, defaultGetSource) { if (/#esmockModuleKeys/gi.test(url)) // parent of mocked modules return defaultGetSource(url, context, defaultGetSource); @@ -62,3 +63,29 @@ export async function getSource (url, context, defaultGetSource) { return defaultGetSource(url, context, defaultGetSource); } + +export async function load (url, context, defaultGetSource) { + if (/#esmockModuleKeys/gi.test(url)) // parent of mocked modules + return defaultGetSource(url, context, defaultGetSource); + + [ url ] = url.split('?esmockGlobals='); + if (url.startsWith(urlDummy)) { + url = url.replace(/[^#]*#/, ''); + } + + const exportedNames = /exportNames=/.test(url) && + url.replace(/.*exportNames=(.*)/, '$1').split(','); + if (exportedNames) { + return { + format : 'module', + source : exportedNames.map(name => name === 'default' + ? `export default global.esmockCacheGet("${url}").default` + : `export const ${name} = global.esmockCacheGet("${url}").${name}` + ).join('\n') + }; + } + + return defaultGetSource(url, context, defaultGetSource); +} + + From 8cc538c7a1f80a20ffea86161848dc8c442a27d9 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 27 Oct 2021 21:16:46 -0700 Subject: [PATCH 2/2] update readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d302e92c..2ddacf99 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,11 @@ test('should mock modules and local files at same time', async t => { ### changelog + * 0.4.2 _Oct.27.2021_ + * export 'load' hook from moduleLoader, required by node v16.12.0+ * 0.4.1 _Oct.10.2021_ - * version bump, increment devDependencies + * version bump, increment devDependencies, + * major improvement to READE, thanks @swivelgames * 0.4.0 _Sep.07.2021_ * do not runtime error when returuning type '[object Module]' default * 0.3.9 _May.05.2021_