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

Skip to content

Commit 39bf586

Browse files
committed
Add possibility to mock undefined, implicit globals
Fixes jhnns#35
1 parent 02a15ea commit 39bf586

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
} else if (typeof arguments.varName === "string" && arguments.length === 2) {
3238
if (!arguments.varName) {
3339
throw new TypeError("__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
throw new TypeError("__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+
return undefinedImplicitGlobal;
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-
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

Comments
 (0)