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.1.0 to 0.2.0

spec/local/usesCoreModule.js

2

package.json
{
"name": "esmock",
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "readmeFilename": "README.md",

@@ -50,4 +50,6 @@ esmock

* 0.1.0 _Apr.10.2021_
* adds support for native es modules
* adds support for native esm modules
* 0.2.0 _Apr.10.2021_
* adds support for mocking core modules such as fs and path
[0]: http://www.bumblehead.com "bumblehead"

@@ -139,1 +139,14 @@ import test from 'ava';

});
test('should mock core module', async t => {
const usesCoreModule = await esmock('./local/usesCoreModule.js', {
fs : {
existsSync : () => true,
readFileSync : filepath => filepath === 'checkfilepath.js'
? 'success'
: filepath
}
});
t.is(usesCoreModule.readPath('checkfilepath.js'), 'success');
});

@@ -7,6 +7,4 @@ import fs from 'fs';

esmockCacheSet,
esmockCacheResolvedPathGet,
esmockCacheResolvedPathSet,
esmockCacheResolvedPathIsESMGet,

@@ -16,6 +14,8 @@ esmockCacheResolvedPathIsESMSet

const esmockModuleApply = (definitionLive, definitionMock) => {
const esmockModuleApply = (definitionLive, definitionMock, definitionPath) => {
const isDefaultNamespace = o => typeof o === 'object' && 'default' in o;
const definition = Object.assign(
{}, definitionLive, definitionMock);
const isCorePath = !/\//.test(definitionPath);
const definition = isCorePath
? Object.assign({ default : definitionMock }, definitionMock)
: Object.assign({}, definitionLive, definitionMock);
const isDefaultLive = isDefaultNamespace(definitionLive);

@@ -37,3 +37,3 @@ const isDefaultMock = isDefaultNamespace(definitionMock);

// is nested default, sometimes generated by babel
if ( isDefaultNamespace( definition.default ) ) {
if (isDefaultNamespace(definition.default)) {
definition.default = definitionMock;

@@ -61,4 +61,4 @@ definition.default.default = definitionMock;

isesm = esmockModuleESMRe.test(
fs.readFileSync(mockPathFull, 'utf-8'));
isesm = /^\//.test(mockPathFull)
&& esmockModuleESMRe.test(fs.readFileSync(mockPathFull, 'utf-8'));

@@ -98,6 +98,8 @@ esmockCacheResolvedPathIsESMSet(mockPathFull, isesm);

const isesm = esmockModuleIsESM(mockPathFull);
const isCorePath = !/\//.test(mockPathFull);
const mockDefinitionFinal = esmockModuleApply(
await import(mockPathFull), mockDef);
await import(mockPathFull), mockDef, mockPathFull);
const mockModuleKey = `file://${mockPathFull}?` + [
const mockModuleProtocol = isCorePath ? 'node:' : 'file://';
const mockModuleKey = `${mockModuleProtocol}${mockPathFull}?` + [
'esmockKey=' + esmockKey,

@@ -104,0 +106,0 @@ 'esmockModuleKey=' + key,

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