@@ -249,16 +249,28 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
249249 } ) ;
250250
251251 it ( "should be possible to set implicit globals" , function ( ) {
252- var implicitGlobalModule = rewire ( "./implicitGlobal.js" ) ;
252+ var implicitGlobalModule ,
253+ err ;
253254
254- implicitGlobalModule . __set__ ( "implicitGlobal" , true ) ;
255- expect ( implicitGlobalModule . __get__ ( "implicitGlobal" ) ) . to . be ( true ) ;
256- // setting implicit global vars will change them globally instead of locally.
257- // that's a shortcoming of the current implementation which can't be solved easily.
258- //expect(implicitGlobal).to.be.a("string");
255+ try {
256+ implicitGlobalModule = rewire ( "./implicitGlobal.js" ) ;
257+
258+ implicitGlobalModule . __set__ ( "implicitGlobal" , true ) ;
259+ expect ( implicitGlobalModule . __get__ ( "implicitGlobal" ) ) . to . be ( true ) ;
260+ // setting implicit global vars will change them globally instead of locally.
261+ // that's a shortcoming of the current implementation which can't be solved easily.
262+ //expect(implicitGlobal).to.be.a("string");
263+ } catch ( e ) {
264+ err = e ;
265+ } finally {
266+ // Cleaning up...
267+ delete global . implicitGlobal ;
268+ delete global . undefinedImplicitGlobal ;
269+ }
259270
260- // Cleaning up...
261- delete global . implicitGlobal ;
271+ if ( err ) {
272+ throw err ;
273+ }
262274 } ) ;
263275
264276 it ( "should throw a TypeError if the path is not a string" , function ( ) {
@@ -298,4 +310,31 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
298310
299311 } ) ;
300312
313+ it ( "should be possible to mock undefined, implicit globals" , function ( ) {
314+ var implicitGlobalModule ,
315+ err ;
316+
317+ try {
318+ implicitGlobalModule = rewire ( "./implicitGlobal.js" ) ;
319+ implicitGlobalModule . __set__ ( "undefinedImplicitGlobal" , "yoo!" ) ;
320+ expect ( implicitGlobalModule . __get__ ( "undefinedImplicitGlobal" ) ) . to . equal ( "yoo!" ) ;
321+
322+ implicitGlobalModule = rewire ( "./implicitGlobal.js" ) ;
323+ implicitGlobalModule . __set__ ( {
324+ undefinedImplicitGlobal : "bro!"
325+ } ) ;
326+ expect ( implicitGlobalModule . __get__ ( "undefinedImplicitGlobal" ) ) . to . equal ( "bro!" ) ;
327+ } catch ( e ) {
328+ err = e ;
329+ } finally {
330+ // Cleaning up...
331+ delete global . implicitGlobal ;
332+ delete global . undefinedImplicitGlobal ;
333+ }
334+
335+ if ( err ) {
336+ throw err ;
337+ }
338+ } ) ;
339+
301340} ) ;
0 commit comments