Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

esmock

Package Overview
Dependencies
Maintainers
1
Versions
107
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esmock - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

4

package.json
{
"name": "esmock",
"version": "0.2.0",
"version": "0.3.0",
"license": "MIT",
"readmeFilename": "README.md",
"description": "esmock",
"description": "mock esm modules for unit-tests",
"author": "Chris <chris@bumblehead.com>",

@@ -8,0 +8,0 @@ "main": "src/esmockLoader.mjs",

@@ -16,3 +16,3 @@ esmock

Add the command to your package.json,
This package **must be used with "module" type packages.** Add the type to your package.json,
``` json

@@ -44,6 +44,22 @@ {

});
test('should apply third parameter "global" definitions', async t => {
const main = await esmock('./local/main.js', {
'./local/mainUtil.js' : {
exportedFunction : () => 'foobar'
}
}, {
fs : {
readFileSync : () => {
return 'this value anywhere the instance imports fs, global';
}
}
});
const tplStr = main.readTemplateFile();
t.is(tplStr, 'this value anywhere the instance imports fs, global');
});
```
### changelog

@@ -55,3 +71,5 @@

* adds support for mocking core modules such as fs and path
* 0.3.0 _Apr.10.2021_
* adds support for mocking modules 'globally' for the instance
[0]: http://www.bumblehead.com "bumblehead"

@@ -36,2 +36,50 @@ import test from 'ava';

test('should mock a module, globally', async t => {
const main = await esmock('./local/main.js', {
'./local/mainUtilNamedExports.js' : {
mainUtilNamedExportOne : 'mocked'
}
}, {
'form-urlencoded' : () => 'mock encode',
fs : {
existsSync : () => true,
readFileSync : filepath => filepath === 'checkfilepath.js'
? 'success'
: filepath
}
});
t.is(typeof main, 'function');
t.is(
main.mainDependencyUsingCoreModuleFSReadPath('checkfilepath.js'),
'success'
);
t.is(main(), 'main string and mocked export, mock encode');
});
test('should purge local and global mocks', async t => {
await esmock('./local/main.js', {
'./local/mainUtilNamedExports.js' : {
mainUtilNamedExportOne : 'mocked'
}
}, {
'form-urlencoded' : () => 'mock encode',
fs : {
existsSync : () => true,
readFileSync : filepath => filepath === 'checkfilepath.js'
? 'success'
: filepath
}
}, {
key : 999
});
const keys = Object
.keys(esmock.esmockCache.mockDefs)
.filter(key => /esmockKey=999/.test(key));
t.truthy(keys.length);
t.true(keys.every(key => esmock.esmockCache.mockDefs[key] === null));
});
test('should mock a module, many times differently', async t => {

@@ -153,1 +201,18 @@ const mainfoo = await esmock('./local/mainUtil.js', {

});
test('should apply third parameter "global" definitions', async t => {
const main = await esmock('./local/main.js', {
'./local/mainUtil.js' : {
exportedFunction : () => 'foobar'
}
}, {
fs : {
readFileSync : () => {
return 'this value anywhere the instance imports fs, global';
}
}
});
const tplStr = main.readTemplateFile();
t.is(tplStr, 'this value anywhere the instance imports fs, global');
});
import request from 'form-urlencoded';
// import m from './mainUtil.js';
import { basename } from 'path';
import {
mainUtilNamedExportOne
} from './mainUtilNamedExports.js';
import { readPath, readSync } from './usesCoreModule.js';
import { createString } from './mainUtil.js';
if (typeof basename !== 'function') {
throw new Error('import basename failed');
}
if (typeof request === 'undefined') {

@@ -9,4 +18,14 @@ throw new Error('imported definition: undefined');

export const mainDependencyUsingCoreModuleFSReadPath = path => {
return readPath(path);
};
export const readTemplateFile = path => {
return readSync(path);
};
export default () => {
return 'main string, ' + createString();
return /mocked/.test(mainUtilNamedExportOne)
? 'main string and mocked export, ' + createString()
: 'main string, ' + createString();
};

@@ -6,2 +6,5 @@ import fs from 'fs';

export const readSync = path => (
fs.readFileSync(path));
export const checkNothing = () => false;

@@ -6,18 +6,25 @@ import {

import {
esmockAddMocked,
esmockImportedModulePurge,
esmockImportedModuleSanitize
esmockModuleMock,
esmockModuleImportedPurge,
esmockModuleImportedSanitize
} from './esmockModule.js';
export default async (modulePath, mockDefs = {}) => {
import {
esmockCache
} from './esmockCache.js';
const esmock = async (modulePath, mockDefs, globalDefs, opt) => {
const calleePath = esmockPathCallee();
const modulePathKey = await esmockAddMocked(
calleePath, modulePath, mockDefs);
const modulePathKey = await esmockModuleMock(
calleePath, modulePath, mockDefs || {}, globalDefs || {}, opt || {});
const importedModule = await import(modulePathKey);
esmockImportedModulePurge(modulePathKey);
esmockModuleImportedPurge(modulePathKey);
// return importedModule;
return esmockImportedModuleSanitize(importedModule);
return esmockModuleImportedSanitize(importedModule);
};
esmock.esmockCache = esmockCache;
export default esmock;

@@ -68,3 +68,3 @@ import fs from 'fs';

// does not need to lookup default as in "esmockedValue.default"
const esmockImportedModuleSanitize = importedModule => {
const esmockModuleImportedSanitize = importedModule => {
if ('default' in importedModule

@@ -78,7 +78,10 @@ && !/boolean|string|number/.test(typeof importedModule.default)) {

const esmockImportedModulePurge = modulePathKey => modulePathKey
.replace(/.*esmockModuleKeys=(.*)/, '$1')
.split('#')
.forEach(key => esmockCacheSet(key, null));
const esmockModuleImportedPurge = modulePathKey => {
const purgeKey = key => esmockCacheSet(key, null);
const [ url, keys ] = modulePathKey.split('#esmockModuleKeys=');
String(keys).split('#').forEach(purgeKey);
String(url.split('esmockGlobals=')[1]).split('#').forEach(purgeKey);
};
const esmockNextKey = ((key = 0) => () => ++key)();

@@ -91,3 +94,5 @@

|| esmockCacheResolvedPathSet(
calleePath, modulePath, resolvewith(modulePath, calleePath, {esm: true}))
calleePath,
modulePath,
resolvewith(modulePath, calleePath, { esm : true }))
);

@@ -138,12 +143,15 @@

const esmockAddMocked = async (calleePath, modulePath, defs) => {
const esmockModuleMock = async (calleePath, modulePath, defs, gdefs, opt) => {
const pathModuleFull = esmockCacheResolvedPathGetCreate(
calleePath, modulePath);
const esmockKey = esmockNextKey();
const esmockKey = typeof opt.key === 'number' ? opt.key : esmockNextKey();
const esmockModuleKeys = await esmockModulesCreate(
calleePath, pathModuleFull, esmockKey, defs, Object.keys(defs));
const esmockCacheKey =
'file://:rootmodulepath?key=:esmockKey#esmockModuleKeys=:moduleKeys'
const esmockGlobalKeys = await esmockModulesCreate(
calleePath, pathModuleFull, esmockKey, gdefs, Object.keys(gdefs));
const esmockCacheKey = `file://${pathModuleFull}?`
+ 'key=:esmockKey?esmockGlobals=:esmockGlobals#esmockModuleKeys=:moduleKeys'
.replace(/:esmockKey/, esmockKey)
.replace(/:rootmodulepath/, pathModuleFull)
.replace(/:esmockGlobals/, esmockGlobalKeys.join('#') || 'null')
.replace(/:moduleKeys/, esmockModuleKeys.join('#'));

@@ -157,5 +165,5 @@

esmockNextKey,
esmockAddMocked,
esmockImportedModulePurge,
esmockImportedModuleSanitize
esmockModuleMock,
esmockModuleImportedPurge,
esmockModuleImportedSanitize
};

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc