Comparing version 0.1.0 to 0.2.0
{ | ||
"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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20195
17
485
55
0
2