Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

muk

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

muk - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

test/dir/bar.js

65

lib/index.js

@@ -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!!');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc