diff options
author | Johannes Ewald <johannes.ewald@peerigon.com> | 2015-11-16 00:02:50 +0300 |
---|---|---|
committer | Johannes Ewald <johannes.ewald@peerigon.com> | 2015-11-16 00:02:50 +0300 |
commit | a7bdea28cca4bf37ad43e4948997b0af48822d6a (patch) | |
tree | b7e65b3ab890045ec4ee21f74c49dad24d779ea4 /lib | |
parent | 5a8ba82b39f69f3750ddf83c6f6fc6f6e2ecb2a2 (diff) |
Move testLib into dedicated folder inside root folder
Diffstat (limited to 'lib')
-rw-r--r-- | lib/testModules/debuggerModule.js | 7 | ||||
-rw-r--r-- | lib/testModules/emptyModule.js | 5 | ||||
-rw-r--r-- | lib/testModules/implicitGlobal.js | 6 | ||||
-rw-r--r-- | lib/testModules/module.coffee | 3 | ||||
-rw-r--r-- | lib/testModules/moduleA.js | 135 | ||||
-rw-r--r-- | lib/testModules/moduleB.js | 137 | ||||
-rw-r--r-- | lib/testModules/node_modules/rewire/package.json | 3 | ||||
-rw-r--r-- | lib/testModules/sharedTestCases.js | 354 | ||||
-rw-r--r-- | lib/testModules/someOtherModule.js | 6 | ||||
-rw-r--r-- | lib/testModules/strictModule.js | 7 | ||||
-rw-r--r-- | lib/testModules/throwError.js | 3 |
11 files changed, 0 insertions, 666 deletions
diff --git a/lib/testModules/debuggerModule.js b/lib/testModules/debuggerModule.js deleted file mode 100644 index 9d2b7df..0000000 --- a/lib/testModules/debuggerModule.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; // run code in ES5 strict mode - -var myNumber = 0; - -module.exports = function () { - myNumber = 1; -};
\ No newline at end of file diff --git a/lib/testModules/emptyModule.js b/lib/testModules/emptyModule.js deleted file mode 100644 index 8b82a74..0000000 --- a/lib/testModules/emptyModule.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; // run code in ES5 strict mode - -var someVar; - -// Comment on file end. Hope this won't break anything
\ No newline at end of file diff --git a/lib/testModules/implicitGlobal.js b/lib/testModules/implicitGlobal.js deleted file mode 100644 index 3048742..0000000 --- a/lib/testModules/implicitGlobal.js +++ /dev/null @@ -1,6 +0,0 @@ -implicitGlobal = "this is an implicit global var ..." + - "yes, it's bad coding style but there are still some libs out there"; - -module.exports = function () { - return undefinedImplicitGlobal; -}; diff --git a/lib/testModules/module.coffee b/lib/testModules/module.coffee deleted file mode 100644 index 5487878..0000000 --- a/lib/testModules/module.coffee +++ /dev/null @@ -1,3 +0,0 @@ -fs = require "fs" - -exports.readFileSync = () -> fs.readFileSync()
\ No newline at end of file diff --git a/lib/testModules/moduleA.js b/lib/testModules/moduleA.js deleted file mode 100644 index b9dce87..0000000 --- a/lib/testModules/moduleA.js +++ /dev/null @@ -1,135 +0,0 @@ -"use strict"; // run code in ES5 strict mode - -var someOtherModule = require("./someOtherModule.js"), - myNumber = 0, // copy by value - myObj = {}, // copy by reference - env = "bla", - fs; - -// We need getters and setters for private vars to check if our injected setters and getters actual work -function setMyNumber(newNumber) { - myNumber = newNumber; -} - -function getMyNumber() { - return myNumber; -} - -function setMyObj(newObj) { - myObj = newObj; -} - -function getMyObj() { - return myObj; -} - -function readFileSync() { - fs.readFileSync("bla.txt", "utf8"); -} - -function checkSomeGlobals() { - var isLowerIE, - typeOfGlobalFunc; - - if (typeof navigator !== "undefined") { - isLowerIE = /MSIE [6-8]\.[0-9]/g.test(navigator.userAgent); - } - if (isLowerIE) { - typeOfGlobalFunc = "object"; - } else { - typeOfGlobalFunc = "function"; - } - - if (typeof global !== "object") { - throw new ReferenceError("global is not an object"); - } - if (typeof console !== "object") { - throw new ReferenceError("console is not an object"); - } - if (typeof require !== "function") { - throw new ReferenceError("require is not a function"); - } - if (typeof module !== "object") { - throw new ReferenceError("module is not an object"); - } - if (typeof exports !== "object") { - throw new ReferenceError("exports is not an object"); - } - if (module.exports !== exports) { - throw new Error("module.exports === exports returns false"); - } - if (typeof __dirname !== "string") { - throw new ReferenceError("__dirname is not a string"); - } - if (typeof __filename !== "string") { - throw new ReferenceError("__filename is not a string"); - } - if (typeof setTimeout !== typeOfGlobalFunc) { - throw new ReferenceError("setTimeout is not a function"); - } - if (typeof clearTimeout !== typeOfGlobalFunc) { - throw new ReferenceError("clearTimeout is not a function"); - } - if (typeof setInterval !== typeOfGlobalFunc) { - throw new ReferenceError("setInterval is not a function"); - } - if (typeof clearInterval !== typeOfGlobalFunc) { - throw new ReferenceError("clearInterval is not a function"); - } - if (typeof Error !== "function") { - throw new ReferenceError("Error is not a function"); - } - if (typeof parseFloat !== "function") { - throw new ReferenceError("parseFloat is not a function"); - } - if (typeof parseInt !== "function") { - throw new ReferenceError("parseInt is not a function"); - } - if (typeof window === "undefined") { - if (typeof process !== "object") { - throw new ReferenceError("process is not an object"); - } - if (typeof Buffer !== "function") { - throw new ReferenceError("Buffer is not a function"); - } - } else { - if (typeof encodeURIComponent !== "function") { - throw new ReferenceError("encodeURIComponent is not a function"); - } - if (typeof decodeURIComponent !== "function") { - throw new ReferenceError("decodeURIComponent is not a function"); - } - if (typeof document !== "object") { - throw new ReferenceError("document is not an object"); - } - } -} - -function getConsole() { - return console; -} - -function getFilename() { - return __filename; -} - -function getBuffer() { - return Buffer; -} - -function getDocument() { - return document; -} - -// different styles of exports in moduleA.js and moduleB.js -exports.setMyNumber = setMyNumber; -exports.getMyNumber = getMyNumber; -exports.setMyObj = setMyObj; -exports.getMyObj = getMyObj; -exports.readFileSync = readFileSync; -exports.checkSomeGlobals = checkSomeGlobals; -exports.getConsole = getConsole; -exports.getFilename = getFilename; -exports.getBuffer = getBuffer; -exports.getDocument = getDocument; -exports.someOtherModule = someOtherModule;
\ No newline at end of file diff --git a/lib/testModules/moduleB.js b/lib/testModules/moduleB.js deleted file mode 100644 index 62b2d3f..0000000 --- a/lib/testModules/moduleB.js +++ /dev/null @@ -1,137 +0,0 @@ -"use strict"; // run code in ES5 strict mode - -var someOtherModule = require("./someOtherModule.js"), - myNumber = 0, // copy by value - myObj = {}, // copy by reference - env = "bla", - fs; - -// We need getters and setters for private vars to check if our injected setters and getters actual work -function setMyNumber(newNumber) { - myNumber = newNumber; -} - -function getMyNumber() { - return myNumber; -} - -function setMyObj(newObj) { - myObj = newObj; -} - -function getMyObj() { - return myObj; -} - -function readFileSync() { - fs.readFileSync("bla.txt", "utf8"); -} - -function checkSomeGlobals() { - var isLowerIE, - typeOfGlobalFunc; - - if (typeof navigator !== "undefined") { - isLowerIE = /MSIE [6-8]\.[0-9]/g.test(navigator.userAgent); - } - if (isLowerIE) { - typeOfGlobalFunc = "object"; - } else { - typeOfGlobalFunc = "function"; - } - - if (typeof global !== "object") { - throw new ReferenceError("global is not an object"); - } - if (typeof console !== "object") { - throw new ReferenceError("console is not an object"); - } - if (typeof require !== "function") { - throw new ReferenceError("require is not a function"); - } - if (typeof module !== "object") { - throw new ReferenceError("module is not an object"); - } - if (typeof exports !== "object") { - throw new ReferenceError("exports is not an object"); - } - if (module.exports === exports) { - throw new Error("module.exports === exports returns true"); - } - if (typeof __dirname !== "string") { - throw new ReferenceError("__dirname is not a string"); - } - if (typeof __filename !== "string") { - throw new ReferenceError("__filename is not a string"); - } - if (typeof setTimeout !== typeOfGlobalFunc) { - throw new ReferenceError("setTimeout is not a function"); - } - if (typeof clearTimeout !== typeOfGlobalFunc) { - throw new ReferenceError("clearTimeout is not a function"); - } - if (typeof setInterval !== typeOfGlobalFunc) { - throw new ReferenceError("setInterval is not a function"); - } - if (typeof clearInterval !== typeOfGlobalFunc) { - throw new ReferenceError("clearInterval is not a function"); - } - if (typeof Error !== "function") { - throw new ReferenceError("Error is not a function"); - } - if (typeof parseFloat !== "function") { - throw new ReferenceError("parseFloat is not a function"); - } - if (typeof parseInt !== "function") { - throw new ReferenceError("parseInt is not a function"); - } - if (typeof window === "undefined") { - if (typeof process !== "object") { - throw new ReferenceError("process is not an object"); - } - if (typeof Buffer !== "function") { - throw new ReferenceError("Buffer is not a function"); - } - } else { - if (typeof encodeURIComponent !== "function") { - throw new ReferenceError("encodeURIComponent is not a function"); - } - if (typeof decodeURIComponent !== "function") { - throw new ReferenceError("decodeURIComponent is not a function"); - } - if (typeof document !== "object") { - throw new ReferenceError("document is not an object"); - } - } -} - -function getConsole() { - return console; -} - -function getFilename() { - return __filename; -} - -function getBuffer() { - return Buffer; -} - -function getDocument() { - return document; -} - -// different styles of exports in moduleA.js and moduleB.js -module.exports = { - setMyNumber: setMyNumber, - getMyNumber: getMyNumber, - setMyObj: setMyObj, - getMyObj: getMyObj, - readFileSync: readFileSync, - checkSomeGlobals: checkSomeGlobals, - getConsole: getConsole, - getFilename: getFilename, - getBuffer: getBuffer, - getDocument: getDocument, - someOtherModule: someOtherModule -}; diff --git a/lib/testModules/node_modules/rewire/package.json b/lib/testModules/node_modules/rewire/package.json deleted file mode 100644 index d8927cd..0000000 --- a/lib/testModules/node_modules/rewire/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "main": "../../../../lib/index.js" -}
\ No newline at end of file diff --git a/lib/testModules/sharedTestCases.js b/lib/testModules/sharedTestCases.js deleted file mode 100644 index ccbafa3..0000000 --- a/lib/testModules/sharedTestCases.js +++ /dev/null @@ -1,354 +0,0 @@ -// Don't run code in ES5 strict mode. -// 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"), - __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; - - someOtherModule.fs = "This has been modified"; - 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"); - - someOtherModule.fs = "This has been changed"; - - rewired = rewire("./someOtherModule.js"); - expect(someOtherModule).not.to.be(rewired); - expect(require("./moduleA.js").someOtherModule).not.to.be(rewired); - expect(require("./moduleA.js").someOtherModule).to.be(someOtherModule); - expect(require("./moduleA.js").someOtherModule.fs).to.be("This has been changed"); - }); - - // 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); - }); - - // 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); - }); - - - ["__get__", "__set__", "__with__"].forEach(function(funcName) { - it("should provide " + funcName + " as a non-enumerable property", function () { - expect(Object.keys(rewire("./moduleA.js")).indexOf(funcName)).to.be(-1) - }); - - it("should provide " + funcName + " as a writable property", function () { - var obj = rewire("./moduleA.js"); - var desc = Object.getOwnPropertyDescriptor(obj, funcName); - expect(desc.writable).to.be(true); - }); - }); - - 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(); - }); - - // 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 = {}; - - expect(rewiredModuleA.getMyNumber()).to.be(0); - rewiredModuleA.__set__("myNumber", 2); - expect(rewiredModuleA.getMyNumber()).to.be(2); - rewiredModuleA.__set__("myObj", newObj); - expect(rewiredModuleA.getMyObj()).to.be(newObj); - rewiredModuleA.__set__("env", "ENVENV"); - }); - - // 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 = { - readFileSync: function (file) { - expect(file).to.be("bla.txt"); - done(); - } - }; - - rewiredModuleA.__set__("fs", mockedFs); - rewiredModuleA.readFileSync(); - }); - - it("should not influence other modules when injecting mocks", function () { - var rewiredModuleA = rewire("./moduleA.js"), - someOtherModule, - mockedFs = {}; - - rewiredModuleA.__set__("fs", mockedFs); - 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"), - consoleMock = {}, - bufferMock = {}, - documentMock = {}, - newFilename = "myFile.js"; - - rewiredModuleA.__set__({ - console: consoleMock, - __filename: newFilename - }); - expect(rewiredModuleA.getConsole()).to.be(consoleMock); - expect(rewiredModuleB.getConsole()).not.to.be(consoleMock); - expect(console).not.to.be(consoleMock); - expect(rewiredModuleA.getFilename()).to.be(newFilename); - expect(rewiredModuleB.getFilename()).not.to.be(newFilename); - expect(console).not.to.be(newFilename); - if (typeof window === "undefined") { - rewiredModuleA.__set__("Buffer", bufferMock); - expect(rewiredModuleA.getBuffer()).to.be(bufferMock); - expect(rewiredModuleB.getBuffer()).not.to.be(bufferMock); - expect(Buffer).not.to.be(bufferMock); - } else { - rewiredModuleA.__set__("document", documentMock); - expect(rewiredModuleA.getDocument()).to.be(documentMock); - expect(rewiredModuleB.getDocument() === documentMock).to.be(false); - expect(document === documentMock).to.be(false); - } - }); - - it("should be possible to mock global objects that are added on runtime", function () { - var rewiredModule; - - if (typeof window === "undefined") { - global.someGlobalVar = "test"; - rewiredModule = rewire("./moduleA.js"); - rewiredModule.__set__("someGlobalVar", "other value"); - expect(global.someGlobalVar).to.be("test"); - expect(rewiredModule.__get__("someGlobalVar")).to.be("other value"); - delete global.someGlobalVar; - } else { - window.someGlobalVar = "test"; - rewiredModule = rewire("./moduleA.js"); - rewiredModule.__set__("someGlobalVar", "other value"); - expect(window.someGlobalVar).to.be("test"); - expect(rewiredModule.__get__("someGlobalVar")).to.be("other value"); - if (typeof navigator !== "undefined" && /MSIE [6-8]\.[0-9]/g.test(navigator.userAgent) === false) { - delete window.someGlobalVar; - } - } - }); - - 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"); - - expect(function () { - strictModule.doSomethingUnstrict(); - }).to.throwException(checkForTypeError); - }); - - it("should not modify line numbers in stack traces", function () { - var throwError = rewire("./throwError.js"); - - try { - throwError(); - } catch (err) { - if (err.stack) { - expect(err.stack.split("\n")[1]).to.match(/:2:11/); - } - } - }); - - it("should be possible to set implicit globals", function () { - var implicitGlobalModule, - err; - - 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; - } - - if (err) { - throw err; - } - }); - - it("should throw a TypeError if the path is not a string", function () { - expect(function () { - rewire(null); - }).to.throwException(checkForTypeError); - }); - - it("should also revert nested changes (with dot notation)", function () { - var rewiredModuleA = rewire("./moduleA.js"), - revert; - - revert = rewiredModuleA.__set__("myObj.test", true); - expect(rewiredModuleA.getMyObj()).to.eql({ - test: true - }); - revert(); - // This test also demonstrates a known drawback of the current implementation - // If the value doesn't exist at the time it is about to be set, it will be - // reverted to undefined instead deleting it from the object - // However, this is probably not a real world use-case because why would you - // want to mock something when it is not set. - expect(rewiredModuleA.getMyObj()).to.eql({ - test: undefined - }); - - revert = rewiredModuleA.__set__({ - "myObj.test": true - }); - expect(rewiredModuleA.getMyObj()).to.eql({ - test: true - }); - revert(); - expect(rewiredModuleA.getMyObj()).to.eql({ - test: undefined - }); - - }); - - 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; - } - }); - - it("should be possible to mock and revert JSON.parse (see #40)", function () { - var moduleA = rewire("./moduleA.js"), - revert; - - revert = moduleA.__set__({ - JSON: { - parse: function () { return true; } - } - }); - - revert(); - }); - -}); diff --git a/lib/testModules/someOtherModule.js b/lib/testModules/someOtherModule.js deleted file mode 100644 index da38f4c..0000000 --- a/lib/testModules/someOtherModule.js +++ /dev/null @@ -1,6 +0,0 @@ -"use strict"; // run code in ES5 strict mode - -__filename = "/test/testModules/someOtherModule.js"; - -exports.fs = {}; -exports.filename = __filename;
\ No newline at end of file diff --git a/lib/testModules/strictModule.js b/lib/testModules/strictModule.js deleted file mode 100644 index 5d6e387..0000000 --- a/lib/testModules/strictModule.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; // run code in ES5 strict mode - -function doSomethingUnstrict() { - var caller = arguments.callee.caller; // this should throw an error as a proof that strict mode is on -} - -exports.doSomethingUnstrict = doSomethingUnstrict;
\ No newline at end of file diff --git a/lib/testModules/throwError.js b/lib/testModules/throwError.js deleted file mode 100644 index 9bdf68b..0000000 --- a/lib/testModules/throwError.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = function () { - throw new Error(); -}; |