2
2
=====
3
3
** Easy dependency injection for node.js unit testing** .
4
4
5
+ [ ![ Build Status] ( https://travis-ci.org/jhnns/rewire.svg?branch=master )] ( http://travis-ci.org/jhnns/rewire )
6
+ [ ![ Dependency Status] ( https://david-dm.org/jhnns/rewire.svg )] ( https://david-dm.org/jhnns/rewire )
7
+ [ ![ Coverage Status] ( https://img.shields.io/coveralls/jhnns/rewire.svg )] ( https://coveralls.io/r/jhnns/rewire )
8
+ [ ![ Gittip Donate Button] ( http://img.shields.io/gittip/peerigon.svg )] ( https://www.gittip.com/peerigon/ )
9
+
5
10
rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may
6
11
7
12
- inject mocks for other modules or globals like` process `
@@ -17,28 +22,17 @@ case CoffeeScript needs to be listed in your devDependencies.
17
22
18
23
If you want to use rewire also on the client-side take a look at[ client-side bundlers] ( https://github.com/jhnns/rewire#client-side-bundlers )
19
24
20
- [ ![ Build Status] ( https://travis-ci.org/jhnns/rewire.svg?branch=master )] ( http://travis-ci.org/jhnns/rewire )
21
- [ ![ Dependency Status] ( https://david-dm.org/jhnns/rewire.svg )] ( https://david-dm.org/jhnns/rewire )
22
- [ ![ Coverage Status] ( https://img.shields.io/coveralls/jhnns/rewire.svg )] ( https://coveralls.io/r/jhnns/rewire )
23
- [ ![ Gittip Donate Button] ( http://img.shields.io/gittip/peerigon.svg )] ( https://www.gittip.com/peerigon/ )
25
+ [ ![ npm status] ( https://nodei.co/npm/rewire.svg?downloads=true&stars=true )] ( https://npmjs.org/package/rewire )
24
26
25
27
<br />
26
28
27
- Installation
28
- ------------
29
-
30
- ` npm install rewire `
31
-
32
- <br />
33
-
34
- Examples
29
+ Introduction
35
30
--------
36
31
37
32
Imagine you want to test this module:
38
33
34
+ ` lib/myModule.js `
39
35
``` javascript
40
- // lib/myModule.js
41
-
42
36
// With rewire you can change all these variables
43
37
var fs= require (" fs" ),
44
38
path= " /somewhere/on/the/disk" ;
@@ -53,9 +47,8 @@ exports.readSomethingFromFileSystem = readSomethingFromFileSystem;
53
47
54
48
Now within your test module:
55
49
50
+ ` test/myModule.test.js `
56
51
``` javascript
57
- // test/myModule.test.js
58
-
59
52
var rewire= require (" rewire" );
60
53
61
54
var myModule= rewire (" ../lib/myModule.js" );
@@ -68,7 +61,7 @@ myModule.__set__("path", "/dev/null");
68
61
myModule .__get__ (" path" );// = '/dev/null'
69
62
```
70
63
71
- This allows you to mock everything in the top-level scope of the module, like the fs- module for example. Just pass the variable name as first parameter and your mock as second.
64
+ This allows you to mock everything in the top-level scope of the module, like the fs module for example. Just pass the variable name as first parameter and your mock as second.
72
65
73
66
``` javascript
74
67
var fsMock= {
@@ -84,7 +77,7 @@ myModule.readSomethingFromFileSystem(function (err, data) {
84
77
});
85
78
```
86
79
87
- You can also setdifferent variables with one call.
80
+ You can also setmultiple variables with one call.
88
81
89
82
``` javascript
90
83
myModule .__set__ ({
@@ -106,6 +99,40 @@ myModule.__set__({
106
99
});
107
100
```
108
101
102
+ ` __set__ ` returns a function which reverts the changes introduced by this particular` __set__ ` call
103
+
104
+ ``` javascript
105
+ var revert= myModule .__set__ (" port" ,3000 );
106
+
107
+ // port is now 3000
108
+ revert ();
109
+ // port is now the previous value
110
+ ```
111
+
112
+ For your convenience you can also use the` __with__ ` method which reverts the given changes after it finished.
113
+
114
+ ``` javascript
115
+ myModule .__with__ ({
116
+ port: 3000
117
+ })(function () {
118
+ // within this function port is 3000
119
+ });
120
+ // now port is the previous value again
121
+ ```
122
+
123
+ The` __with__ ` method is also aware of promises. If a thenable is returned all changes stay until the promise has either been resolved or rejected.
124
+
125
+ ``` javascript
126
+ myModule .__with__ ({
127
+ port: 3000
128
+ })(function () {
129
+ return new Promise (... );
130
+ }).then (function () {
131
+ // now port is the previous value again
132
+ });
133
+ // port is still 3000 here because the promise hasn't been resolved yet
134
+ ```
135
+
109
136
###Caveats
110
137
111
138
** Difference to require()** <br >
@@ -126,27 +153,28 @@ myModule.__set__("console.log", function () { /* be quiet */ });
126
153
127
154
<br />
128
155
129
- ##API
156
+ API
157
+ ------
158
+
159
+ ###rewire(filename: String): rewiredModule
160
+
161
+ Returns a rewired version of the module found at` filename ` . Use` rewire() ` exactly like` require() ` .
162
+
163
+ ###rewiredModule._ ;_ ; set_ ;_ ; (name: String, value:* ): Function
130
164
131
- ###rewire(filename): rewiredModule
165
+ Sets the internal variable ` name ` to the given ` value ` . Returns a function which can be called to revert the change.
132
166
133
- - * filename* : <br />
134
- Path to the module that shall be rewired. Use it exactly like require().
167
+ ###rewiredModule._ ;_ ; set_ ;_ ; (obj: Object): Function
135
168
136
- ###rewiredModule. &# 95 ;&# 95 ; set &# 95 ;&# 95 ; (name, value)
169
+ Takes all enumerable keys of ` obj ` as variable names and sets the values respectively. Returns a function which can be called to revert the change.
137
170
138
- - * name* : <br />
139
- Name of the variable to set. The variable should be global or defined with` var ` in the top-leve scope of the module.
140
- - * value* : <br />
141
- The value to set.
171
+ ###rewiredModule._ ;_ ; get_ ;_ ; (name: String): *
142
172
143
- ###rewiredModule._ ;_ ; set_ ;_ ; (env)
144
- - * env* : <br />
145
- Takes all keys as variable names and sets the values respectively.
173
+ Returns the private variable with the given` name ` .
146
174
147
- ###rewiredModule._ ;_ ; get _ ;_ ; (name ):value
175
+ ###rewiredModule._ ;_ ; with _ ;_ ; (obj: Object ):Function & lt ; callback: Function>
148
176
149
- Returns theprivate variable .
177
+ Returnsa function which - when being called - sets ` obj ` , executes thegiven ` callback ` and reverts ` obj ` . If ` callback ` returns a promise, ` obj ` is only reverted after the promise has been resolved or rejected. For your convenience the returned function passes the received promise through .
150
178
151
179
<br />
152
180