22=====
33** Easy dependency injection for node.js unit testing** .
44
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+
510rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may
611
712- inject mocks for other modules or globals like` process `
@@ -17,28 +22,17 @@ case CoffeeScript needs to be listed in your devDependencies.
1722
1823If 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 )
1924
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 )
2426
2527<br />
2628
27- Installation
28- ------------
29-
30- ` npm install rewire `
31-
32- <br />
33-
34- Examples
29+ Introduction
3530--------
3631
3732Imagine you want to test this module:
3833
34+ ` lib/myModule.js `
3935``` javascript
40- // lib/myModule.js
41-
4236// With rewire you can change all these variables
4337var fs= require (" fs" ),
4438 path= " /somewhere/on/the/disk" ;
@@ -53,9 +47,8 @@ exports.readSomethingFromFileSystem = readSomethingFromFileSystem;
5347
5448Now within your test module:
5549
50+ ` test/myModule.test.js `
5651``` javascript
57- // test/myModule.test.js
58-
5952var rewire= require (" rewire" );
6053
6154var myModule= rewire (" ../lib/myModule.js" );
@@ -68,7 +61,7 @@ myModule.__set__("path", "/dev/null");
6861myModule .__get__ (" path" );// = '/dev/null'
6962```
7063
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.
7265
7366``` javascript
7467var fsMock= {
@@ -84,7 +77,7 @@ myModule.readSomethingFromFileSystem(function (err, data) {
8477});
8578```
8679
87- You can also setdifferent variables with one call.
80+ You can also setmultiple variables with one call.
8881
8982``` javascript
9083myModule .__set__ ({
@@ -106,6 +99,40 @@ myModule.__set__({
10699});
107100```
108101
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+
109136###Caveats
110137
111138** Difference to require()** <br >
@@ -126,27 +153,28 @@ myModule.__set__("console.log", function () { /* be quiet */ });
126153
127154<br />
128155
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
130164
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.
132166
133- - * filename* : <br />
134- Path to the module that shall be rewired. Use it exactly like require().
167+ ###rewiredModule._ ;_ ; set_ ;_ ; (obj: Object): Function
135168
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.
137170
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): *
142172
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 ` .
146174
147- ###rewiredModule._ ;_ ; get _ ;_ ; (name ):value
175+ ###rewiredModule._ ;_ ; with _ ;_ ; (obj: Object ):Function & lt ; callback: Function>
148176
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 .
150178
151179<br />
152180