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

proxyquire

Package Overview
Dependencies
Maintainers
2
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

proxyquire - npm Package Compare versions

Comparing version 1.7.4 to 1.7.5

test/samples/cache/bar.js

14

lib/proxyquire.js

@@ -6,2 +6,4 @@ 'use strict';

, Module = require('module')
, resolve = require('resolve')
, dirname = require('path').dirname
, ProxyquireError = require('./proxyquire-error')

@@ -184,3 +186,13 @@ , is = require('./is')

} else {
delete require.cache[Module._resolveFilename(path, module)];
var id = Module._resolveFilename(path, module);
var stubIds = Object.keys(stubs).map(function (stubPath) {
return resolve.sync(stubPath, {
basedir: dirname(id)
})
});
var ids = [id].concat(stubIds);
ids.forEach(function (id) {
delete require.cache[id];
});
}

@@ -187,0 +199,0 @@

5

package.json
{
"name": "proxyquire",
"version": "1.7.4",
"version": "1.7.5",
"description": "Proxies nodejs require in order to allow overriding dependencies during testing.",

@@ -33,4 +33,5 @@ "main": "index.js",

"fill-keys": "^1.0.2",
"module-not-found-error": "^1.0.0"
"module-not-found-error": "^1.0.0",
"resolve": "~1.1.7"
}
}

@@ -56,2 +56,29 @@ # proxyquire [![Build Status](https://secure.travis-ci.org/thlorenz/proxyquire.svg)](http://travis-ci.org/thlorenz/proxyquire)

You can also replace functions directly:
```js
var get = require('simple-get');
var assert = require('assert');
module.exports = function fetch (callback) {
get('https://api/users', callback);
};
```
```js
var proxyquire = require('proxyquire');
var fetch = proxyquire('./get', {
'simple-get': function (url, callback) {
process.nextTick(function () {
callback(null, fakeResponse)
})
}
});
fetch(function (err, res) {
assert(res.statusCode, 200)
});
```
<!-- START doctoc generated TOC please keep comment here to allow auto update -->

@@ -58,0 +85,0 @@ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

@@ -84,3 +84,22 @@ /*jshint asi:true*/

});
it('deletes the require.cache for the stubs', function() {
var proxyquire = require('..').noPreserveCache();
var bar = {};
var foo = proxyquire.load('./samples/cache/foo', { './bar': bar });
bar.f.g = function () { return 'a' };
bar.h = function () { return 'a' };
assert.equal(foo.bar.f.g(), 'a')
assert.equal(foo.bar.h(), 'a')
foo = proxyquire.load('./samples/cache/foo', { './bar': {} });
assert.equal(foo.bar.h(), 'h')
assert.equal(foo.bar.f.g(), 'g')
assert.equal(undefined, require.cache[require.resolve('./samples/cache/foo')]);
assert.equal(undefined, require.cache[require.resolve('./samples/cache/bar')]);
});
});
});
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