Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e70efd1

Browse files
committed
Fix problems when global objects like JSON, etc. have been rewired
Fixes jhnns#40
1 parent 52e914d commit e70efd1

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 (typeof arguments[0] === "object" && arguments.length === 1) {
29+
if (typeof arguments[0] === "object") {
1930
arguments.env = arguments.varName;
20-
if (!arguments.env || Array.isArray(arguments.env)) {
21-
throw new TypeError("__set__ expects an object as env");
31+
if (!arguments.env || arguments.refs.isArray(arguments.env)) {
32+
throw new arguments.refs.TypeError("__set__ expects an object as env");
2233
}
2334
arguments.revertArgs[0] = {};
2435
for (arguments.varName in arguments.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-
} else if (typeof arguments.varName === "string" && arguments.length === 2) {
48+
} else if (typeof arguments.varName === "string") {
3849
if (!arguments.varName) {
39-
throw new TypeError("__set__ expects a non-empty string as a variable name");
50+
throw new arguments.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-
throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
61+
throw new arguments.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
return function (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+
var moduleA = rewire("./moduleA.js"),
342+
revert;
343+
344+
revert = moduleA.__set__({
345+
JSON: {
346+
parse: function () { return true; }
347+
}
348+
});
349+
350+
revert();
351+
});
352+
340353
});

0 commit comments

Comments
 (0)