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

Commit39bf586

Browse files
committed
Add possibility to mock undefined, implicit globals
Fixesjhnns#35
1 parent02a15ea commit39bf586

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

‎lib/__set__.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,27 @@ function __set__() {
2525
if(arguments.env.hasOwnProperty(arguments.varName)){
2626
arguments.varValue=arguments.env[arguments.varName];
2727
arguments.src+=arguments.varName+" = arguments.env["+JSON.stringify(arguments.varName)+"]; ";
28-
arguments.revertArgs[0][arguments.varName]=eval(arguments.varName);
28+
try{
29+
// Allow tests to mock implicit globals
30+
//@see https://github.com/jhnns/rewire/issues/35
31+
arguments.revertArgs[0][arguments.varName]=eval(arguments.varName);
32+
}catch(err){
33+
arguments.revertArgs[0][arguments.varName]=undefined;
34+
}
2935
}
3036
}
3137
}elseif(typeofarguments.varName==="string"&&arguments.length===2){
3238
if(!arguments.varName){
3339
thrownewTypeError("__set__ expects a non-empty string as a variable name");
3440
}
3541
arguments.src=arguments.varName+" = arguments.varValue;";
36-
arguments.revertArgs=[arguments.varName,eval(arguments.varName)];
42+
try{
43+
// Allow tests to mock implicit globals
44+
//@see https://github.com/jhnns/rewire/issues/35
45+
arguments.revertArgs=[arguments.varName,eval(arguments.varName)];
46+
}catch(err){
47+
arguments.revertArgs=[arguments.varName,undefined];
48+
}
3749
}else{
3850
thrownewTypeError("__set__ expects an environment object or a non-empty string as a variable name");
3951
}

‎test/testModules/implicitGlobal.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
implicitGlobal="this is an implicit global var ..."+
2-
"yes, it's bad coding style but there are still some libs out there";
2+
"yes, it's bad coding style but there are still some libs out there";
3+
4+
module.exports=function(){
5+
returnundefinedImplicitGlobal;
6+
};

‎test/testModules/sharedTestCases.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,28 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
249249
});
250250

251251
it("should be possible to set implicit globals",function(){
252-
varimplicitGlobalModule=rewire("./implicitGlobal.js");
252+
varimplicitGlobalModule,
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+
deleteglobal.implicitGlobal;
268+
deleteglobal.undefinedImplicitGlobal;
269+
}
259270

260-
// Cleaning up...
261-
deleteglobal.implicitGlobal;
271+
if(err){
272+
throwerr;
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+
varimplicitGlobalModule,
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+
deleteglobal.implicitGlobal;
332+
deleteglobal.undefinedImplicitGlobal;
333+
}
334+
335+
if(err){
336+
throwerr;
337+
}
338+
});
339+
301340
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp