From 39bf586fba7022a1a7cf61515ec45fad9dcbe846 Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Tue, 17 Feb 2015 01:07:22 +0100 Subject: [PATCH] Add possibility to mock undefined, implicit globals Fixes #35 --- lib/__set__.js | 16 +++++++-- test/testModules/implicitGlobal.js | 6 +++- test/testModules/sharedTestCases.js | 55 ++++++++++++++++++++++++----- 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/lib/__set__.js b/lib/__set__.js index 851a340..bdb5042 100644 --- a/lib/__set__.js +++ b/lib/__set__.js @@ -25,7 +25,13 @@ function __set__() { if (arguments.env.hasOwnProperty(arguments.varName)) { arguments.varValue = arguments.env[arguments.varName]; arguments.src += arguments.varName + " = arguments.env[" + JSON.stringify(arguments.varName) + "]; "; - arguments.revertArgs[0][arguments.varName] = eval(arguments.varName); + try { + // Allow tests to mock implicit globals + // @see https://github.com/jhnns/rewire/issues/35 + arguments.revertArgs[0][arguments.varName] = eval(arguments.varName); + } catch (err) { + arguments.revertArgs[0][arguments.varName] = undefined; + } } } } else if (typeof arguments.varName === "string" && arguments.length === 2) { @@ -33,7 +39,13 @@ function __set__() { throw new TypeError("__set__ expects a non-empty string as a variable name"); } arguments.src = arguments.varName + " = arguments.varValue;"; - arguments.revertArgs = [arguments.varName, eval(arguments.varName)]; + try { + // Allow tests to mock implicit globals + // @see https://github.com/jhnns/rewire/issues/35 + arguments.revertArgs = [arguments.varName, eval(arguments.varName)]; + } catch (err) { + arguments.revertArgs = [arguments.varName, undefined]; + } } else { throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name"); } diff --git a/test/testModules/implicitGlobal.js b/test/testModules/implicitGlobal.js index 9bc3e2a..3048742 100644 --- a/test/testModules/implicitGlobal.js +++ b/test/testModules/implicitGlobal.js @@ -1,2 +1,6 @@ implicitGlobal = "this is an implicit global var ..." + - "yes, it's bad coding style but there are still some libs out there"; \ No newline at end of file + "yes, it's bad coding style but there are still some libs out there"; + +module.exports = function () { + return undefinedImplicitGlobal; +}; diff --git a/test/testModules/sharedTestCases.js b/test/testModules/sharedTestCases.js index 1e03d13..76498bd 100644 --- a/test/testModules/sharedTestCases.js +++ b/test/testModules/sharedTestCases.js @@ -249,16 +249,28 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + }); it("should be possible to set implicit globals", function () { - var implicitGlobalModule = rewire("./implicitGlobal.js"); + var implicitGlobalModule, + err; - implicitGlobalModule.__set__("implicitGlobal", true); - expect(implicitGlobalModule.__get__("implicitGlobal")).to.be(true); - // setting implicit global vars will change them globally instead of locally. - // that's a shortcoming of the current implementation which can't be solved easily. - //expect(implicitGlobal).to.be.a("string"); + try { + implicitGlobalModule = rewire("./implicitGlobal.js"); + + implicitGlobalModule.__set__("implicitGlobal", true); + expect(implicitGlobalModule.__get__("implicitGlobal")).to.be(true); + // setting implicit global vars will change them globally instead of locally. + // that's a shortcoming of the current implementation which can't be solved easily. + //expect(implicitGlobal).to.be.a("string"); + } catch (e) { + err = e; + } finally { + // Cleaning up... + delete global.implicitGlobal; + delete global.undefinedImplicitGlobal; + } - // Cleaning up... - delete global.implicitGlobal; + if (err) { + throw err; + } }); it("should throw a TypeError if the path is not a string", function () { @@ -298,4 +310,31 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + }); + it("should be possible to mock undefined, implicit globals", function () { + var implicitGlobalModule, + err; + + try { + implicitGlobalModule = rewire("./implicitGlobal.js"); + implicitGlobalModule.__set__("undefinedImplicitGlobal", "yoo!"); + expect(implicitGlobalModule.__get__("undefinedImplicitGlobal")).to.equal("yoo!"); + + implicitGlobalModule = rewire("./implicitGlobal.js"); + implicitGlobalModule.__set__({ + undefinedImplicitGlobal: "bro!" + }); + expect(implicitGlobalModule.__get__("undefinedImplicitGlobal")).to.equal("bro!"); + } catch (e) { + err = e; + } finally { + // Cleaning up... + delete global.implicitGlobal; + delete global.undefinedImplicitGlobal; + } + + if (err) { + throw err; + } + }); + });