2
2
// In case this module was in strict mode, all other modules called by this would also be strict.
3
3
// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
4
4
5
+ // These shared test cases are used to check if the provided implementation of rewire is compatible
6
+ // with the original rewire. Since you can use rewire with client-side bundlers like webpack we need
7
+ // to test the implementation there again.
8
+ //@see https://github.com/jhnns/rewire-webpack
9
+
5
10
var expect = require ( "expect.js" ) ,
6
- rewire = require ( "rewire" ) ;
11
+ rewire = require ( "rewire" ) ,
12
+ __set__Src = require ( "../../lib/__set__.js" ) . toString ( ) ,
13
+ __get__Src = require ( "../../lib/__get__.js" ) . toString ( ) ,
14
+ __with__Src = require ( "../../lib/__with__.js" ) . toString ( ) ;
7
15
8
16
function checkForTypeError ( err ) {
9
17
expect ( err . constructor ) . to . be ( TypeError ) ;
10
18
}
11
19
12
20
describe ( "rewire " + ( typeof testEnv === "undefined" ?"(node)" :"(" + testEnv + ")" ) , function ( ) {
21
+
13
22
it ( "should work like require()" , function ( ) {
14
23
rewire ( "./moduleA.js" ) . getFilename ( ) ;
15
24
require ( "./moduleA.js" ) . getFilename ( ) ;
16
25
expect ( rewire ( "./moduleA.js" ) . getFilename ( ) ) . to . eql ( require ( "./moduleA.js" ) . getFilename ( ) ) ;
17
26
expect ( rewire ( "../testModules/someOtherModule.js" ) . filename ) . to . eql ( require ( "../testModules/someOtherModule.js" ) . filename ) ;
18
27
} ) ;
28
+
19
29
it ( "should return a fresh instance of the module" , function ( ) {
20
30
var someOtherModule = require ( "./someOtherModule.js" ) ,
21
31
rewiredSomeOtherModule ;
@@ -24,6 +34,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
24
34
rewiredSomeOtherModule = rewire ( "./someOtherModule.js" ) ;
25
35
expect ( rewiredSomeOtherModule . fs ) . not . to . be ( "This has been modified" ) ;
26
36
} ) ;
37
+
27
38
it ( "should not cache the rewired module" , function ( ) {
28
39
var rewired ,
29
40
someOtherModule = require ( "./someOtherModule.js" ) ;
@@ -36,26 +47,42 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
36
47
expect ( require ( "./moduleA.js" ) . someOtherModule ) . to . be ( someOtherModule ) ;
37
48
expect ( require ( "./moduleA.js" ) . someOtherModule . fs ) . to . be ( "This has been changed" ) ;
38
49
} ) ;
39
- it ( "should modify the module so it provides a __set__ - function" , function ( ) {
40
- expect ( rewire ( "./moduleA.js" ) . __set__ ) . to . be . a ( Function ) ;
41
- expect ( rewire ( "./moduleB.js" ) . __set__ ) . to . be . a ( Function ) ;
50
+
51
+ // By comparing the src we can ensure that the provided __set__ function is our tested implementation
52
+ it ( "should modify the module so it provides the __set__ - function" , function ( ) {
53
+ expect ( rewire ( "./moduleA.js" ) . __set__ . toString ( ) ) . to . be ( __set__Src ) ;
54
+ expect ( rewire ( "./moduleB.js" ) . __set__ . toString ( ) ) . to . be ( __set__Src ) ;
55
+ } ) ;
56
+
57
+ // By comparing the src we can ensure that the provided __set__ function is our tested implementation
58
+ it ( "should modify the module so it provides the __get__ - function" , function ( ) {
59
+ expect ( rewire ( "./moduleA.js" ) . __get__ . toString ( ) ) . to . be ( __get__Src ) ;
60
+ expect ( rewire ( "./moduleB.js" ) . __get__ . toString ( ) ) . to . be ( __get__Src ) ;
42
61
} ) ;
43
- it ( "should modify the module so it provides a __get__ - function" , function ( ) {
44
- expect ( rewire ( "./moduleA.js" ) . __get__ ) . to . be . a ( Function ) ;
45
- expect ( rewire ( "./moduleB.js" ) . __get__ ) . to . be . a ( Function ) ;
62
+
63
+ // By comparing the src we can ensure that the provided __set__ function is our tested implementation
64
+ it ( "should modify the module so it provides the __with__ - function" , function ( ) {
65
+ expect ( rewire ( "./moduleA.js" ) . __with__ . toString ( ) ) . to . be ( __with__Src ) ;
66
+ expect ( rewire ( "./moduleB.js" ) . __with__ . toString ( ) ) . to . be ( __with__Src ) ;
46
67
} ) ;
68
+
47
69
it ( "should not influence other modules" , function ( ) {
48
70
rewire ( "./moduleA.js" ) ;
49
71
50
72
expect ( require ( "./someOtherModule.js" ) . __set__ ) . to . be ( undefined ) ;
51
73
expect ( require ( "./someOtherModule.js" ) . __get__ ) . to . be ( undefined ) ;
74
+ expect ( require ( "./someOtherModule.js" ) . __with__ ) . to . be ( undefined ) ;
52
75
} ) ;
76
+
53
77
it ( "should not override/influence global objects by default" , function ( ) {
54
78
// This should throw no exception
55
79
rewire ( "./moduleA.js" ) . checkSomeGlobals ( ) ;
56
80
rewire ( "./moduleB.js" ) . checkSomeGlobals ( ) ;
57
81
} ) ;
58
- it ( "should provide the ability to set private vars" , function ( ) {
82
+
83
+ // This is just an integration test for the __set__ method
84
+ // You can find a full test for __set__ under /test/__set__.test.js
85
+ it ( "should provide a working __set__ method" , function ( ) {
59
86
var rewiredModuleA = rewire ( "./moduleA.js" ) ,
60
87
newObj = { } ;
61
88
@@ -66,12 +93,37 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
66
93
expect ( rewiredModuleA . getMyObj ( ) ) . to . be ( newObj ) ;
67
94
rewiredModuleA . __set__ ( "env" , "ENVENV" ) ;
68
95
} ) ;
69
- it ( "should provide the ability to get private vars" , function ( ) {
96
+
97
+ // This is just an integration test for the __get__ method
98
+ // You can find a full test for __get__ under /test/__get__.test.js
99
+ it ( "should provide a working __get__ method" , function ( ) {
70
100
var rewiredModuleA = rewire ( "./moduleA.js" ) ;
71
101
72
102
expect ( rewiredModuleA . __get__ ( "myNumber" ) ) . to . be ( rewiredModuleA . getMyNumber ( ) ) ;
73
103
expect ( rewiredModuleA . __get__ ( "myObj" ) ) . to . be ( rewiredModuleA . getMyObj ( ) ) ;
74
104
} ) ;
105
+
106
+ // This is just an integration test for the __with__ method
107
+ // You can find a full test for __with__ under /test/__with__.test.js
108
+ it ( "should provide a working __with__ method" , function ( ) {
109
+ var rewiredModuleA = rewire ( "./moduleA.js" ) ,
110
+ newObj = { } ;
111
+
112
+ expect ( rewiredModuleA . getMyNumber ( ) ) . to . be ( 0 ) ;
113
+ expect ( rewiredModuleA . getMyObj ( ) ) . to . not . be ( newObj ) ;
114
+
115
+ rewiredModuleA . __with__ ( {
116
+ myNumber :2 ,
117
+ myObj :newObj
118
+ } ) ( function ( ) {
119
+ expect ( rewiredModuleA . getMyNumber ( ) ) . to . be ( 2 ) ;
120
+ expect ( rewiredModuleA . getMyObj ( ) ) . to . be ( newObj ) ;
121
+ } ) ;
122
+
123
+ expect ( rewiredModuleA . getMyNumber ( ) ) . to . be ( 0 ) ;
124
+ expect ( rewiredModuleA . getMyObj ( ) ) . to . not . be ( newObj ) ;
125
+ } ) ;
126
+
75
127
it ( "should provide the ability to inject mocks" , function ( done ) {
76
128
var rewiredModuleA = rewire ( "./moduleA.js" ) ,
77
129
mockedFs = {
@@ -84,6 +136,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
84
136
rewiredModuleA . __set__ ( "fs" , mockedFs ) ;
85
137
rewiredModuleA . readFileSync ( ) ;
86
138
} ) ;
139
+
87
140
it ( "should not influence other modules when injecting mocks" , function ( ) {
88
141
var rewiredModuleA = rewire ( "./moduleA.js" ) ,
89
142
someOtherModule ,
@@ -93,6 +146,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
93
146
someOtherModule = require ( "./someOtherModule.js" ) ;
94
147
expect ( someOtherModule . fs ) . not . to . be ( mockedFs ) ;
95
148
} ) ;
149
+
96
150
it ( "should provide the ability to mock global objects just within the module" , function ( ) {
97
151
var rewiredModuleA = rewire ( "./moduleA.js" ) ,
98
152
rewiredModuleB = rewire ( "./moduleB.js" ) ,
@@ -123,6 +177,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
123
177
expect ( document === documentMock ) . to . be ( false ) ;
124
178
}
125
179
} ) ;
180
+
126
181
it ( "should be possible to mock global objects that are added on runtime" , function ( ) {
127
182
var rewiredModule ;
128
183
@@ -144,26 +199,31 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
144
199
}
145
200
}
146
201
} ) ;
202
+
147
203
it ( "should not be a problem to have a comment on file end" , function ( ) {
148
204
var rewired = rewire ( "./emptyModule.js" ) ;
149
205
150
206
rewired . __set__ ( "someVar" , "hello" ) ;
151
207
expect ( rewired . __get__ ( "someVar" ) ) . to . be ( "hello" ) ;
152
208
} ) ;
209
+
153
210
it ( "should not influence the original require if nothing has been required within the rewired module" , function ( ) {
154
211
rewire ( "./emptyModule.js" ) ; // nothing happens here because emptyModule doesn't require anything
155
212
expect ( require ( "./moduleA.js" ) . __set__ ) . to . be ( undefined ) ; // if restoring the original node require didn't worked, the module would have a setter
156
213
} ) ;
214
+
157
215
it ( "subsequent calls of rewire should always return a new instance" , function ( ) {
158
216
expect ( rewire ( "./moduleA.js" ) ) . not . to . be ( rewire ( "./moduleA.js" ) ) ;
159
217
} ) ;
218
+
160
219
it ( "should preserve the strict mode" , function ( ) {
161
220
var strictModule = rewire ( "./strictModule.js" ) ;
162
221
163
222
expect ( function ( ) {
164
223
strictModule . doSomethingUnstrict ( ) ;
165
224
} ) . to . throwException ( checkForTypeError ) ;
166
225
} ) ;
226
+
167
227
it ( "should not modify line numbers in stack traces" , function ( ) {
168
228
var throwError = rewire ( "./throwError.js" ) ;
169
229
@@ -175,9 +235,11 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
175
235
}
176
236
}
177
237
} ) ;
238
+
178
239
it ( "should throw a TypeError if the path is not a string" , function ( ) {
179
240
expect ( function ( ) {
180
241
rewire ( null ) ;
181
242
} ) . to . throwException ( checkForTypeError ) ;
182
243
} ) ;
244
+
183
245
} ) ;