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

proxyquire

Package Overview
Dependencies
Maintainers
1
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.2.0 to 1.3.0

test/proxyquire-remove.js

9

lib/proxyquire.js

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

for (var key in stubs) {
if (stubs[key] === null) continue;
if (stubs[key].hasOwnProperty('@global')) {

@@ -145,2 +147,9 @@ this._containsGlobal = true;

if (stub === null) {
// Mimic the module-not-found exception thrown by node.js.
var notfound = new Error("Cannot find module '" + path + "'");
notfound.code = 'MODULE_NOT_FOUND';
throw notfound;
}
if (stub.hasOwnProperty('@noCallThru') ? !stub['@noCallThru'] : !this._noCallThru) {

@@ -147,0 +156,0 @@ fillMissingKeys(stub, Module._load(path, module));

2

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

@@ -5,0 +5,0 @@ "main": "index.js",

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

- [All together, now](#all-together-now)
- [Using proxyquire to simulate the absence of Modules](#using-proxyquire-to-simulate-the-absence-of-modules)
- [Forcing proxyquire to reload modules](#forcing-proxyquire-to-reload-modules)

@@ -165,2 +166,29 @@ - [Globally override require](#globally-override-require)

## Using proxyquire to simulate the absence of Modules
Some libraries may behave differently in the presence of absence of a
package, for example:
```javascript
var cluster;
try {
cluster = require('cluster');
} catch(e) {
// cluster module is not present.
cluster = null
}
if (cluster) {
// Then provide some functionality for a cluster-aware version of Node.js
} else {
// and some alternative for a cluster-unaware version.
}
```
To exercise the second branch of the `if` statement, you can make proxyquire pretend the package isn't present by
setting the stub for it to `null`. This works even if a `cluster` module is actually present.
```javascript
var foo = proxyquire('./foo', { cluster: null });
```
## Forcing proxyquire to reload modules

@@ -167,0 +195,0 @@

@@ -7,7 +7,11 @@ var stats = require('./stats')

, path = require('path')
, crypto
;
// Require if present.
try { crypto = require('crypto'); } catch (e) { crypto = 'caught'; }
stats.incFooRequires();
function bigBar () {
function bigBar () {
// inline require

@@ -30,2 +34,6 @@ return require('./bar').bar().toUpperCase();

function bigCrypto () {
return crypto;
}
module.exports = {

@@ -39,3 +47,4 @@ bigBar: bigBar

, foobool: foobool
, bigCrypto: bigCrypto
, state: ''
};

Sorry, the diff of this file is not supported yet

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