8000 Clearify caveat about dot notation · ShMcK/rewire-coderoad@52c3a69 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 52c3a69

Browse files
committed
Clearify caveat about dot notation
1 parent 6413664 commit 52c3a69

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
rewire
2-
=====
2+
======
33
**Easy dependency injection for node.js unit testing**.
44

55
[![Build Status](https://travis-ci.org/jhnns/rewire.svg?branch=master)](http://travis-ci.org/jhnns/rewire)
@@ -27,7 +27,7 @@ If you want to use rewire also on the client-side take a look at [client-side bu
2727
<br />
2828

2929
Introduction
30-
--------
30+
------------
3131

3232
Imagine you want to test this module:
3333

@@ -144,13 +144,17 @@ rewire("./myModule.js") === rewire("./myModule.js"); // = false
144144

145145
This can especially be a problem if the module is not idempotent [like mongoose models](https://github.com/jhnns/rewire/issues/27).
146146

147-
**Changing globals**<br>
148-
Be careful, if you do something like this you'll change your global console instance.
147+
**Dot notation**<br>
148+
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:
149149

150150
```javascript
151-
myModule.__set__("console.log", function () { /* be quiet */ });
151+
myModule.__set__("console", {
152+
log: function () {}
153+
});
152154
```
153155

156+
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.
157+
154158
**Globals with invalid variable names**<br>
155159
rewire imports global variables into the local scope by prepending a list of `var` declarations:
156160

@@ -166,7 +170,7 @@ Please be aware that you can't rewire `eval()` or the global object itself.
166170
<br />
167171

168172
API
169-
------
173+
---
170174

171175
### rewire(filename: String): rewiredModule
172176

@@ -190,16 +194,16 @@ Returns a function which - when being called - sets `obj`, executes the given `c
190194

191195
<br />
192196

193-
##Client-Side Bundlers
197+
## Client-Side Bundlers
194198

195-
###webpack
199+
### webpack
196200
See [rewire-webpack](https://github.com/jhnns/rewire-webpack)
197201

198-
###browserify
202+
### browserify
199203
If you're using browserify and want to use rewire with browserify [please let me know](https://github.com/jhnns/rewire/issues/13).
200204

201205
<br />
202206

203-
##License
207+
## License
204208

205209
MIT

0 commit comments

Comments
 (0)
0