Skip to content

Commit 447f383

Browse files
committed
Update and improve README
1 parent e676560 commit 447f383

File tree

1 file changed

+58
-49
lines changed

1 file changed

+58
-49
lines changed

README.md

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
rewire
22
======
3-
**Easy monkey-patching for node.js unit testing**.
3+
**Easy monkey-patching for node.js unit tests**
44

5-
[![Build Status](https://travis-ci.org/jhnns/rewire.svg?branch=master)](http://travis-ci.org/jhnns/rewire)
5+
[![](https://img.shields.io/npm/v/rewire.svg)](https://www.npmjs.com/package/rewire)
6+
[![](https://img.shields.io/npm/dm/rewire.svg)](https://www.npmjs.com/package/rewire)
67
[![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/)
8+
[![Build Status](https://travis-ci.org/jhnns/rewire.svg?branch=master)](https://travis-ci.org/rewire/jhnns)
9+
[![Coverage Status](https://img.shields.io/coveralls/jhnns/rewire.svg)](https://coveralls.io/r/jhnns/rewire?branch=master)
910

1011
rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may
1112

1213
- inject mocks for other modules or globals like `process`
13-
- leak private variables
14+
- inspect private variables
1415
- override variables within the module.
1516

16-
rewire does **not** load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own
17-
require to load the module. Thus your module behaves exactly the same in your test environment as under regular
18-
circumstances (except your modifications).
17+
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).
1918

20-
Good news to all caffeine-addicts: rewire works also with [Coffee-Script](http://coffeescript.org/). Note that in this
21-
case CoffeeScript needs to be listed in your devDependencies.
19+
**Please note:** The current version of rewire is not compatible with `const` or [babel](http://babeljs.io/). See [Limitations](https://github.com/jhnns/rewire#limitations).
2220

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)
21+
<br>
2422

25-
[![npm status](https://nodei.co/npm/rewire.svg?downloads=true&stars=true&downloadRank=true)](https://npmjs.org/package/rewire)
23+
Installation
24+
------------
25+
26+
`npm install rewire`
2627

2728
<br />
2829

@@ -31,8 +32,8 @@ Introduction
3132

3233
Imagine you want to test this module:
3334

34-
`lib/myModule.js`
3535
```javascript
36+
// lib/myModules.js
3637
// With rewire you can change all these variables
3738
var fs = require("fs"),
3839
path = "/somewhere/on/the/disk";
@@ -47,8 +48,8 @@ exports.readSomethingFromFileSystem = readSomethingFromFileSystem;
4748

4849
Now within your test module:
4950

50-
`test/myModule.test.js`
5151
```javascript
52+
// test/myModule.test.js
5253
var rewire = require("rewire");
5354

5455
var myModule = rewire("../lib/myModule.js");
@@ -133,10 +134,19 @@ myModule.__with__({
133134
// port is still 3000 here because the promise hasn't been resolved yet
134135
```
135136

136-
### Limitations
137+
<br />
138+
139+
Limitations
140+
-----------
141+
142+
**Using `const`**<br>
143+
It's not possible to rewire `const` (see [#79](https://github.com/jhnns/rewire/issues/79)). This can probably be solved with [proxies](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy) someday but requires further research.
144+
145+
**Transpilers**<br>
146+
Some transpilers, like babel, rename variables in order to emulate certain language features. Rewire will not work in these cases (see [#62](https://github.com/jhnns/rewire/issues/62)). A possible solution might be switching to [babel-plugin-rewire](https://github.com/speedskater/babel-plugin-rewire).
137147

138148
**Variables inside functions**<br>
139-
Variables inside functions can not be changed by rewire. This is constrained by JavaScript and can't be circumvented by rewire.
149+
Variables inside functions can not be changed by rewire. This is constrained by the language.
140150

141151
```javascript
142152
// myModule.js
@@ -146,9 +156,6 @@ Variables inside functions can not be changed by rewire. This is constrained by
146156
})()
147157
```
148158

149-
**const**
150-
Rewire does not work with const [see this issue](https://github.com/jhnns/rewire/issues/79)
151-
152159
**Modules that export primitives**<br>
153160
rewire is not able to attach the `__set__`- and `__get__`-method if your module is just exporting a primitive. Rewiring does not work in this case.
154161

@@ -169,32 +176,6 @@ If `someGlobalVar` is not a valid variable name, rewire just ignores it. **In th
169176
**Special globals**<br>
170177
Please be aware that you can't rewire `eval()` or the global object itself.
171178

172-
### Caveats
173-
174-
**Difference to require()**<br>
175-
Every call of rewire() executes the module again and returns a fresh instance.
176-
177-
```javascript
178-
rewire("./myModule.js") === rewire("./myModule.js"); // = false
179-
```
180-
181-
This can especially be a problem if the module is not idempotent [like mongoose models](https://github.com/jhnns/rewire/issues/27).
182-
183-
**Dot notation**<br>
184-
Although it is possible to use dot notation when calling `__set__`, it is strongly discouraged in most cases. For instance, writing `myModule.__set__("console.log", fn)` is effectively the same as just writing `console.log = fn`. It would be better to write:
185-
186-
```javascript
187-
myModule.__set__("console", {
188-
log: function () {}
189-
});
190-
```
191-
192-
This replaces `console` just inside `myModule`. That is, because rewire is using `eval()` to turn the key expression into an assignment. Hence, calling `myModule.__set__("console.log", fn)` modifies the `log` function on the *global* `console` object.
193-
194-
**Transpiled ES6 modules**<br>
195-
If you are using Babel with ES6 rewire does not know how to mock the top level references in a module because Babel has remapped them (see #62).
196-
197-
In this case you should use [babel-plugin-rewire](https://github.com/speedskater/babel-plugin-rewire) instead.
198179

199180
<br />
200181

@@ -223,13 +204,41 @@ Returns a function which - when being called - sets `obj`, executes the given `c
223204

224205
<br />
225206

226-
## Client-Side Bundlers
207+
Caveats
208+
-------
209+
210+
**Difference to require()**<br>
211+
Every call of rewire() executes the module again and returns a fresh instance.
212+
213+
```javascript
214+
rewire("./myModule.js") === rewire("./myModule.js"); // = false
215+
```
216+
217+
This can especially be a problem if the module is not idempotent [like mongoose models](https://github.com/jhnns/rewire/issues/27).
227218

228-
### webpack
219+
**Dot notation**<br>
220+
Although it is possible to use dot notation when calling `__set__`, it is strongly discouraged in most cases. For instance, writing `myModule.__set__("console.log", fn)` is effectively the same as just writing `console.log = fn`. It would be better to write:
221+
222+
```javascript
223+
myModule.__set__("console", {
224+
log: function () {}
225+
});
226+
```
227+
228+
This replaces `console` just inside `myModule`. That is, because rewire is using `eval()` to turn the key expression into an assignment. Hence, calling `myModule.__set__("console.log", fn)` modifies the `log` function on the *global* `console` object.
229+
230+
<br />
231+
232+
webpack
233+
-------
229234
See [rewire-webpack](https://github.com/jhnns/rewire-webpack)
230235

231-
### browserify
232-
If you're using browserify and want to use rewire with browserify [please let me know](https://github.com/jhnns/rewire/issues/13).
236+
<br />
237+
238+
CoffeeScript
239+
------------
240+
241+
Good news to all caffeine-addicts: rewire works also with [Coffee-Script](http://coffeescript.org/). Note that in this case CoffeeScript needs to be listed in your devDependencies.
233242

234243
<br />
235244

0 commit comments

Comments
 (0)