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

Commite70efd1

Browse files
committed
Fix problems when global objects like JSON, etc. have been rewired
Fixesjhnns#40
1 parent52e914d commite70efd1

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

‎lib/__set__.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,30 @@
1212
function__set__(){
1313
arguments.varName=arguments[0];
1414
arguments.varValue=arguments[1];
15+
// Saving references to global objects and functions. Thus a test may even change these variables
16+
// without interfering with rewire().
17+
//@see https://github.com/jhnns/rewire/issues/40
18+
arguments.refs=arguments[2]||{
19+
isArray:Array.isArray,
20+
TypeError:TypeError,
21+
stringify:JSON.stringify
22+
// We can't save eval() because eval() is a *special* global function
23+
// That's why it can't be re-assigned in strict mode
24+
//eval: eval
25+
};
1526
arguments.src="";
1627
arguments.revertArgs=[];
1728

18-
if(typeofarguments[0]==="object"&&arguments.length===1){
29+
if(typeofarguments[0]==="object"){
1930
arguments.env=arguments.varName;
20-
if(!arguments.env||Array.isArray(arguments.env)){
21-
thrownewTypeError("__set__ expects an object as env");
31+
if(!arguments.env||arguments.refs.isArray(arguments.env)){
32+
thrownewarguments.refs.TypeError("__set__ expects an object as env");
2233
}
2334
arguments.revertArgs[0]={};
2435
for(arguments.varNameinarguments.env){
2536
if(arguments.env.hasOwnProperty(arguments.varName)){
2637
arguments.varValue=arguments.env[arguments.varName];
27-
arguments.src+=arguments.varName+" = arguments.env["+JSON.stringify(arguments.varName)+"]; ";
38+
arguments.src+=arguments.varName+" = arguments.env["+arguments.refs.stringify(arguments.varName)+"]; ";
2839
try{
2940
// Allow tests to mock implicit globals
3041
//@see https://github.com/jhnns/rewire/issues/35
@@ -34,9 +45,9 @@ function __set__() {
3445
}
3546
}
3647
}
37-
}elseif(typeofarguments.varName==="string"&&arguments.length===2){
48+
}elseif(typeofarguments.varName==="string"){
3849
if(!arguments.varName){
39-
thrownewTypeError("__set__ expects a non-empty string as a variable name");
50+
thrownewarguments.refs.TypeError("__set__ expects a non-empty string as a variable name");
4051
}
4152
arguments.src=arguments.varName+" = arguments.varValue;";
4253
try{
@@ -47,9 +58,12 @@ function __set__() {
4758
arguments.revertArgs=[arguments.varName,undefined];
4859
}
4960
}else{
50-
thrownewTypeError("__set__ expects an environment object or a non-empty string as a variable name");
61+
thrownewarguments.refs.TypeError("__set__ expects an environment object or a non-empty string as a variable name");
5162
}
5263

64+
// Passing our saved references on to the revert function
65+
arguments.revertArgs[2]=arguments.refs;
66+
5367
eval(arguments.src);
5468

5569
returnfunction(revertArgs){

‎test/__set__.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,5 @@ describe("__set__", function () {
121121
expect(function(){
122122
moduleFake.__set__(function(){});
123123
}).to.throwException(expectTypeError);
124-
expect(function(){
125-
moduleFake.__set__({},true);// misfitting number of params
126-
}).to.throwException(expectTypeError);
127-
expect(function(){
128-
moduleFake.__set__("someVar");// misfitting number of params
129-
}).to.throwException(expectTypeError);
130124
});
131-
});
125+
});

‎test/testModules/sharedTestCases.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,17 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
337337
}
338338
});
339339

340+
it("should be possible to mock and revert JSON.parse (see #40)",function(){
341+
varmoduleA=rewire("./moduleA.js"),
342+
revert;
343+
344+
revert=moduleA.__set__({
345+
JSON:{
346+
parse:function(){returntrue;}
347+
}
348+
});
349+
350+
revert();
351+
});
352+
340353
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp