Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75f6a77

Browse files
committed
improved docs
1 parent dc03f51 commit 75f6a77

File tree

1 file changed

+64
-76
lines changed

1 file changed

+64
-76
lines changed

README.md

Lines changed: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ rewire
44

55
rewire allows you to modify the behaviour of modules for better unit testing. You may
66

7-
- provide mocks for other modules
7+
- introduce mocks for other modules
88
- leak private variables
99
- override variables within the module
1010
- inject your own scripts
@@ -32,83 +32,74 @@ Examples
3232
--------
3333

3434
```javascript
35-
var rewire = require("rewire"),
36-
rewiredModule;
35+
var rewire = require("rewire");
3736

38-
// Default
39-
////////////////////////////////
4037
// rewire acts exactly like require when omitting all other params
4138
rewire("./myModuleA.js") === require("./myModuleA.js"); // = true
39+
```
4240

43-
44-
45-
// Mocks
46-
////////////////////////////////
47-
var mockedModuleB = {},
48-
mockedFs = {},
49-
mocks = {
50-
"fs": mockedFs,
51-
"path/to/moduleB.js": mockedModuleB
52-
};
53-
41+
### Mocks
42+
```javascript
43+
// You can introduce your own mocks for modules that are required:
44+
rewiredModule = rewire("./myModuleA.js", {
45+
"fs": {
46+
readFile: function (path, encoding, cb) { cb(null, "Success!"); }
47+
},
48+
"../path/to/moduleB.js": myMockForModuleB
49+
});
5450
// The rewired module will now use your mocks instead of fs
5551
// and moduleB.js. Just make sure that the path is exactly as
5652
// in myModuleA.js required.
57-
rewiredModule = rewire("./myModuleA.js", mocks);
58-
59-
53+
```
6054

61-
// Injections
62-
////////////////////////////////
63-
var injections = {
64-
console: {
65-
log: function () { /* be quiet */ }
66-
},
67-
process: { argv: ["someArgs"] },
68-
__filename: "some/other/dir"
69-
};
55+
### Injections
56+
```javascript
57+
// You can inject your own mocks for internal or global objects.
58+
// These injections are only visible within the module.
59+
rewiredModule = rewire("./myModuleA.js", null, {
60+
console: {
61+
log: function () { /* be quiet */ }
62+
},
63+
process: { argv: ["some", "other", "args"] },
64+
__filename: "some/other/dir"
65+
});
7066

7167
// This will inject
7268
// var console = {log: function () { /* be quiet */ }};
73-
// var process = {argv: ["someArgs"] };
69+
// var process = {argv: ["some", "other", "args"]};
7470
// var __filename = "some/other/dir";
75-
// at the bottom of the module.
76-
// This way you can override private variables within the module
77-
rewiredModule = rewire("./myModuleA.js", null, injections);
78-
79-
// You can also pass a script to inject
80-
rewiredModule =
81-
rewire("./myModuleA.js", null, "console.log('hellooo');"); // prints "hellooo"
82-
71+
// at the end of the module.
8372

73+
// You can also pass a script to inject at the end
74+
rewiredModule = rewire("./myModuleA.js", null, "console.log('hello');");
75+
// This will print "hello" when the module loads
76+
```
8477

85-
// Leaks
86-
////////////////////////////////
87-
var leaks = ["myPrivateVar1", "myPrivateVar2"];
78+
### Leaks
79+
```javascript
80+
// You can expose private variables for unit tests
81+
rewiredModule = rewire("./myModuleA.js", null, null, ["myVar1", "myVar2");
8882

8983
// This will inject
90-
// module.exports._ = {myPrivateVar1: myPrivateVar1, myPrivateVar2: myPrivateVar2}
91-
// at the bottom of the module.
92-
rewiredModule = rewire("./myModuleA.js", null, null, leaks);
93-
94-
// You now can access your private varialbes under the special __-object
95-
rewiredModule.__.myPrivateVar1; // returns former private variable myPrivateVar1
96-
rewiredModule.__.myPrivateVar2; // returns former private variable myPrivateVar2
97-
84+
// module.exports.__ = {myVar1: myVar1, myVar2: myVar2}
85+
// at the end of the module.
9886

87+
// You can access now your private variables under the special.__-object
88+
console.log(rewiredModule.__.myVar1);
89+
console.log(rewiredModule.__.myVar2);
90+
```
9991

