Socket
Socket
Sign inDemoInstall

rewire

Package Overview
Dependencies
1
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.1.1

2

lib/index.js

@@ -26,2 +26,4 @@ "use strict"; // run code in ES5 strict mode

rewire.reset = rewireModule.reset;
module.exports = rewire;

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

getLeakingSrc = require("./getLeakingSrc.js"),
getInjectionSrc = require("./getInjectionSrc.js");
getInjectionSrc = require("./getInjectionSrc.js"),
rewiredModules = [];

@@ -17,3 +18,3 @@ function restoreOriginalWrappers() {

*/
module.exports = function doRewire(parentModule, filename, mocks, injections, leaks, cache) {
function rewire(parentModule, filename, mocks, injections, leaks, cache) {
var testModule,

@@ -63,2 +64,3 @@ nodeRequire,

require.cache[filename] = testModule;
rewiredModules.push(filename); // save in private cache for .reset()
}

@@ -69,2 +71,14 @@

return testModule.exports;
};
}
rewire.reset = function () {
var i;
for (i = 0; i < rewiredModules.length; i++) {
delete require.cache[rewiredModules[i]];
}
rewiredModules = [];
};
module.exports = rewire;

2

package.json
{
"name" : "rewire",
"version" : "0.1.0",
"version" : "0.1.1",
"description" : "Dependency injection for node.js applications",

@@ -5,0 +5,0 @@ "keywords" : [

@@ -21,3 +21,3 @@ rewire

```npm install rewire```
`npm install rewire`

@@ -49,4 +49,5 @@ -----------------------------------------------------------------

// The rewired module will now use your mocks instead of fs and moduleB.js.
// Just make sure that the path is exactly as in myModuleA.js required.
// The rewired module will now use your mocks instead of fs
// and moduleB.js. Just make sure that the path is exactly as
// in myModuleA.js required.
rewiredModule = rewire("./myModuleA.js", mocks);

@@ -75,3 +76,4 @@

// You can also pass a script to inject
rewiredModule = rewire("./myModuleA.js", null, "console.log('hellooo');"); // prints "hellooo"
rewiredModule =
rewire("./myModuleA.js", null, "console.log('hellooo');"); // prints "hellooo"

@@ -94,5 +96,11 @@

// By disabling the module cache the rewired module will not be cached.
// Any later require()-calls within other modules will now return the original
// module again instead of the rewired. Caching is enabled by default.
rewire("./myModuleA.js", null, null, null, false) !== require("./myModuleA.js"); // = true
// Any require()-calls will now return the original module again instead
// of the rewired. Caching is enabled by default.
rewire("./myModuleA.js", null, null, null, false) !==
require("./myModuleA.js"); // = true
// This removes all rewired modules from require.cache.
rewire.reset();
// IMPORTANT: You should call this before every unit test to ensure
// a clean test environment.
```

@@ -104,10 +112,26 @@

**rewire(***filename, mocks, injections, leaks, cache***)**
**rewire(***filename, mocks, injections, leaks, cache***)**
- *{!String} filename*: Path to the module that shall be rewired. Use it exactly like require().
- *{Object} mocks (optional)*: An object with mocks. Keys should be the exactly the same like they're required in the target module. So if you write ```require("../../myModules/myModuleA.js")``` you need to pass ```{"../../myModules/myModuleA.js": myModuleAMock}```.
- *{Object|String} injections (optional)*: If you pass an object, all keys of the object will be ```var```s within the module. You can also eval a string. **Please note**: All scripts are injected at the end of the module. So if there is any code in your module that is executed during ```require()```, your injected variables will be undefined at this point. For example: passing ```{console: null}``` will cause all calls of ```console.log()``` to throw an exception if they're executed during ```require()```.
- *{Array&lt;String&gt;} leaks (optional)*: An array with variable names that should be exported. These variables are accessible via ```myModule.__```
- *{Boolean=true} cache (optional)*: 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.
Returns the rewired module.
- *{!String} filename*: <br/>
Path to the module that shall be rewired. Use it exactly like require().
- *{Object} mocks (optional)*: <br/>
An object with mocks. Keys should be the exactly the same like they're required in the target module. So if you write `require("../../myModules/myModuleA.js")` you need to pass `{"../../myModules/myModuleA.js": myModuleAMock}`.
- *{Object|String} injections (optional)*: <br />
If you pass an object, all keys of the object will be `var`s within the module. You can also eval a string. **Please note**: All scripts are injected at the end of the module. So if there is any code in your module that is executed during `require()`, your injected variables will be undefined at this point. For example: passing `{console: null}` will cause all calls of `console.log()` to throw an exception if they're executed during `require()`.
- *{Array&lt;String&gt;} leaks (optional)*: <br/>
An array with variable names that should be exported. These variables are accessible via `myModule.__`
- *{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.
**rewire.reset()**
Removes all rewired modules from `require.cache`. Every `require()` will now return the original module again. <br />**Please note:** You should call this before every unit test to ensure a clean test environment.
-----------------------------------------------------------------

@@ -114,0 +138,0 @@

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

describe("#getMonkeyPatchSrc", function () {
describe("getMonkeyPatchSrc", function () {
it("should return ''", function () {

@@ -9,0 +9,0 @@ var expectedSrc = "",

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

describe("#getLeakingWrapper", function () {
describe("getLeakingWrapper", function () {
it("should return 'exports.__ = {};'", function () {

@@ -9,0 +9,0 @@ expect(getLeakingWrapper([])).to.be("exports.__ = {};");

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

function cleanRequireCache() {

@@ -24,3 +23,3 @@ var i;

describe("#rewire", function () {
describe("rewire", function () {
beforeEach(cleanRequireCache); // ensuring a clean test environment

@@ -125,2 +124,11 @@ it("should work like require() when omitting all other params", function () {

});
describe("#reset", function () {
it("should remove all rewired modules from cache", function () {
var rewired = rewire("./testModules/B/moduleB.js");
expect(require("./testModules/B/moduleB.js")).to.be(rewired);
rewire.reset();
expect(require("./testModules/B/moduleB.js")).not.to.be(rewired);
});
});
});

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