Skip to content

Commit

Permalink
Add possibility to mock undefined, implicit globals
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
jhnns committed Feb 17, 2015
1 parent 02a15ea commit 39bf586
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
16 changes: 14 additions & 2 deletions lib/__set__.js
Expand Up @@ -25,15 +25,27 @@ 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) {
if (!arguments.varName) {
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");
}
Expand Down
6 changes: 5 additions & 1 deletion 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";
"yes, it's bad coding style but there are still some libs out there";

module.exports = function () {
return undefinedImplicitGlobal;
};
55 changes: 47 additions & 8 deletions test/testModules/sharedTestCases.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
}
});

});

0 comments on commit 39bf586

Please sign in to comment.