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
**Dependency injection for node.js applications**.
4
+
5
+
rewire allows you to modify the behaviour of modules for better unit testing. You may
6
+
7
+
- provide mocks for other modules
8
+
- leak private variables
9
+
- override variables within the module
10
+
- inject scripts
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).
13
+
14
+
Installation
15
+
------------
16
+
17
+
```npm install rewire```
18
+
19
+
Examples
20
+
--------
21
+
22
+
```javascript
23
+
var rewire =require("rewire"),
24
+
rewiredModule;
25
+
26
+
// Default
27
+
////////////////////////////////
28
+
// rewire acts exactly like require when omitting all other params
29
+
rewiredModule =rewire("./myModuleA.js");
30
+
31
+
// Mocks
32
+
////////////////////////////////
33
+
var mockedModuleB = {},
34
+
mocks = {
35
+
"path/to/moduleB.js": mockedModuleB
36
+
};
37
+
38
+
// the rewired module will now use your mock instead of moduleB.js.
39
+
rewiredModule =rewire("./myModuleA.js", mocks);
40
+
41
+
42
+
// Injections
43
+
////////////////////////////////
44
+
var injections = {
45
+
console: {
46
+
log:function () { /* be quiet */ }
47
+
},
48
+
process: { argv: ["someArgs"] },
49
+
__filename:"some/other/dir"
50
+
};
51
+
52
+
// overrides all given variables within the module
0 commit comments