Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcdc24b0

Browse files
committed
Adjust README
1 parentecb5b7a commitcdc24b0

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

‎README.md

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,9 @@ Imagine you want to test this module:
4141

4242
// With rewire you can change all these variables
4343
var fs=require("fs"),
44-
http=require("http"),
45-
someOtherVar="hi",
46-
myPrivateVar=1;
44+
path="/somewhere/on/the/disk";
4745

4846
functionreadSomethingFromFileSystem(cb) {
49-
// But no scoped variables
50-
var path="/somewhere/on/the/disk";
51-
5247
console.log("Reading from file system ...");
5348
fs.readFile(path,"utf8", cb);
5449
}
@@ -63,34 +58,44 @@ Now within your test module:
6358

6459
var rewire=require("rewire");
6560

66-
// rewire acts exactly like require.
6761
var myModule=rewire("../lib/myModule.js");
62+
```
63+
64+
rewire acts exactly like require. Just with one difference: Your module will now export a special setter and getter for private variables.
65+
66+
```javascript
67+
myModule.__set__("path","/dev/null");
68+
myModule.__get__("path");// = '/dev/null'
69+
```
6870

69-
// Just with one difference:
70-
// Your module will now export a special setter and getter for private variables.
71-
myModule.__set__("myPrivateVar",123);
72-
myModule.__get__("myPrivateVar");// = 123
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.
7372

74-
// This allows you to mock almost everything within the module e.g. the fs-module.
75-
// Just pass the variable name as first parameter and your mock as second.
76-
myModule.__set__("fs", {
73+
```javascript
74+
var fsMock= {
7775
readFile:function (path,encoding,cb) {
76+
expect(path).to.equal("/somewhere/on/the/disk");
7877
cb(null,"Success!");
7978
}
80-
});
79+
};
80+
myModule.__set__("fs", fsMock);
81+
8182
myModule.readSomethingFromFileSystem(function (err,data) {
8283
console.log(data);// = Success!
8384
});
85+
```
86+
87+
You can also set different variables with one call.
8488

85-
// You can set different variables with one call.
89+
```javascript
8690
myModule.__set__({
8791
fs: fsMock,
88-
http: httpMock,
89-
someOtherVar:"hello"
92+
path:"/dev/null"
9093
});
94+
```
95+
96+
You may also override globals. These changes are only within the module, so you don't have to be concerned that other modules are influenced by your mock.
9197

92-
// You may also override globals. These changes are only within the module, so
93-
// you don't have to be concerned that other modules are influenced by your mock.
98+
```javascript
9499
myModule.__set__({
95100
console: {
96101
log:function () {/* be quiet*/ }
@@ -99,16 +104,26 @@ myModule.__set__({
99104
argv: ["testArg1","testArg2"]
100105
}
101106
});
107+
```
102108

103-
// But be careful, if you do something like this you'll change your global
104-
// console instance.
105-
myModule.__set__("console.log",function () {/* be quiet*/ });
109+
###Caveats
110+
111+
**Difference to require()**
112+
Every call of rewire() executes the module again and returns a fresh instance.
106113

107-
// There is another difference to require:
108-
// Every call of rewire() returns a new instance.
114+
```javascript
109115
rewire("./myModule.js")===rewire("./myModule.js");// = false
110116
```
111117

118+
This can especially be a problem if the module is not idempotent[like mongoose models](https://github.com/jhnns/rewire/issues/27).
119+
120+
**Changing globals**
121+
Be careful, if you do something like this you'll change your global console instance.
122+
123+
```javascript
124+
myModule.__set__("console.log",function () {/* be quiet*/ });
125+
```
126+
112127
<br />
113128

114129
##API

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp