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
path: root/test
diff options
context:
space:
mode:
authorJohannes Ewald <johannes.ewald@peerigon.com>2014-07-10 23:00:50 +0400
committerJohannes Ewald <johannes.ewald@peerigon.com>2014-07-10 23:00:50 +0400
commit5f68ec6e0828a9c80bf836018bb59b79345497f0 (patch)
treeab838ce5e3084e09e084a6b6ac8e9d1954413d4c /test
parentf6e3dd0b398e4e302add682de2eed0d97542e25c (diff)
Improve shared test cases
Diffstat (limited to 'test')
-rw-r--r--test/testModules/sharedTestCases.js80
1 files changed, 71 insertions, 9 deletions
diff --git a/test/testModules/sharedTestCases.js b/test/testModules/sharedTestCases.js
index 3672769..1ba1fae 100644
--- a/test/testModules/sharedTestCases.js
+++ b/test/testModules/sharedTestCases.js
@@ -2,20 +2,30 @@
// In case this module was in strict mode, all other modules called by this would also be strict.
// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
+// These shared test cases are used to check if the provided implementation of rewire is compatible
+// with the original rewire. Since you can use rewire with client-side bundlers like webpack we need
+// to test the implementation there again.
+// @see https://github.com/jhnns/rewire-webpack
+
var expect = require("expect.js"),
- rewire = require("rewire");
+ rewire = require("rewire"),
+ __set__Src = require("../../lib/__set__.js").toString(),
+ __get__Src = require("../../lib/__get__.js").toString(),
+ __with__Src = require("../../lib/__with__.js").toString();
function checkForTypeError(err) {
expect(err.constructor).to.be(TypeError);
}
describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + ")"), function () {
+
it("should work like require()", function () {
rewire("./moduleA.js").getFilename();
require("./moduleA.js").getFilename();
expect(rewire("./moduleA.js").getFilename()).to.eql(require("./moduleA.js").getFilename());
expect(rewire("../testModules/someOtherModule.js").filename).to.eql(require("../testModules/someOtherModule.js").filename);
});
+
it("should return a fresh instance of the module", function () {
var someOtherModule = require("./someOtherModule.js"),
rewiredSomeOtherModule;
@@ -24,6 +34,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
rewiredSomeOtherModule = rewire("./someOtherModule.js");
expect(rewiredSomeOtherModule.fs).not.to.be("This has been modified");
});
+
it("should not cache the rewired module", function () {
var rewired,
someOtherModule = require("./someOtherModule.js");
@@ -36,26 +47,42 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
expect(require("./moduleA.js").someOtherModule).to.be(someOtherModule);
expect(require("./moduleA.js").someOtherModule.fs).to.be("This has been changed");
});
- it("should modify the module so it provides a __set__ - function", function () {
- expect(rewire("./moduleA.js").__set__).to.be.a(Function);
- expect(rewire("./moduleB.js").__set__).to.be.a(Function);
+
+ // By comparing the src we can ensure that the provided __set__ function is our tested implementation
+ it("should modify the module so it provides the __set__ - function", function () {
+ expect(rewire("./moduleA.js").__set__.toString()).to.be(__set__Src);
+ expect(rewire("./moduleB.js").__set__.toString()).to.be(__set__Src);
+ });
+
+ // By comparing the src we can ensure that the provided __set__ function is our tested implementation
+ it("should modify the module so it provides the __get__ - function", function () {
+ expect(rewire("./moduleA.js").__get__.toString()).to.be(__get__Src);
+ expect(rewire("./moduleB.js").__get__.toString()).to.be(__get__Src);
});
- it("should modify the module so it provides a __get__ - function", function () {
- expect(rewire("./moduleA.js").__get__).to.be.a(Function);
- expect(rewire("./moduleB.js").__get__).to.be.a(Function);
+
+ // By comparing the src we can ensure that the provided __set__ function is our tested implementation
+ it("should modify the module so it provides the __with__ - function", function () {
+ expect(rewire("./moduleA.js").__with__.toString()).to.be(__with__Src);
+ expect(rewire("./moduleB.js").__with__.toString()).to.be(__with__Src);
});
+
it("should not influence other modules", function () {
rewire("./moduleA.js");
expect(require("./someOtherModule.js").__set__).to.be(undefined);
expect(require("./someOtherModule.js").__get__).to.be(undefined);
+ expect(require("./someOtherModule.js").__with__).to.be(undefined);
});
+
it("should not override/influence global objects by default", function () {
// This should throw no exception
rewire("./moduleA.js").checkSomeGlobals();
rewire("./moduleB.js").checkSomeGlobals();
});
- it("should provide the ability to set private vars", function () {
+
+ // This is just an integration test for the __set__ method
+ // You can find a full test for __set__ under /test/__set__.test.js
+ it("should provide a working __set__ method", function () {
var rewiredModuleA = rewire("./moduleA.js"),
newObj = {};
@@ -66,12 +93,37 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
expect(rewiredModuleA.getMyObj()).to.be(newObj);
rewiredModuleA.__set__("env", "ENVENV");
});
- it("should provide the ability to get private vars", function () {
+
+ // This is just an integration test for the __get__ method
+ // You can find a full test for __get__ under /test/__get__.test.js
+ it("should provide a working __get__ method", function () {
var rewiredModuleA = rewire("./moduleA.js");
expect(rewiredModuleA.__get__("myNumber")).to.be(rewiredModuleA.getMyNumber());
expect(rewiredModuleA.__get__("myObj")).to.be(rewiredModuleA.getMyObj());
});
+
+ // This is just an integration test for the __with__ method
+ // You can find a full test for __with__ under /test/__with__.test.js
+ it("should provide a working __with__ method", function () {
+ var rewiredModuleA = rewire("./moduleA.js"),
+ newObj = {};
+
+ expect(rewiredModuleA.getMyNumber()).to.be(0);
+ expect(rewiredModuleA.getMyObj()).to.not.be(newObj);
+
+ rewiredModuleA.__with__({
+ myNumber: 2,
+ myObj: newObj
+ })(function () {
+ expect(rewiredModuleA.getMyNumber()).to.be(2);
+ expect(rewiredModuleA.getMyObj()).to.be(newObj);
+ });
+
+ expect(rewiredModuleA.getMyNumber()).to.be(0);
+ expect(rewiredModuleA.getMyObj()).to.not.be(newObj);
+ });
+
it("should provide the ability to inject mocks", function (done) {
var rewiredModuleA = rewire("./moduleA.js"),
mockedFs = {
@@ -84,6 +136,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
rewiredModuleA.__set__("fs", mockedFs);
rewiredModuleA.readFileSync();
});
+
it("should not influence other modules when injecting mocks", function () {
var rewiredModuleA = rewire("./moduleA.js"),
someOtherModule,
@@ -93,6 +146,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
someOtherModule = require("./someOtherModule.js");
expect(someOtherModule.fs).not.to.be(mockedFs);
});
+
it("should provide the ability to mock global objects just within the module", function () {
var rewiredModuleA = rewire("./moduleA.js"),
rewiredModuleB = rewire("./moduleB.js"),
@@ -123,6 +177,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
expect(document === documentMock).to.be(false);
}
});
+
it("should be possible to mock global objects that are added on runtime", function () {
var rewiredModule;
@@ -144,19 +199,23 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
}
}
});
+
it("should not be a problem to have a comment on file end", function () {
var rewired = rewire("./emptyModule.js");
rewired.__set__("someVar", "hello");
expect(rewired.__get__("someVar")).to.be("hello");
});
+
it("should not influence the original require if nothing has been required within the rewired module", function () {
rewire("./emptyModule.js"); // nothing happens here because emptyModule doesn't require anything
expect(require("./moduleA.js").__set__).to.be(undefined); // if restoring the original node require didn't worked, the module would have a setter
});
+
it("subsequent calls of rewire should always return a new instance", function () {
expect(rewire("./moduleA.js")).not.to.be(rewire("./moduleA.js"));
});
+
it("should preserve the strict mode", function () {
var strictModule = rewire("./strictModule.js");
@@ -164,6 +223,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
strictModule.doSomethingUnstrict();
}).to.throwException(checkForTypeError);
});
+
it("should not modify line numbers in stack traces", function () {
var throwError = rewire("./throwError.js");
@@ -175,9 +235,11 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
}
}
});
+
it("should throw a TypeError if the path is not a string", function () {
expect(function () {
rewire(null);
}).to.throwException(checkForTypeError);
});
+
}); \ No newline at end of file