Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Ewald <johannes.ewald@peerigon.com>2014-07-08 03:16:29 +0400
committerJohannes Ewald <johannes.ewald@peerigon.com>2014-07-08 03:16:29 +0400
commit71733c8dad414c08efd57821b896bfc1d6868e22 (patch)
tree12668ac33f7f8a2e350b514027429aa6c2690e63
parentba5edffe2fab999c0b0b31b6c6ae7acb9ea943bf (diff)
Move __with__-function into own module
-rw-r--r--lib/__set__.js28
-rw-r--r--lib/__with__.js34
-rw-r--r--lib/rewire.js5
-rw-r--r--test/__set__.test.js89
-rw-r--r--test/__with__.test.js89
5 files changed, 136 insertions, 109 deletions
diff --git a/lib/__set__.js b/lib/__set__.js
index feea24a..3fcfbdb 100644
--- a/lib/__set__.js
+++ b/lib/__set__.js
@@ -7,9 +7,7 @@
*
* @param {String|Object} varName name of the variable to set
* @param {String} varValue new value
- * @throws {TypeError}
- * @throws {ReferenceError} When the variable is unknown
- * @return {*}
+ * @return {Function}
*/
function __set__() {
arguments.varName = arguments[0];
@@ -42,28 +40,8 @@ function __set__() {
eval(arguments.src);
return function (snapshot) {
- __set__(snapshot);
+ module.exports.__set__(snapshot);
}.bind(null, arguments.snapshot);
}
-function __with__() {
- var args = arguments;
-
- return function (callback) {
- var undo;
-
- if (typeof callback !== "function") {
- throw new TypeError("__with__ expects a callback function")
- }
-
- undo = __set__.apply(null, args);
-
- try {
- callback();
- } finally {
- undo();
- }
- }
-}
-
-module.exports = {"__set__": __set__, "__with__": __with__};
+module.exports = __set__;
diff --git a/lib/__with__.js b/lib/__with__.js
new file mode 100644
index 0000000..ef29c8c
--- /dev/null
+++ b/lib/__with__.js
@@ -0,0 +1,34 @@
+"use strict";
+
+/**
+ * This function will be stringified and then injected into every rewired module.
+ *
+ * Calling myModule.__with__("myPrivateVar", newValue) returns a function where
+ * you can place your tests. As long as the returned function is executed variables
+ * will be set to the given value, after that all changed variables are reset back to normal.
+ *
+ * @param {String|Object} varName name of the variable to set
+ * @param {String} varValue new value
+ * @return {Function}
+ */
+function __with__() {
+ var args = arguments;
+
+ return function (callback) {
+ var undo;
+
+ if (typeof callback !== "function") {
+ throw new TypeError("__with__ expects a callback function");
+ }
+
+ undo = module.exports.__set__.apply(null, args);
+
+ try {
+ callback();
+ } finally {
+ undo();
+ }
+ };
+}
+
+module.exports = __with__; \ No newline at end of file
diff --git a/lib/rewire.js b/lib/rewire.js
index 61160fd..086cc27 100644
--- a/lib/rewire.js
+++ b/lib/rewire.js
@@ -1,9 +1,8 @@
var Module = require("module"),
fs = require("fs"),
__get__ = require("./__get__.js"),
- setModule = require ("./__set__.js"),
- __set__ = setModule["__set__"],
- __with__ = setModule["__with__"],
+ __set__ = require ("./__set__.js"),
+ __with__ = require("./__with__.js"),
getImportGlobalsSrc = require("./getImportGlobalsSrc.js"),
detectStrictMode = require("./detectStrictMode.js"),
moduleEnv = require("./moduleEnv.js");
diff --git a/test/__set__.test.js b/test/__set__.test.js
index fe6f529..c883c27 100644
--- a/test/__set__.test.js
+++ b/test/__set__.test.js
@@ -1,7 +1,5 @@
var expect = require("expect.js"),
- setModule = require("../lib/__set__.js")
- __set__ = setModule["__set__"],
- __with__ = setModule["__with__"],
+ __set__ = require("../lib/__set__.js"),
vm = require("vm"),
expectReferenceError = expectError(ReferenceError),
@@ -18,12 +16,15 @@ describe("__set__", function () {
beforeEach(function () {
moduleFake = {
+ module: {
+ exports: {}
+ },
myValue: 0, // copy by value
myReference: {} // copy by reference
};
vm.runInNewContext(
- "__set__ = " + __set__.toString() + "; " +
+ "__set__ = module.exports.__set__ = " + __set__.toString() + "; " +
"getValue = function () { return myValue; }; " +
"getReference = function () { return myReference; }; ",
moduleFake
@@ -72,10 +73,10 @@ describe("__set__", function () {
expect(moduleFake.getReference()).to.be(newObj);
});
it("should return a function that when invoked reverts to the values before set was called", function () {
- undo = moduleFake.__set__("myValue", 4)
+ undo = moduleFake.__set__("myValue", 4);
expect(typeof undo).to.be("function");
expect(moduleFake.getValue()).to.be(4);
- undo()
+ undo();
expect(moduleFake.getValue()).to.be(0);
});
it("should be able to revert when calling with an env-obj", function () {
@@ -126,78 +127,4 @@ describe("__set__", function () {
moduleFake.__set__("someVar"); // misfitting number of params
}).to.throwException(expectTypeError);
});
-});
-
-describe("__with__", function() {
- var moduleFake;
-
- beforeEach(function () {
- moduleFake = {
- myValue: 0, // copy by value
- myReference: {} // copy by reference
- };
-
- //__with__ requires __set__ to be in scope
- vm.runInNewContext(
- "__set__ = " + __set__.toString() + "; " +
- "__with__ = " + __with__.toString() + "; " +
- "getValue = function () { return myValue; }; " +
- "getReference = function () { return myReference; }; ",
- moduleFake
- );
- });
-
- it("should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end", function () {
- var newObj = { hello: "hello" };
-
- expect(moduleFake.getValue()).to.be(0);
- expect(moduleFake.getReference()).to.eql({});
-
- moduleFake.__with__({
- myValue: 2,
- myReference: newObj
- })(function() {
- //changes will be visible from within this callback function
- expect(moduleFake.getValue()).to.be(2);
- expect(moduleFake.getReference()).to.be(newObj);
- })
-
- //undo will automatically get called for you after returning from your callback function
- expect(moduleFake.getValue()).to.be(0);
- expect(moduleFake.getReference()).to.eql({});
- });
-
- it("should still revert values if the callback throws an exception", function(){
- var newObj = { hello: "hello" };
- function withError(){
- moduleFake.__with__({
- myValue: 2,
- myReference: newObj
- })(function() {
- throw new Error("something went wrong...");
- })
- }
- expect(withError).to.throwError();
- expect(moduleFake.getValue()).to.be(0);
- expect(moduleFake.getReference()).to.eql({});
- });
-
- it("should throw an error if something other than a function is passed as the callback", function() {
- var newObj = { hello: "hello" },
- withFunction = moduleFake.__with__({
- myValue: 2,
- myReference: newObj
- })
- callWithFunction = function(){
- var args = arguments;
- return function() {
- withFunction.apply(null, args);
- };
- };
-
- expect(callWithFunction(1)).to.throwError();
- expect(callWithFunction("a string")).to.throwError();
- expect(callWithFunction({})).to.throwError();
- expect(callWithFunction(function(){})).to.not.throwError();
- });
-});
+}); \ No newline at end of file
diff --git a/test/__with__.test.js b/test/__with__.test.js
new file mode 100644
index 0000000..247838a
--- /dev/null
+++ b/test/__with__.test.js
@@ -0,0 +1,89 @@
+var expect = require("expect.js"),
+ __with__ = require("../lib/__with__.js"),
+ __set__ = require("../lib/__set__.js"),
+ vm = require("vm"),
+ expectReferenceError = expectError(ReferenceError),
+ expectTypeError = expectError(TypeError);
+
+function expectError(ErrConstructor) {
+ return function expectReferenceError(err) {
+ expect(err.constructor.name).to.be(ErrConstructor.name);
+ };
+}
+
+describe("__with__", function() {
+ var moduleFake;
+
+ beforeEach(function () {
+ moduleFake = {
+ module: {
+ exports: {}
+ },
+ myValue: 0, // copy by value
+ myReference: {} // copy by reference
+ };
+
+ //__with__ requires __set__ to be in scope
+ vm.runInNewContext(
+ "module.exports.__set__ = " + __set__.toString() + "; " +
+ "__with__ = " + __with__.toString() + "; " +
+ "getValue = function () { return myValue; }; " +
+ "getReference = function () { return myReference; }; ",
+ moduleFake
+ );
+ });
+
+ it("should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end", function () {
+ var newObj = { hello: "hello" };
+
+ expect(moduleFake.getValue()).to.be(0);
+ expect(moduleFake.getReference()).to.eql({});
+
+ moduleFake.__with__({
+ myValue: 2,
+ myReference: newObj
+ })(function() {
+ //changes will be visible from within this callback function
+ expect(moduleFake.getValue()).to.be(2);
+ expect(moduleFake.getReference()).to.be(newObj);
+ });
+
+ //undo will automatically get called for you after returning from your callback function
+ expect(moduleFake.getValue()).to.be(0);
+ expect(moduleFake.getReference()).to.eql({});
+ });
+
+ it("should still revert values if the callback throws an exception", function(){
+ var newObj = { hello: "hello" };
+ function withError(){
+ moduleFake.__with__({
+ myValue: 2,
+ myReference: newObj
+ })(function() {
+ throw new Error("something went wrong...");
+ });
+ }
+ expect(withError).to.throwError();
+ expect(moduleFake.getValue()).to.be(0);
+ expect(moduleFake.getReference()).to.eql({});
+ });
+
+ it("should throw an error if something other than a function is passed as the callback", function() {
+ var newObj = { hello: "hello" },
+ withFunction = moduleFake.__with__({
+ myValue: 2,
+ myReference: newObj
+ });
+ callWithFunction = function(){
+ var args = arguments;
+ return function() {
+ withFunction.apply(null, args);
+ };
+ };
+
+ expect(callWithFunction(1)).to.throwError();
+ expect(callWithFunction("a string")).to.throwError();
+ expect(callWithFunction({})).to.throwError();
+ expect(callWithFunction(function(){})).to.not.throwError();
+ });
+});