diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/bundlers.browserify.test.js | 6 | ||||
-rw-r--r-- | test/bundlers.webpack.test.js | 7 | ||||
-rw-r--r-- | test/bundlers/browserify/index.html | 6 | ||||
-rw-r--r-- | test/bundlers/webpack/index.html | 2 | ||||
-rw-r--r-- | test/testHelpers/browserShims.js | 44 | ||||
-rw-r--r-- | test/testModules/sharedTestCases.js | 100 | ||||
-rw-r--r-- | test/testModules/someOtherModule.js | 5 |
7 files changed, 81 insertions, 89 deletions
diff --git a/test/bundlers.browserify.test.js b/test/bundlers.browserify.test.js index 43b6bb7..c454ba9 100644 --- a/test/bundlers.browserify.test.js +++ b/test/bundlers.browserify.test.js @@ -24,7 +24,8 @@ function runInFakeBrowserContext(src, filename) { encodeURIComponent: function () {}, decodeURIComponent: function () {}, document: {}, - console: console + console: console, + testEnv: "browserify" }; context.window = context; vm.runInNewContext(src, context, filename); @@ -34,7 +35,6 @@ describe("rewire bundled with browserify", function () { before(require("./testHelpers/createFakePackageJSON.js")); after(require("./testHelpers/removeFakePackageJSON.js")); it("should run all sharedTestCases without exception", function () { - return; var b = browserify({ debug: true }), @@ -49,7 +49,7 @@ describe("rewire bundled with browserify", function () { browserBundle = vmBundle; // Setup for mocha - browserBundle = "function enableTests() {" + browserBundle + "}"; + browserBundle = "function enableTests() { " + browserBundle + " }"; // Output for browser-testing fs.writeFileSync(browserOutput, browserBundle, "utf8"); diff --git a/test/bundlers.webpack.test.js b/test/bundlers.webpack.test.js index 53785de..3ee367b 100644 --- a/test/bundlers.webpack.test.js +++ b/test/bundlers.webpack.test.js @@ -25,7 +25,8 @@ function runInFakeBrowserContext(src, filename) { encodeURIComponent: function () {}, decodeURIComponent: function () {}, document: {}, - console: console + console: console, + testEnv: "webpack" }; context.window = context; vm.runInNewContext(src, context, filename); @@ -35,7 +36,6 @@ describe("rewire bundled with webpack", function () { before(require("./testHelpers/createFakePackageJSON.js")); after(require("./testHelpers/removeFakePackageJSON.js")); it("should run all sharedTestCases without exception", function (done) { - done(); return; var webpackOptions, src, outputPath = __dirname + "/bundlers/webpack/bundle.js", @@ -49,10 +49,9 @@ describe("rewire bundled with webpack", function () { configureWebpack(webpackOptions); webpack(__dirname + "/testModules/sharedTestCases.js", webpackOptions, function onWebpackFinished(err, stats) { - expect(err).to.be(null); expect(stats.errors).to.have.length(0); - //expect(stats.warnings).to.have.length(0); + expect(stats.warnings).to.have.length(0); // Read generated source src = fs.readFileSync(outputPath, "utf8"); diff --git a/test/bundlers/browserify/index.html b/test/bundlers/browserify/index.html index dc76f31..5374871 100644 --- a/test/bundlers/browserify/index.html +++ b/test/bundlers/browserify/index.html @@ -1,11 +1,13 @@ <!doctype html> <head> - <link rel="stylesheet" href="../../node_modules/mocha/mocha.css" /> - <script src="../../node_modules/mocha/mocha.js" type="text/javascript"></script> + <link rel="stylesheet" href="../../../node_modules/mocha/mocha.css" /> + <script src="../../../node_modules/mocha/mocha.js" type="text/javascript"></script> + <script src="../../testHelpers/browserShims.js" type="text/javascript"></script> <script src="bundle.js" type="text/javascript"></script> </head> <body> <script type="text/javascript"> + var testEnv = "browserify"; window.onload = function () { console.log("These tests will only work in all browsers with the console open"); mocha.setup({ diff --git a/test/bundlers/webpack/index.html b/test/bundlers/webpack/index.html index e9b2d63..5aea4b1 100644 --- a/test/bundlers/webpack/index.html +++ b/test/bundlers/webpack/index.html @@ -2,10 +2,12 @@ <head> <link rel="stylesheet" href="../../../node_modules/mocha/mocha.css" /> <script src="../../../node_modules/mocha/mocha.js" type="text/javascript"></script> + <script src="../../testHelpers/browserShims.js" type="text/javascript"></script> <script src="bundle.js" type="text/javascript"></script> </head> <body> <script type="text/javascript"> + var testEnv = "webpack"; window.onload = function () { console.log("These tests will only work in all browsers with the console open"); mocha.setup({ diff --git a/test/testHelpers/browserShims.js b/test/testHelpers/browserShims.js new file mode 100644 index 0000000..ef29307 --- /dev/null +++ b/test/testHelpers/browserShims.js @@ -0,0 +1,44 @@ +if (!Array.prototype.indexOf) { + Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) { + "use strict"; + if (this == null) { + throw new TypeError(); + } + var t = Object(this); + var len = t.length >>> 0; + if (len === 0) { + return -1; + } + var n = 0; + if (arguments.length > 0) { + n = Number(arguments[1]); + if (n != n) { // shortcut for verifying if it's NaN + n = 0; + } else if (n != 0 && n != Infinity && n != -Infinity) { + n = (n > 0 || -1) * Math.floor(Math.abs(n)); + } + } + if (n >= len) { + return -1; + } + var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); + for (; k < len; k++) { + if (k in t && t[k] === searchElement) { + return k; + } + } + return -1; + } +} + +if(!String.prototype.trim) { + String.prototype.trim = function () { + return this.replace(/^\s+|\s+$/g,''); + }; +} + +if(!Array.isArray) { + Array.isArray = function (vArg) { + return Object.prototype.toString.call(vArg) === "[object Array]"; + }; +}
\ No newline at end of file diff --git a/test/testModules/sharedTestCases.js b/test/testModules/sharedTestCases.js index c0918de..6001797 100644 --- a/test/testModules/sharedTestCases.js +++ b/test/testModules/sharedTestCases.js @@ -6,53 +6,36 @@ var path = require("path"), expect = require("expect.js"), rewire = require("rewire"); -var testModules = { - A: path.resolve(__dirname, "./moduleA.js"), - B: path.resolve(__dirname, "./moduleB.js"), - someOtherModule: path.resolve(__dirname, "./someOtherModule.js"), - emptyModule: path.resolve(__dirname, "./emptyModule.js"), - strictModule: path.resolve(__dirname, "./strictModule.js") - }; - - function checkForTypeError(err) { expect(err.constructor).to.be(TypeError); } -function cleanRequireCache() { - var moduleName, - modulePath; - - for (moduleName in testModules) { - if (testModules.hasOwnProperty(moduleName)) { - modulePath = testModules[moduleName]; - delete require.cache[modulePath]; - } - } -} - -describe("rewire " + (typeof window === "undefined"? "(node)": "(browser)"), function () { - afterEach(cleanRequireCache); // ensuring a clean test environment +describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + ")"), function () { it("should work like require()", function () { - var rewiredModule; + 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; - rewiredModule = rewire("./moduleA.js"); - delete rewiredModule.__set__; - delete rewiredModule.__get__; - expect(rewiredModule).to.eql(require("./moduleA.js")); - cleanRequireCache(); + 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"); - rewiredModule = rewire("../testModules/moduleA.js"); - delete rewiredModule.__set__; - delete rewiredModule.__get__; - expect(rewiredModule).to.eql(require("../testModules/moduleA.js")); - cleanRequireCache(); + someOtherModule.fs = "This has been changed"; - rewiredModule = rewire("./moduleA.js"); - delete rewiredModule.__set__; - delete rewiredModule.__get__; - expect(rewiredModule).to.eql(require("./moduleA.js")); - cleanRequireCache(); + 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"); }); it("should modify the module so it provides a __set__ - function", function () { expect(rewire("./moduleA.js").__set__).to.be.a(Function); @@ -162,25 +145,6 @@ describe("rewire " + (typeof window === "undefined"? "(node)": "(browser)"), fun } } }); - it("should cache the rewired module", function () { - var rewired; - - rewired = rewire("./someOtherModule.js"); - expect(require("./moduleA.js").someOtherModule).to.be(rewired); - cleanRequireCache(); - rewired = rewire("./someOtherModule.js", true); - expect(require("./moduleA.js").someOtherModule).to.be(rewired); - }); - it("should not cache the rewired module on demand", function () { - var rewired, - someOtherModule = require("./someOtherModule.js"); - - someOtherModule.fs = "This has been changed"; - - rewired = rewire("./someOtherModule.js", false); - expect(require("./moduleA.js").someOtherModule).not.to.be(rewired); - expect(require("./moduleA.js").someOtherModule.fs).to.be("This has been changed"); - }); 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 @@ -195,24 +159,4 @@ describe("rewire " + (typeof window === "undefined"? "(node)": "(browser)"), fun strictModule.doSomethingUnstrict(); }).to.throwException(checkForTypeError); }); - 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"); - }); - describe("#reset", function () { - it("should remove all rewired modules from cache", function () { - var rewiredModuleA = rewire("./moduleA.js"), - rewiredModuleB = rewire("./moduleB.js"); - - expect(require("./moduleA.js")).to.be(rewiredModuleA); - expect(require("./moduleB.js")).to.be(rewiredModuleB); - rewire.reset(); - expect(require("./moduleA.js")).not.to.be(rewiredModuleA); - expect(require("./moduleB.js")).not.to.be(rewiredModuleB); - }); - }); });
\ No newline at end of file diff --git a/test/testModules/someOtherModule.js b/test/testModules/someOtherModule.js index 203fb06..da38f4c 100644 --- a/test/testModules/someOtherModule.js +++ b/test/testModules/someOtherModule.js @@ -1,5 +1,6 @@ "use strict"; // run code in ES5 strict mode -__filename = "/test/testModules/someOtherModule.js"; // unifying filename for the pretty stack trace test +__filename = "/test/testModules/someOtherModule.js"; -exports.fs = {};
\ No newline at end of file +exports.fs = {}; +exports.filename = __filename;
\ No newline at end of file |