You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-25Lines changed: 40 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -41,14 +41,9 @@ Imagine you want to test this module:
41
41
42
42
// With rewire you can change all these variables
43
43
var fs=require("fs"),
44
-
http=require("http"),
45
-
someOtherVar="hi",
46
-
myPrivateVar=1;
44
+
path="/somewhere/on/the/disk";
47
45
48
46
functionreadSomethingFromFileSystem(cb) {
49
-
// But no scoped variables
50
-
var path="/somewhere/on/the/disk";
51
-
52
47
console.log("Reading from file system ...");
53
48
fs.readFile(path,"utf8", cb);
54
49
}
@@ -63,34 +58,44 @@ Now within your test module:
63
58
64
59
var rewire=require("rewire");
65
60
66
-
// rewire acts exactly like require.
67
61
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
+
```
68
70
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.
73
72
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.
You can also set different variables with one call.
84
88
85
-
// You can set different variables with one call.
89
+
```javascript
86
90
myModule.__set__({
87
91
fs: fsMock,
88
-
http: httpMock,
89
-
someOtherVar:"hello"
92
+
path:"/dev/null"
90
93
});
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.
91
97
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
94
99
myModule.__set__({
95
100
console: {
96
101
log:function () {/* be quiet*/ }
@@ -99,16 +104,26 @@ myModule.__set__({
99
104
argv: ["testArg1","testArg2"]
100
105
}
101
106
});
107
+
```
102
108
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.