2
2
=====
3
3
** Dependency injection for node.js applications** .
4
4
5
- rewire allows you to modify the behaviour of modules for better unit testing. You may
5
+ rewire adds a special setter and getter that allow you to modify the behaviour of modules
6
+ for better unit testing. You may
6
7
7
8
- introduce mocks for other modules
8
9
- leak private variables
9
- - override variables within the module
10
- - inject your own scripts
10
+ - override variables within the module.
11
11
12
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).
13
13
@@ -22,8 +22,7 @@ Installation
22
22
` npm install rewire `
23
23
24
24
** For older node versions:** <br />
25
- rewire is tested with node 0.6.x. I recommend to run the unit tests via` mocha ` in the rewire-folder before
26
- using rewire with older node versions.
25
+ rewire is tested with node 0.6.x. I recommend to run the unit tests via` mocha ` in the rewire-folder before using rewire with older node versions.
27
26
28
27
-----------------------------------------------------------------
29
28
<br />
@@ -34,146 +33,115 @@ Examples
34
33
``` javascript
35
34
var rewire= require (" rewire" );
36
35
37
- // rewire acts exactly like require when omitting all other params
38
- rewire (" ./myModuleA.js" )=== require (" ./myModuleA.js" );// = true
39
- ```
40
36
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
37
+ // rewire acts exactly like require.
38
+ var myRewiredModule= rewire (" ../lib/myModule.js" );
39
+ myRewiredModule=== require (" ../lib/myModule.js" );// = true
40
+
41
+
42
+ // Your module will now export a special setter and getter for private variables.
43
+ myModule .__set__ (" myPrivateVar" ,123 );
44
+ myModule .__get__ (" myPrivateVar" );// = 123
45
+
46
+
47
+ // This allows you to mock almost everything within the module e.g. the fs-module.
48
+ // Just pass the variable name as first parameter and your mock as second.
49
+ myModule .__set__ (" fs" , {
50
+ readFile : function (path ,encoding ,cb ) {
51
+ cb (null ," Success!" );
52
+ }
53
+ });
54
+ myModule .readSomethingFromFileSystem (function (err ,data ) {
55
+ console .log (data);// = Success!
49
56
});
50
- // The rewired module will now use your mocks instead of fs
51
- // and moduleB.js. Just make sure that the path is exactly as
52
- // in myModuleA.js required.
53
- ```
54
57
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 , {
58
+
59
+ // All later requires will now return the module with the mock.
60
+ myModule=== require (" ./myModule.js" );// = true
61
+
62
+
63
+ // You can set different variables with one call.
64
+ myModule .__set__ ({
65
+ fs: fsMock,
66
+ http: httpMock,
67
+ someOtherVar: " hello"
68
+ });
69
+
70
+
71
+ // You may also override globals. These changes are only within the module,
72
+ // so you don't have to be afraid that other modules are influenced by your mock.
73
+ myModule .__set__ ({
60
74
console: {
61
75
log : function () {/* be quiet*/ }
62
76
},
63
- process: { argv: [" some" ," other" ," args" ] },
64
- __filename: " some/other/dir"
77
+ process: {
78
+ argv: [" testArg1" ," testArg2" ]
79
+ }
65
80
});
66
81
67
- // This will inject
68
- // var console = {log: function () { /* be quiet */ }};
69
- // var process = {argv: ["some", "other", "args"]};
70
- // var __filename = "some/other/dir";
71
- // at the end of the module.
72
82
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
- ```
83
+ // But be careful, if you do something like this you'll change your global
84
+ // console instance.
85
+ myModule .__set__ (" console.log" ,function () {/* be quiet*/ });
77
86
78
- ###Leaks
79
- ``` javascript
80
- // You can expose private variables for unit tests
81
- rewiredModule= rewire (" ./myModuleA.js" ,null ,null , [" myVar1" ," myVar2" );
82
87
83
- // This will inject
84
- // module.exports.__ = {myVar1: myVar1, myVar2: myVar2}
85
- // at the end of the module.
88
+ // By getting private variables you can test for instance if your
89
+ // module is in a specific state
90
+ assert . ok ( myModule . __get__ ( " currentState " ) === " idle " );
86
91
87
- // You can access now your private variables under the special.__-object
88
- console .log (rewiredModule .__ .myVar1 );
89
- console .log (rewiredModule .__ .myVar2 );
90
- ```
91
92
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
93
+ // You can also disable caching when loading the rewired module. All
94
+ // subsequent calls of require() will than return the original module again.
95
+ rewire (" ./myModule.js" ,false )=== require (" ./myModule.js" );// = false
96
+
97
+
98
+ // Every call of rewire returns a new instance and overwrites the old
99
+ // one in the module cache.
100
+ rewire (" ./myModule.js" )=== rewire (" ./myModule.js" );// = false
99
101
100
- // You can also delete all rewired modules from the cache by one call.
102
+
103
+ // If you want to remove all your rewired modules from the
104
+ // cache just call rewire.reset().
105
+ // Do this before every unit test to ensure a clean testing environment.
101
106
rewire .reset ();
102
- // You should call this after every unit test to ensure a clean test environment.
103
107
```
104
108
105
109
-----------------------------------------------------------------
106
110
<br />
107
111
108
112
##API
109
113
110
- ** rewire(*** filename,mocks, injections, leaks, cache*** )**
114
+ ** rewire(*** filename, cache*** ): {RewiredModule} **
111
115
112
116
- * {!String} filename* : <br />
113
117
Path to the module that shall be rewired. Use it exactly like require().
114
118
115
- - * {Object} mocks (optional)* : <br />
116
- An object with module mocks. Keys should reflect the required path of the module.
117
-
118
- - * {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.
120
-
121
- - * {Array< ; String> ; } leaks (optional)* : <br />
122
- An array with variable names that should be exported. These variables are accessible via` myModule.__ ` .
123
-
124
119
- * {Boolean=true} cache (optional)* : <br />
125
120
Indicates whether the rewired module should be cached by node so subsequent calls of` require() ` will
126
121
return the rewired module. Further calls of` rewire() ` will always overwrite the cache.
127
122
128
- Returns the rewired module.
129
-
130
123
** rewire.reset()**
131
124
132
125
Removes all rewired modules from` require.cache ` . Every` require() ` will now return the original module again.
133
126
134
- -----------------------------------------------------------------
135
- <br />
127
+ ** RewiredModule._ ;_ ; set_ ;_ ; (*** name, value*** )**
136
128
137
- ##Please note
138
- ###Keys should be exactly the same like they're required in the target module
139
- So if you write` require("../../myModules/myModuleA.js") ` you need to pass
140
- ` {"../../myModules/myModuleA.js": myModuleAMock} ` .
129
+ - * {!String} name* : <br />
130
+ Name of the variable to set. The variable should be a global or defined with` var ` in the top-level
131
+ scope of the module.
141
132
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.
133
+ - * {&lowast ; } value* : <br />
134
+ The value to set
145
135
146
- Imagine ` rewire("./myModule.js", null, {console: null}); ` :
136
+ ** RewiredModule. &# 95 ;&# 95 ; set &# 95 ;&# 95 ; ( *** env *** ) **
147
137
148
- ``` javascript
149
- console .log (" Hello" );// ouch, that won't work. console is undefined at this point because of hoisting
150
-
151
- // End of module ///////////////
152
- // rewire will inject here
153
- var console = null ;
154
- ```
155
-
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:
161
-
162
- ``` javascript
163
- var myLeaks= {};
164
-
165
- module .exports = function (someValue ) {
166
- myLeaks .someValue = someValue;
167
- };
168
- ```
138
+ - * {!Object} env* : <br />
139
+ Takes all keys as variable names and sets the values respectively.
169
140
170
- And then: ``` rewire("myModuleA.js", null, null, ["myLeaks"]); ```
141
+ ** RewiredModule. &# 95 ;&# 95 ; get &# 95 ;&# 95 ; ( *** name *** ): { & lowast ; } **
171
142
172
- Because``` myLeaks ``` is defined at the end of the module, you're able to access the leak object and all leaks that
173
- are attached to it later during runtime.
143
+ Returns the private variable.
174
144
175
- ###Call rewire.reset() after every unit test
176
- All``` require() ``` s will now return the original module again.
177
145
178
146
-----------------------------------------------------------------
179
147
<br />