Comparing version 0.1.1 to 0.1.2
@@ -13,4 +13,4 @@ var path = require('path'); | ||
* | ||
* @param {Object} obj | ||
* @param {string} key | ||
* @param {Object|string} obj | ||
* @param {!string|Object} key | ||
* @param {!Function} method | ||
@@ -20,3 +20,7 @@ */ | ||
if (typeof obj === 'string') { | ||
return mockRequire(obj, key, module.parent.filename); | ||
if (key) { | ||
return mockRequire(obj, key); | ||
} else { | ||
return refresh(obj); | ||
} | ||
} else { | ||
@@ -49,17 +53,10 @@ mockMethod(obj, key, method); | ||
* @param {string} filename | ||
* @param {Object} module | ||
* @param {string} parentFilename | ||
* @param {Object} stub | ||
*/ | ||
function mockRequire(filename, module, parentFilename) { | ||
// check if `filename` is relative | ||
if (filename[0] === '.') { | ||
var i = parentFilename.lastIndexOf(sep); | ||
filename = path.resolve(parentFilename.slice(0, i), filename); | ||
} | ||
function mockRequire(filename, stub) { | ||
filename = resolve(filename); | ||
var existed = !!require.cache[filename]; | ||
var resolvedFilename = require.resolve(filename); | ||
var existed = !!require.cache[resolvedFilename]; | ||
requireMocks.push({ | ||
filename: resolvedFilename, | ||
filename: filename, | ||
existed: existed, | ||
@@ -69,3 +66,3 @@ original: require(filename) | ||
return require.cache[resolvedFilename].exports = module; | ||
return require.cache[filename].exports = stub; | ||
} | ||
@@ -75,2 +72,33 @@ | ||
/** | ||
* Deletes module from cache and requires it again. | ||
* | ||
* @param {string} filename | ||
* @return {Object} | ||
*/ | ||
function refresh(filename) { | ||
filename = resolve(filename); | ||
delete require.cache[filename]; | ||
return require(filename); | ||
} | ||
/** | ||
* Resolve the path to a module. | ||
* | ||
* @parma {string} filename | ||
* @return {string} | ||
*/ | ||
function resolve(filename) { | ||
// check if `filename` is relative | ||
if (filename[0] === '.') { | ||
var parentFilename = module.parent.filename; | ||
var i = parentFilename.lastIndexOf(sep); | ||
filename = path.resolve(parentFilename.slice(0, i), filename); | ||
} | ||
return require.resolve(filename); | ||
} | ||
/** | ||
* Restore all mocks | ||
@@ -93,1 +121,6 @@ */ | ||
}; | ||
// delete this module from the cache so that the next time it gets | ||
// require()'d it will be aware of the new parent | ||
delete require.cache[require.resolve(__filename)]; |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["test", "mock", "dependency"], | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -19,2 +19,11 @@ # muk [![Build Status](https://secure.travis-ci.org/fent/node-muk.png)](http://travis-ci.org/fent/node-muk) | ||
**foo.js** | ||
``` | ||
var request = require('request'); | ||
module.exports = function foo(url) { | ||
// do something with request | ||
}; | ||
``` | ||
```js | ||
@@ -26,8 +35,10 @@ var mockedRequest = function(url, options, callback) { | ||
require('request', mockedRequest); | ||
console.log(mockedRequest === require('request')); // true | ||
var foo = muk('./foo'); | ||
``` | ||
Only userland modules dependencies can be mocked. You can also mock modules required with a relative path. | ||
Only userland modules dependencies can be mocked. When muk() is called with one string argument, it will delete that module from the cache and return the `require()`d object, refreshing the module. | ||
You can also mock modules required with a relative path. | ||
```js | ||
@@ -34,0 +45,0 @@ muk('./foo', 'hey!!'); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
9756
13
220
72
11
1