You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Path to the module that shall be rewired. Use it exactly like require().
114
115
115
116
-*{Object} mocks (optional)*: <br/>
116
-
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}`.
117
+
An object with mocks.
117
118
118
119
-*{Object|String} injections (optional)*: <br />
119
-
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()`.
120
+
If you pass an object, all keys of the object will be`var`s within the module. You can also eval a string.
120
121
121
122
-*{Array<String>} leaks (optional)*: <br/>
122
-
An array with variable names that should be exported. These variables are accessible via`myModule.__`
123
-
123
+
An array with variable names that should be exported. These variables are accessible via`myModule.__`.
124
124
125
125
-*{Boolean=true} cache (optional)*: <br />
126
-
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.
126
+
Indicates whether the rewired module should be cached by node so subsequent calls of`require()` will
127
+
return the rewired module. Further calls of`rewire()` will always overwrite the cache.
128
+
129
+
Returns the rewired module.
127
130
128
131
**rewire.reset()**
129
132
130
-
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.
133
+
Removes all rewired modules from`require.cache`. Every`require()` will now return the original module again.
console.log("Hello");// ouch, that won't work. console is undefined at this point because of hoisting
152
+
153
+
// End of module ///////////////
154
+
// rewire will inject here
155
+
varconsole=null;
156
+
```
157
+
158
+
###leaks
159
+
Leaks are executed at the end of the module. If a`var` is undefined at this point you
160
+
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)).
161
+
A good approach to this is:
162
+
163
+
```javascript
164
+
var myLeaks= {};
165
+
166
+
module.exports=function (someValue) {
167
+
myLeaks.someValue= someValue;
168
+
};
169
+
170
+
// End of module ///////////////
171
+
// rewire will inject here
172
+
module.exports.__= {myLeaks: myLeaks};
173
+
```
174
+
175
+
Because```myLeaks``` is defined at the end of the module, you're able to access the leak object and all leaks that
176
+
are attached to it later during runtime. Because myLeaks is not exposed under regular circumstances your
177
+
module interface stays clean.
178
+
179
+
###reset
180
+
You should call this before every unit test to ensure a clean test environment.