100-
// Cache
101-
////////////////////////////////
102-
// By disabling the module cache the rewired module will not be cached.
103-
// Any require()-calls will now return the original module again instead
104-
// of the rewired. Caching is enabled by default.
105-
rewire("./myModuleA.js", null, null, null, false) !==
106-
require("./myModuleA.js"); // = true
92+
### Cache
93+
```javascript
94+
// You can disable caching of the rewired module. Any require()-calls will
95+
// now return the original module again instead of the rewired.
96+
// Caching is enabled by default.
97+
rewire("./myModuleA.js", null, null, null, false) === require("./myModuleA.js");
98+
// = false
10799

108-
// This removes all rewired modules from require.cache.
100+
// You can also delete all rewired modules from the cache by one call.
109101
rewire.reset();
110-
// IMPORTANT: You should call this before every unit test to ensure
111-
// a clean test environment.
102+
// You should call this after every unit test to ensure a clean test environment.
112103
```
113104

114105
-----------------------------------------------------------------
@@ -122,7 +113,7 @@ rewire.reset();
122113
Path to the module that shall be rewired. Use it exactly like require().
123114

124115
- *{Object} mocks (optional)*: <br/>
125-
An object with mocks.
116+
An object with module mocks. Keys should reflect the required path of the module.
126117

127118
- *{Object|String} injections (optional)*: <br />
128119
If you pass an object, all keys of the object will be `var`s within the module. You can also eval a string.
@@ -144,14 +135,13 @@ Removes all rewired modules from `require.cache`. Every `require()` will now ret
144135
<br />
145136

146137
## Please note
147-
### mocks
148-
Keys should be exactly the same like they're required in the target module.
138+
### Keys should be exactly the same like they're required in the target module
149139
So if you write `require("../../myModules/myModuleA.js")` you need to pass
150140
`{"../../myModules/myModuleA.js": myModuleAMock}`.
151141

152-
### injections
153-
All scripts are injected at the end of the module. So if there is any code in your module
154-
that is executed during `require()`, your injected variables will be undefined at this point.
142+
### All scripts are injected at the end of the module
143+
So if there is any code in your module that is executed during `require()`, your
144+
injected variables will be undefined at this point.
155145

156146
Imagine `rewire("./myModule.js", null, {console: null});`:
157147

@@ -163,29 +153,27 @@ console.log("Hello"); // ouch, that won't work. console is undefined at this p
163153
var console = null;
164154
```
165155

166-
### leaks
167-
Leaks are executed at the end of the module. If a `var` is undefined at this point you
168-
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)).
169-
A good approach to this is:
156+
### Leaks are executed at the end of the module.
157+
All variables, that are [copied by value](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language)
158+
will not be updated anymore.
159+
160+
A good approach to solve this would be:
170161

171162
```javascript
172163
var myLeaks = {};
173164

174165
module.exports = function (someValue) {
175166
myLeaks.someValue = someValue;
176167
};
177-
178-
// End of module ///////////////
179-
// rewire will inject here
180-
module.exports.__ = {myLeaks: myLeaks};
181168
```
182169

170+
And then: ```rewire("myModuleA.js", null, null, ["myLeaks"]);```
171+
183172
Because ```myLeaks``` is defined at the end of the module, you're able to access the leak object and all leaks that
184-
are attached to it later during runtime. Because myLeaks is not exposed under regular circumstances your
185-
module interface stays clean.
173+
are attached to it later during runtime.
186174

187-
### reset
188-
You should call this before every unit test to ensure a clean test environment.
175+
### Call rewire.reset() after every unit test
176+
All ```require()```s will now return the original module again.
189177

190178
-----------------------------------------------------------------
191179
<br />

0 commit comments

Comments
 (0)