mock-require
Advanced tools
Comparing version 1.1.0 to 1.2.0
45
index.js
@@ -10,9 +10,7 @@ var Module = require('module') | ||
Module._load = function(request, parent) { | ||
var fullFilePath = Module._resolveFilename(request, parent); | ||
var fullFilePath = getFullPath(request, parent.filename); | ||
if (!intercept.hasOwnProperty(fullFilePath)) { | ||
return originalLoader.apply(this, arguments); | ||
} | ||
return intercept[fullFilePath]; | ||
return intercept.hasOwnProperty(fullFilePath) | ||
? intercept[fullFilePath] | ||
: originalLoader.apply(this, arguments); | ||
}; | ||
@@ -40,24 +38,29 @@ | ||
function getFullPath(path, calledFrom) { | ||
var needsFullPath = true | ||
, resolvedPath | ||
, isExternal | ||
; | ||
var resolvedPath; | ||
try { | ||
resolvedPath = require.resolve(path); | ||
isExternal = resolvedPath.indexOf('/node_modules/') !== -1; | ||
} catch(e) { } | ||
needsFullPath = resolvedPath !== path && !isExternal; | ||
var isExternal = /[/\\]node_modules[/\\]/.test(resolvedPath); | ||
var isSystemModule = resolvedPath === path; | ||
if (isExternal || isSystemModule) { | ||
return resolvedPath; | ||
} | ||
if (isExternal) { | ||
path = resolvedPath; | ||
} | ||
} catch(e) { } | ||
var isLocalModule = /^\.{1,2}[/\\]/.test(path); | ||
if (!isLocalModule) { | ||
return path; | ||
} | ||
if (needsFullPath) { | ||
path = join(dirname(calledFrom), path); | ||
path = Module._resolveFilename(path); | ||
var localModuleName = join(dirname(calledFrom), path); | ||
try { | ||
return Module._resolveFilename(localModuleName); | ||
} catch (e) { | ||
if (isModuleNotFoundError(e)) { return localModuleName; } | ||
else { throw e; } | ||
} | ||
} | ||
return path; | ||
function isModuleNotFoundError(e){ | ||
return e.code && e.code === 'MODULE_NOT_FOUND' | ||
} | ||
@@ -64,0 +67,0 @@ |
{ | ||
"name": "mock-require", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Simple, intuitive mocking of Node.js modules.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -109,2 +109,82 @@ var assert = require('assert') | ||
(function shouldRegisterMockForExternalModuleThatIsNotFound() { | ||
mock('a', {id: 'a'}); | ||
assert.equal(require('a').id, 'a'); | ||
mock.stopAll(); | ||
})(); | ||
(function shouldRegisterMultipleMocksForExternalModulesThatAreNotFound() { | ||
mock('a', {id: 'a'}); | ||
mock('b', {id: 'b'}); | ||
mock('c', {id: 'c'}); | ||
assert.equal(require('a').id, 'a'); | ||
assert.equal(require('b').id, 'b'); | ||
assert.equal(require('c').id, 'c'); | ||
mock.stopAll(); | ||
})(); | ||
(function shouldRegisterMockForLocalModuleThatIsNotFound() { | ||
mock('./a', {id: 'a'}); | ||
assert.equal(require('./a').id, 'a'); | ||
mock.stopAll(); | ||
})(); | ||
(function shouldRegisterMockForLocalModuleThatIsNotFound_2() { | ||
mock('../a', {id: 'a'}); | ||
assert.equal(require('../a').id, 'a'); | ||
mock.stopAll(); | ||
})(); | ||
(function shouldRegisterMockForLocalModuleThatIsNotFoundAtCorrectPath() { | ||
mock('./x', {id: 'x'}); | ||
assert.equal(require('./nested/module-c').dependentOn.id, 'x'); | ||
mock.stopAll(); | ||
})(); | ||
(function shouldRegisterMultipleMocksForLocalModulesThatAreNotFound() { | ||
mock('./a', {id: 'a'}); | ||
mock('./b', {id: 'b'}); | ||
mock('./c', {id: 'c'}); | ||
assert.equal(require('./a').id, 'a'); | ||
assert.equal(require('./b').id, 'b'); | ||
assert.equal(require('./c').id, 'c'); | ||
mock.stopAll(); | ||
})(); | ||
(function shouldUnRegisterMockForModuleThatIsNotFound() { | ||
var moduleName = 'module-that-is-not-installed'; | ||
mock(moduleName, {mocked: true}); | ||
mock.stop(moduleName); | ||
try{ | ||
require(moduleName) | ||
} catch (e) { | ||
assert.equal(e.code, 'MODULE_NOT_FOUND') | ||
} | ||
})(); | ||
(function shouldLoadMockedExternalModuleWhenLocalModuleHasSameName() { | ||
mock('module-a', {id: 'external-module-a'}); | ||
var b = require('./module-b') | ||
assert.equal(b.dependentOn.id, 'local-module-a') | ||
assert.equal(b.dependentOn.dependentOn.id, 'external-module-a') | ||
mock.stopAll(); | ||
})(); | ||
console.log('All tests pass!'); |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
11522
15
219
10