require-inject
Advanced tools
Comparing version 1.1.0 to 1.1.1
27
index.js
@@ -5,10 +5,21 @@ "use strict"; | ||
module.exports = function (toLoad, mocks) { | ||
// Copy the existing cache | ||
var originalCache = {} | ||
Object.keys(require.cache).forEach(function(name) { | ||
originalCache[name] = require.cache[name] | ||
}) | ||
// Inject all of our mocks | ||
Object.keys(mocks).forEach(function(name){ | ||
var path = require.resolve(name) | ||
require.cache[path] = {exports: mocks[name]} | ||
if (mocks[name] == null) { | ||
delete require.cache[path] | ||
} | ||
else { | ||
require.cache[path] = {exports: mocks[name]} | ||
} | ||
}) | ||
if (/^[.][.]?\//.test(toLoad)) { | ||
toLoad = path.resolve(path.dirname(module.parent.filename), toLoad) | ||
toLoad = path.resolve(path.dirname(module.parent.filename), toLoad) | ||
} | ||
@@ -19,12 +30,12 @@ var toLoadPath = require.resolve(toLoad) | ||
delete require.cache[toLoadPath] | ||
// load our new version using our mocks | ||
var mocked = module.parent.require(toLoadPath) | ||
// remove our version from the cache so anyone else gets the real thing | ||
delete require.cache[toLoadPath] | ||
// Remove our injected mocks | ||
Object.keys(mocks).forEach(function(name){ | ||
delete require.cache[require.resolve(name)] | ||
}) | ||
// restore the cache, we can't just assign originalCache to require.cache as the require | ||
// object is unique to each module, even though require.cache is shared | ||
Object.keys(require.cache).forEach(function(name){ delete require.cache[name] }) | ||
Object.keys(originalCache).forEach(function(name){ require.cache[name] = originalCache[name] }) | ||
return mocked | ||
} |
{ | ||
"name": "require-inject", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A simple mock injector compatible needing no instrumentation in the libraries being tested", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "tap test/*.js" | ||
}, | ||
@@ -21,3 +21,6 @@ "author": "Rebecca Turner <me@re-becca.org> (http://re-becca.org)", | ||
}, | ||
"license": "ISC" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"tap": "^0.4.13" | ||
} | ||
} |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
4159
9
79
1
1