22// In case this module was in strict mode, all other modules called by this would also be strict.
33// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
44
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+
510var 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 ( ) ;
715
816function checkForTypeError ( err ) {
917 expect ( err . constructor ) . to . be ( TypeError ) ;
1018}
1119
1220describe ( "rewire " + ( typeof testEnv === "undefined" ? "(node)" : "(" + testEnv + ")" ) , function ( ) {
21+
1322 it ( "should work like require()" , function ( ) {
1423 rewire ( "./moduleA.js" ) . getFilename ( ) ;
1524 require ( "./moduleA.js" ) . getFilename ( ) ;
1625 expect ( rewire ( "./moduleA.js" ) . getFilename ( ) ) . to . eql ( require ( "./moduleA.js" ) . getFilename ( ) ) ;
1726 expect ( rewire ( "../testModules/someOtherModule.js" ) . filename ) . to . eql ( require ( "../testModules/someOtherModule.js" ) . filename ) ;
1827 } ) ;
28+
1929 it ( "should return a fresh instance of the module" , function ( ) {
2030 var someOtherModule = require ( "./someOtherModule.js" ) ,
2131 rewiredSomeOtherModule ;
@@ -24,6 +34,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
2434 rewiredSomeOtherModule = rewire ( "./someOtherModule.js" ) ;
2535 expect ( rewiredSomeOtherModule . fs ) . not . to . be ( "This has been modified" ) ;
2636 } ) ;
37+
2738 it ( "should not cache the rewired module" , function ( ) {
2839 var rewired ,
2940 someOtherModule = require ( "./someOtherModule.js" ) ;
@@ -36,26 +47,42 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
3647 expect ( require ( "./moduleA.js" ) . someOtherModule ) . to . be ( someOtherModule ) ;
3748 expect ( require ( "./moduleA.js" ) . someOtherModule . fs ) . to . be ( "This has been changed" ) ;
3849 } ) ;
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 ) ;
4261 } ) ;
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 ) ;
4667 } ) ;
68+
4769 it ( "should not influence other modules" , function ( ) {
4870 rewire ( "./moduleA.js" ) ;
4971
5072 expect ( require ( "./someOtherModule.js" ) . __set__ ) . to . be ( undefined ) ;
5173 expect ( require ( "./someOtherModule.js" ) . __get__ ) . to . be ( undefined ) ;
74+ expect ( require ( "./someOtherModule.js" ) . __with__ ) . to . be ( undefined ) ;
5275 } ) ;
76+
5377 it ( "should not override/influence global objects by default" , function ( ) {
5478 // This should throw no exception
5579 rewire ( "./moduleA.js" ) . checkSomeGlobals ( ) ;
5680 rewire ( "./moduleB.js" ) . checkSomeGlobals ( ) ;
5781 } ) ;
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 ( ) {
5986 var rewiredModuleA = rewire ( "./moduleA.js" ) ,
6087 newObj = { } ;
6188
@@ -66,12 +93,37 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
6693 expect ( rewiredModuleA . getMyObj ( ) ) . to . be ( newObj ) ;
6794 rewiredModuleA . __set__ ( "env" , "ENVENV" ) ;
6895 } ) ;
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 ( ) {
70100 var rewiredModuleA = rewire ( "./moduleA.js" ) ;
71101
72102 expect ( rewiredModuleA . __get__ ( "myNumber" ) ) . to . be ( rewiredModuleA . getMyNumber ( ) ) ;
73103 expect ( rewiredModuleA . __get__ ( "myObj" ) ) . to . be ( rewiredModuleA . getMyObj ( ) ) ;
74104 } ) ;
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+
75127 it ( "should provide the ability to inject mocks" , function ( done ) {
76128 var rewiredModuleA = rewire ( "./moduleA.js" ) ,
77129 mockedFs = {
@@ -84,6 +136,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
84136 rewiredModuleA . __set__ ( "fs" , mockedFs ) ;
85137 rewiredModuleA . readFileSync ( ) ;
86138 } ) ;
139+
87140 it ( "should not influence other modules when injecting mocks" , function ( ) {
88141 var rewiredModuleA = rewire ( "./moduleA.js" ) ,
89142 someOtherModule ,
@@ -93,6 +146,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
93146 someOtherModule = require ( "./someOtherModule.js" ) ;
94147 expect ( someOtherModule . fs ) . not . to . be ( mockedFs ) ;
95148 } ) ;
149+
96150 it ( "should provide the ability to mock global objects just within the module" , function ( ) {
97151 var rewiredModuleA = rewire ( "./moduleA.js" ) ,
98152 rewiredModuleB = rewire ( "./moduleB.js" ) ,
@@ -123,6 +177,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
123177 expect ( document === documentMock ) . to . be ( false ) ;
124178 }
125179 } ) ;
180+
126181 it ( "should be possible to mock global objects that are added on runtime" , function ( ) {
127182 var rewiredModule ;
128183
@@ -144,26 +199,31 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
144199 }
145200 }
146201 } ) ;
202+
147203 it ( "should not be a problem to have a comment on file end" , function ( ) {
148204 var rewired = rewire ( "./emptyModule.js" ) ;
149205
150206 rewired . __set__ ( "someVar" , "hello" ) ;
151207 expect ( rewired . __get__ ( "someVar" ) ) . to . be ( "hello" ) ;
152208 } ) ;
209+
153210 it ( "should not influence the original require if nothing has been required within the rewired module" , function ( ) {
154211 rewire ( "./emptyModule.js" ) ; // nothing happens here because emptyModule doesn't require anything
155212 expect ( require ( "./moduleA.js" ) . __set__ ) . to . be ( undefined ) ; // if restoring the original node require didn't worked, the module would have a setter
156213 } ) ;
214+
157215 it ( "subsequent calls of rewire should always return a new instance" , function ( ) {
158216 expect ( rewire ( "./moduleA.js" ) ) . not . to . be ( rewire ( "./moduleA.js" ) ) ;
159217 } ) ;
218+
160219 it ( "should preserve the strict mode" , function ( ) {
161220 var strictModule = rewire ( "./strictModule.js" ) ;
162221
163222 expect ( function ( ) {
164223 strictModule . doSomethingUnstrict ( ) ;
165224 } ) . to . throwException ( checkForTypeError ) ;
166225 } ) ;
226+
167227 it ( "should not modify line numbers in stack traces" , function ( ) {
168228 var throwError = rewire ( "./throwError.js" ) ;
169229
@@ -175,9 +235,11 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
175235 }
176236 }
177237 } ) ;
238+
178239 it ( "should throw a TypeError if the path is not a string" , function ( ) {
179240 expect ( function ( ) {
180241 rewire ( null ) ;
181242 } ) . to . throwException ( checkForTypeError ) ;
182243 } ) ;
244+
183245} ) ;
0 commit comments