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
Copy file name to clipboardExpand all lines: README.md
+80-5Lines changed: 80 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -7,15 +7,21 @@ rewire allows you to modify the behaviour of modules for better unit testing. Yo
7
7
- provide mocks for other modules
8
8
- leak private variables
9
9
- override variables within the module
10
-
- inject scripts
10
+
- injectyour ownscripts
11
11
12
-
rewire does**not** load the file and eval it to emulate node's require mechanism. In fact it uses node's require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).
12
+
rewire does**not** load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).
-*{!String}**filename***: Path to the module that shall be rewired. Use it exactly like require().
100
+
-*{Object}**mocks***: An object with mocks. Keys should be the exactly 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}```.
101
+
-*{Object|String}**injections***: 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: {...}}``` will cause all calls of```console.log()``` to throw an exception if they're executed during```require()```.
102
+
-*{Array<String>}**leaks***: An array with variable names that should be exported. These variables are accessible via```myModule.__```
103
+
-*{Boolean=true}**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.
* This function is needed to determine the calling parent module.
7
+
* Thus rewire acts exactly the same like require() in the test module.
8
+
*
9
+
*@param {!String} request Path to the module that shall be rewired. Use it exactly like require().
10
+
*@param {Object} mocks An object with mocks. Keys should be the exactly 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}.
11
+
*@param {Object} injections If you pass an object, all keys of the object will be vars 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: {...}} will cause all calls of console.log() to throw an exception if they're executed during require().
12
+
*@param {Array} leaks An array with variable names that should be exported. These variables are accessible via myModule.__
13
+
*@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.