Socket
Socket
Sign inDemoInstall

rewire

Package Overview
Dependencies
0
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.3.2 to 1.0.0

CHANGELOG.md

2

lib/getImportGlobalsSrc.js

@@ -6,3 +6,3 @@ /**

* Returns something like
* "var console = console; var process = process; ..."
* "var console = global.console; var process = global.process; ..."
*

@@ -9,0 +9,0 @@ * @return {String}

@@ -1,2 +0,2 @@

var rewireModule;
var rewireModule = require("./internalRewire.js");

@@ -8,27 +8,16 @@ /**

* @param {!String} filename Path to the module that shall be rewired. Use it exactly like require().
* @param {Boolean} cache Indicates whether the rewired module should be cached by node so subsequent calls of require() will return the rewired module. Subsequent calls of rewire() will always overwrite the cache.
* @return {*} the rewired module
*/
function rewire(filename, cache) {
if (cache === undefined) {
cache = true;
}
return rewireModule(module.parent, filename, cache);
function rewire(filename) {
return rewireModule(module.parent, filename);
}
// Conditional require for different environments
if (process.title === "browser") {
module.exports = require("./browserify/browserifyRewire.js");
} else {
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date
rewire.reset = rewireModule.reset;
rewire.bundlers = {
browserify: require("./bundlers/browserify/browserifyMiddleware.js"),
webpack: require("./bundlers/webpack/configureWebpack.js")
};
// Putting (require) within brackets is a hack to disable browserify's require sniffing
// @see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
rewireModule = (require)("./internalRewire.js");
module.exports = rewire;
rewire.reset = rewireModule.reset;
rewire.browserify = (require)("./browserify/browserifyMiddleware.js");
module.exports = rewire;
}
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date

@@ -9,4 +9,3 @@ var Module = require("module"),

moduleWrapper0 = Module.wrapper[0], // caching original wrapper
moduleWrapper1 = Module.wrapper[1], // caching original wrapper
rewiredModules = []; // cache for all rewired modules so it can be reset anytime
moduleWrapper1 = Module.wrapper[1]; // caching original wrapper

@@ -21,3 +20,3 @@ function restoreOriginalWrappers() {

*/
function internalRewire(parentModulePath, targetPath, cache) {
function internalRewire(parentModulePath, targetPath) {
var testModule,

@@ -30,3 +29,3 @@ nodeRequire,

/**
* Proxies the first require call in order to draw back all changes.
* Proxies the first require call in order to draw back all changes to the Module.wrapper.
* Thus our changes don't influence other modules

@@ -37,4 +36,4 @@ *

function requireProxy(path) {
restoreOriginalWrappers(); // we need to restore the wrappers now so we don't influence other modules
testModule.require = nodeRequire; // restoring original nodeRequire
restoreOriginalWrappers();
testModule.require = nodeRequire;
return nodeRequire.call(testModule, path); // node's require only works when "this" points to the module

@@ -87,8 +86,2 @@ }

// Store the rewired module in the cache when enabled
if (cache) {
rewiredModules.push(targetPath); // save in private cache for .reset()
require.cache[targetPath] = testModule;
}
// This is only necessary if nothing has been required within the module

@@ -100,15 +93,2 @@ restoreOriginalWrappers();

/**
* Deletes all rewired modules from the cache
*/
internalRewire.reset = function () {
var i;
for (i = 0; i < rewiredModules.length; i++) {
delete require.cache[rewiredModules[i]];
}
rewiredModules = [];
};
module.exports = internalRewire;
{
"name" : "rewire",
"version" : "0.3.2",
"version" : "1.0.0",
"description" : "Dependency injection for node.js applications",

@@ -33,5 +33,6 @@ "keywords" : [

"devDependencies": {
"mocha": "1.3.x",
"expect.js": "0.1.x",
"browserify": ">=1.13.5 <1.14.x"
"mocha": "1.x",
"expect.js": "0.x",
"browserify": "1.x",
"webpack": "0.x"
},

@@ -38,0 +39,0 @@ "scripts" : {

@@ -7,3 +7,3 @@ rewire

- introduce mocks for other modules
- inject mocks for other modules
- leak private variables

@@ -14,7 +14,4 @@ - override variables within the module.

**Debugging is fully supported.**
Furthermore rewire comes also with support for various client-side bundlers (see [below](#client-side-bundlers)).
Furthermore rewire comes also with support for [browserify](https://github.com/substack/node-browserify). You just
have to add rewire as a middleware (see below).
[![Build Status](https://secure.travis-ci.org/jhnns/rewire.png?branch=master)](http://travis-ci.org/jhnns/rewire)

@@ -29,16 +26,2 @@

**For older node versions:**<br />
rewire is tested with node 0.6.x - 0.8.x. I recommend to run the unit tests via `mocha` in the rewire-folder before
using rewire with other node versions.
**Use with [browserify](https://github.com/substack/node-browserify):**<br />
```javascript
var b = require("browserify")({debug: true});
b.use(require("rewire").browserify);
```
After that rewire works exactly as in node.
<br />

@@ -57,2 +40,3 @@

// Just with one difference:
// Your module will now export a special setter and getter for private variables.

@@ -75,6 +59,2 @@ myModule.__set__("myPrivateVar", 123);

// All later requires will now return the module with the mock.
myModule === require("./myModule.js"); // = true
// You can set different variables with one call.

@@ -104,23 +84,5 @@ myModule.__set__({

// By getting private variables you can test for instance if your
// module is in a specific state
assert.ok(myModule.__get__("currentState") === "idle");
// You can also disable caching when loading the rewired module. All
// subsequent calls of require() will than return the original module again.
rewire("./myModule.js") === require("./myModule.js"); // = true
rewire("./myModule.js", false) === require("./myModule.js"); // = false
// Every call of rewire returns a new instance and overwrites the old
// one in the module cache.
// There is another difference to require:
// Every call of rewire() returns a new instance.
rewire("./myModule.js") === rewire("./myModule.js"); // = false
// If you want to remove all your rewired modules from
// cache just call rewire.reset().
// Do this after every single unit test to ensure a clean testing environment.
rewire.reset();
```

@@ -132,37 +94,43 @@

**rewire(***filename, cache***): {RewiredModule}**
###rewire(filename): rewiredModule
- *{!String} filename*: <br/>
- *filename*: <br/>
Path to the module that shall be rewired. Use it exactly like require().
- *{Boolean=true} cache (optional)*: <br />
Indicates whether the rewired module should be cached by node so subsequent calls of `require()` will
return the rewired module. Further calls of `rewire()` will always overwrite the cache.
###rewiredModule.&#95;&#95;set&#95;&#95;(name, value)
**rewire.reset()**
- *name*: <br/>
Name of the variable to set. The variable should be global or defined with `var` in the top-leve scope of the module.
- *value*: <br/>
The value to set.
Removes all rewired modules from `require.cache`. Every `require()` will now return the original module again.
###rewiredModule.&#95;&#95;set&#95;&#95;(env)
- *env*: <br/>
Takes all keys as variable names and sets the values respectively.
**RewiredModule.&#95;&#95;set&#95;&#95;(***name, value***)**
###rewiredModule.&#95;&#95;get&#95;&#95;(name): value
- *{!String} name*: <br/>
Name of the variable to set. The variable should be a global or defined with `var` in the top-level
scope of the module.
Returns the private variable.
- *{&lowast;} value*: <br/>
The value to set
<br />
**RewiredModule.&#95;&#95;set&#95;&#95;(***env***)**
##Client-Side Bundlers
Since rewire relies heavily on node's require mechanism it can't be used on the client-side without adding special middleware to the bundling process. Currently supported bundlers are:
- *{!Object} env*: <br/>
Takes all keys as variable names and sets the values respectively.
- [browserify](https://github.com/substack/node-browserify)
- [webpack](https://github.com/webpack/webpack)
**RewiredModule.&#95;&#95;get&#95;&#95;(***name***): {&lowast;}**
###browserify
Returns the private variable.
```javascript
var b = browserify();
b.use(require("rewire").bundlers.browserify);
```
<br />
###webpack
## Credits
```javascript
var options = {};
This module is inspired by the great [injectr](https://github.com/nathanmacinnes/injectr "injectr")-module.
require("rewire").bundlers.webpack(options);
```

@@ -5,3 +5,3 @@ // Don't run code in ES5 strict mode.

describe("internalRewire (node.js)", function () {
describe("internalRewire", function () {
before(require("./testHelpers/createFakePackageJSON.js"));

@@ -8,0 +8,0 @@ after(require("./testHelpers/removeFakePackageJSON.js"));

@@ -7,3 +7,3 @@ "use strict"; // run code in ES5 strict mode

env = "bla",
fs = require("fs");
fs;

@@ -10,0 +10,0 @@ // We need getters and setters for private vars to check if our injected setters and getters actual work

@@ -7,3 +7,3 @@ "use strict"; // run code in ES5 strict mode

env = "bla",
fs = require("fs");
fs;

@@ -10,0 +10,0 @@ // We need getters and setters for private vars to check if our injected setters and getters actual work

@@ -9,11 +9,2 @@ // Don't run code in ES5 strict mode.

var testModules = {
A: path.resolve(__dirname, "./moduleA.js"),
B: path.resolve(__dirname, "./moduleB.js"),
someOtherModule: path.resolve(__dirname, "./someOtherModule.js"),
emptyModule: path.resolve(__dirname, "./emptyModule.js"),
strictModule: path.resolve(__dirname, "./strictModule.js")
};
function checkForTypeError(err) {

@@ -23,22 +14,28 @@ expect(err.constructor).to.be(TypeError);

function cleanRequireCache() {
var moduleName,
modulePath;
describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + ")"), function () {
it("should work like require()", function () {
rewire("./moduleA.js").getFilename();
require("./moduleA.js").getFilename();
expect(rewire("./moduleA.js").getFilename()).to.eql(require("./moduleA.js").getFilename());
expect(rewire("../testModules/someOtherModule.js").filename).to.eql(require("../testModules/someOtherModule.js").filename);
});
it("should return a fresh instance of the module", function () {
var someOtherModule = require("./someOtherModule.js"),
rewiredSomeOtherModule;
for (moduleName in testModules) {
if (testModules.hasOwnProperty(moduleName)) {
modulePath = testModules[moduleName];
delete require.cache[modulePath];
}
}
}
someOtherModule.fs = "This has been modified";
rewiredSomeOtherModule = rewire("./someOtherModule.js");
expect(rewiredSomeOtherModule.fs).not.to.be("This has been modified");
});
it("should not cache the rewired module", function () {
var rewired,
someOtherModule = require("./someOtherModule.js");
describe("rewire " + (typeof window === "undefined"? "(node.js)": "(browser)"), function () {
afterEach(cleanRequireCache); // ensuring a clean test environment
it("should work like require()", function () {
expect(rewire("./moduleA.js")).to.be(require("./moduleA.js"));
cleanRequireCache();
expect(rewire("../testModules/moduleA.js")).to.be(require("../testModules/moduleA.js"));
cleanRequireCache();
expect(rewire("./moduleA.js")).to.be(require("./moduleA.js"));
someOtherModule.fs = "This has been changed";
rewired = rewire("./someOtherModule.js");
expect(someOtherModule).not.to.be(rewired);
expect(require("./moduleA.js").someOtherModule).not.to.be(rewired);
expect(require("./moduleA.js").someOtherModule).to.be(someOtherModule);
expect(require("./moduleA.js").someOtherModule.fs).to.be("This has been changed");
});

@@ -58,4 +55,2 @@ it("should modify the module so it provides a __set__ - function", function () {

expect(require("./someOtherModule.js").__get__).to.be(undefined);
expect(require("fs").__set__).to.be(undefined);
expect(require("fs").__get__).to.be(undefined);
});

@@ -156,25 +151,5 @@ it("should not override/influence global objects by default", function () {

});
it("should cache the rewired module", function () {
var rewired;
rewired = rewire("./someOtherModule.js");
expect(require("./moduleA.js").someOtherModule).to.be(rewired);
cleanRequireCache();
rewired = rewire("./someOtherModule.js", true);
expect(require("./moduleA.js").someOtherModule).to.be(rewired);
});
it("should not cache the rewired module on demand", function () {
var rewired,
someOtherModule = require("./someOtherModule.js");
someOtherModule.fs = "This has been changed";
rewired = rewire("./someOtherModule.js", false);
expect(require("./moduleA.js").someOtherModule).not.to.be(rewired);
expect(require("./moduleA.js").someOtherModule.fs).to.be("This has been changed");
});
it("should not influence the original require if nothing has been required within the rewired module", function () {
rewire("./emptyModule.js"); // nothing happens here because emptyModule doesn't require anything
expect(require("./moduleA.js").__set__).to.be(undefined); // if restoring the original node require didn't worked, the module would have a setter
});

@@ -191,22 +166,2 @@ it("subsequent calls of rewire should always return a new instance", function () {

});
it("should return a fresh instance of the module", function () {
var someOtherModule = require("./someOtherModule.js"),
rewiredSomeOtherModule;
someOtherModule.fs = "This has been modified";
rewiredSomeOtherModule = rewire("./someOtherModule.js");
expect(rewiredSomeOtherModule.fs).not.to.be("This has been modified");
});
describe("#reset", function () {
it("should remove all rewired modules from cache", function () {
var rewiredModuleA = rewire("./moduleA.js"),
rewiredModuleB = rewire("./moduleB.js");
expect(require("./moduleA.js")).to.be(rewiredModuleA);
expect(require("./moduleB.js")).to.be(rewiredModuleB);
rewire.reset();
expect(require("./moduleA.js")).not.to.be(rewiredModuleA);
expect(require("./moduleB.js")).not.to.be(rewiredModuleB);
});
});
});
"use strict"; // run code in ES5 strict mode
__filename = "/test/testModules/someOtherModule.js"; // unifying filename for the pretty stack trace test
__filename = "/test/testModules/someOtherModule.js";
var fs = require("fs");
exports.fs = fs;
exports.fs = {};
exports.filename = __filename;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc