require-inject
Advanced tools
Comparing version 1.3.0 to 1.3.1
21
index.js
@@ -22,19 +22,24 @@ "use strict"; | ||
function resolve(callerFilename, name) { | ||
if (/^[.][.]?\//.test(name)) { | ||
name = path.resolve(path.dirname(callerFilename), name) | ||
} | ||
return require.resolve(name) | ||
} | ||
var installGlobally = module.exports.installGlobally = function (toLoad, mocks) { | ||
var callerFilename = caller() == module.filename ? caller(2) : caller(); | ||
// Inject all of our mocks | ||
Object.keys(mocks).forEach(function(name){ | ||
var path = require.resolve(name) | ||
var namePath = resolve(callerFilename, name) | ||
if (mocks[name] == null) { | ||
delete require.cache[path] | ||
delete require.cache[namePath] | ||
} | ||
else { | ||
require.cache[path] = {exports: mocks[name]} | ||
require.cache[namePath] = {exports: mocks[name]} | ||
} | ||
}) | ||
var callerFilename = caller() == module.filename ? caller(2) : caller(); | ||
if (/^[.][.]?\//.test(toLoad)) { | ||
toLoad = path.resolve(path.dirname(callerFilename), toLoad) | ||
} | ||
var toLoadPath = require.resolve(toLoad) | ||
var toLoadPath = resolve(callerFilename, toLoad) | ||
@@ -41,0 +46,0 @@ // remove any unmocked version previously loaded |
{ | ||
"name": "require-inject", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "A simple mock injector compatible needing no instrumentation in the libraries being tested", | ||
@@ -23,3 +23,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tap": "^0.4.13" | ||
"tap": "^2.2.0" | ||
}, | ||
@@ -26,0 +26,0 @@ "dependencies": { |
@@ -27,3 +27,6 @@ require-inject | ||
*module* is the name of the module you want to require. | ||
*module* is the name of the module you want to require. This is what you'd | ||
pass to `require` to load the module from your script. This means that for | ||
relative paths, the path should be relative to your test script, not to the | ||
thing you're injecting dependencies into. | ||
@@ -30,0 +33,0 @@ *mocks* is an object with keys that are the names of the modules you want |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7054
12
112
45
2