rewire
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -7,3 +7,3 @@ "use strict"; // run code in ES5 strict mode | ||
* e.g.: | ||
* "exports.__ = {myPrivateVar: myPrivateVar};" | ||
* "module.exports.__ = {myPrivateVar: myPrivateVar};" | ||
* | ||
@@ -14,3 +14,3 @@ * @param {Array<String>} leaks | ||
function getLeakingSrc(leaks) { | ||
var src = "exports.__ = {", | ||
var src = "module.exports.__ = {", | ||
varName, | ||
@@ -17,0 +17,0 @@ i; |
{ | ||
"name" : "rewire", | ||
"version" : "0.1.1", | ||
"version" : "0.1.2", | ||
"description" : "Dependency injection for node.js applications", | ||
@@ -28,3 +28,3 @@ "keywords" : [ | ||
"engines" : { | ||
"node" : "0.6.x" | ||
"node" : ">=0.6.15" | ||
}, | ||
@@ -31,0 +31,0 @@ "dependencies": { |
@@ -16,3 +16,8 @@ rewire | ||
##Important note: | ||
rewire works currently only with node >=0.6.15. In doubt run `npm test`. | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -25,2 +30,3 @@ Installation | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -85,9 +91,13 @@ Examples | ||
// rewire exports variables under the special "__"-object. | ||
// This will inject | ||
// module.exports._ = {myPrivateVar1: myPrivateVar1, myPrivateVar2: myPrivateVar2} | ||
// at the bottom of the module. | ||
rewiredModule = rewire("./myModuleA.js", null, null, leaks); | ||
rewiredModule.__.myPrivateVar1; // returns former private myPrivateVar1 | ||
rewiredModule.__.myPrivateVar2; // returns former private myPrivateVar2 | ||
// You now can access your private varialbes under the special __-object | ||
rewiredModule.__.myPrivateVar1; // returns former private variable myPrivateVar1 | ||
rewiredModule.__.myPrivateVar2; // returns former private variable myPrivateVar2 | ||
// Cache | ||
@@ -108,2 +118,3 @@ //////////////////////////////// | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -114,4 +125,2 @@ ##API | ||
Returns the rewired module. | ||
- *{!String} filename*: <br/> | ||
@@ -121,20 +130,70 @@ 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}`. | ||
An object with mocks. | ||
- *{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()`. | ||
If you pass an object, all keys of the object will be `var`s within the module. You can also eval a string. | ||
- *{Array<String>} leaks (optional)*: <br/> | ||
An array with variable names that should be exported. These variables are accessible via `myModule.__` | ||
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. | ||
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. | ||
**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. | ||
Removes all rewired modules from `require.cache`. Every `require()` will now return the original module again. | ||
----------------------------------------------------------------- | ||
<br /> | ||
## Please note | ||
### mocks | ||
Keys should be 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}`. | ||
### injections | ||
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. | ||
Imagine `rewire("./myModule.js", null, {console: null});`: | ||
```javascript | ||
console.log("Hello"); // ouch, that won't work. console is undefined at this point because of hoisting | ||
// End of module /////////////// | ||
// rewire will inject here | ||
var console = null; | ||
``` | ||
### leaks | ||
Leaks are executed at the end of the module. If a `var` is undefined at this point you | ||
won't be able to access the leak (because `undefined`-values are [copied by value](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language)). | ||
A good approach to this is: | ||
```javascript | ||
var myLeaks = {}; | ||
module.exports = function (someValue) { | ||
myLeaks.someValue = someValue; | ||
}; | ||
// End of module /////////////// | ||
// rewire will inject here | ||
module.exports.__ = {myLeaks: myLeaks}; | ||
``` | ||
Because ```myLeaks``` is defined at the end of the module, you're able to access the leak object and all leaks that | ||
are attached to it later during runtime. Because myLeaks is not exposed under regular circumstances your | ||
module interface stays clean. | ||
### reset | ||
You should call this before every unit test to ensure a clean test environment. | ||
----------------------------------------------------------------- | ||
<br /> | ||
## Credits | ||
@@ -145,2 +204,3 @@ | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -147,0 +207,0 @@ ## License |
@@ -7,11 +7,11 @@ "use strict"; // run code in ES5 strict mode | ||
describe("getLeakingWrapper", function () { | ||
it("should return 'exports.__ = {};'", function () { | ||
expect(getLeakingWrapper([])).to.be("exports.__ = {};"); | ||
it("should return 'module.exports.__ = {};'", function () { | ||
expect(getLeakingWrapper([])).to.be("module.exports.__ = {};"); | ||
}); | ||
it("should return 'exports.__ = {somethingPrivate:somethingPrivate,somethingSecret:somethingSecret};'", function () { | ||
it("should return 'module.exports.__ = {somethingPrivate:somethingPrivate,somethingSecret:somethingSecret};'", function () { | ||
var leakArr = ["somethingPrivate", "somethingSecret"]; | ||
expect(getLeakingWrapper(leakArr)) | ||
.to.be("exports.__ = {somethingPrivate:somethingPrivate,somethingSecret:somethingSecret};"); | ||
.to.be("module.exports.__ = {somethingPrivate:somethingPrivate,somethingSecret:somethingSecret};"); | ||
}); | ||
}); |
@@ -73,15 +73,19 @@ "use strict"; // run code in ES5 strict mode | ||
}); | ||
it("should leak private variables", function () { | ||
it("should leak private variables with both exports-styles (exports.bla = bla and module.exports = bla)", function () { | ||
var rewired, | ||
leaks = ["myPrivateVar"]; | ||
rewired = rewire("./testModules/A/moduleA.js", null, null, leaks); | ||
rewired = rewire("./testModules/privateModules/privateModuleA.js", null, null, leaks); | ||
expect(rewired.__.myPrivateVar).to.be("Hello I'm very private"); | ||
rewired = rewire("./testModules/privateModules/privateModuleB.js", null, null, leaks); | ||
expect(rewired.__.myPrivateVar).to.be("Hello I'm very private"); | ||
}); | ||
it("should leak private functions", function () { | ||
it("should leak private functions with both exports-styles (exports.bla = bla and module.exports = bla)", function () { | ||
var rewired, | ||
leaks = ["myPrivateFunction"]; | ||
rewired = rewire("./testModules/A/moduleA.js", null, null, leaks); | ||
rewired = rewire("./testModules/privateModules/privateModuleA.js", null, null, leaks); | ||
expect(rewired.__.myPrivateFunction()).to.be("Hello I'm very private"); | ||
rewired = rewire("./testModules/privateModules/privateModuleB.js", null, null, leaks); | ||
expect(rewired.__.myPrivateFunction()).to.be("Hello I'm very private"); | ||
}); | ||
@@ -88,0 +92,0 @@ it("should leak nothing on demand", function () { |
@@ -12,8 +12,2 @@ "use strict"; // run code in ES5 strict mode | ||
var myPrivateVar = "Hello I'm very private"; | ||
function myPrivateFunction() { | ||
return "Hello I'm very private"; | ||
} | ||
function exportAll() { | ||
@@ -20,0 +14,0 @@ // expose all required modules to test for mocks |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60258
31
344
223