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 <johannes.ewald@roomieplanet.de>2012-07-02 19:58:09 +0400
committerJohannes <johannes.ewald@roomieplanet.de>2012-07-02 19:58:09 +0400
commit8bfdd979dabc1a0ab2e85fd80548d66bccdd1b82 (patch)
treee0f246aaa958e135af053276b3a75c817e710fb1 /test
parent7d7eca0fbcecf677479b58ef8b84131cf656fd6d (diff)
- changed browserify version to 1.13.5
- fixed global var injection in the browser
Diffstat (limited to 'test')
-rw-r--r--test/browserify.browserifyRewire.test.js77
-rw-r--r--test/browserify/index.html (renamed from test/browser/index.html)2
-rw-r--r--test/getImportGlobalsSrc.test.js20
-rw-r--r--test/testModules/moduleA.js62
-rw-r--r--test/testModules/moduleB.js82
-rw-r--r--test/testModules/sharedTestCases.js37
6 files changed, 206 insertions, 74 deletions
diff --git a/test/browserify.browserifyRewire.test.js b/test/browserify.browserifyRewire.test.js
index 43c7082..e52a50c 100644
--- a/test/browserify.browserifyRewire.test.js
+++ b/test/browserify.browserifyRewire.test.js
@@ -20,7 +20,15 @@ function runInFakeBrowserContext(src, filename) {
afterEach: afterEach,
setTimeout: setTimeout
},
- console: console
+ console: console,
+ setTimeout: setTimeout,
+ clearTimeout: clearTimeout,
+ setInterval: setInterval,
+ clearInterval: clearInterval,
+ parseFloat: parseFloat,
+ parseInt: parseInt,
+ encodeURIComponent: function () {},
+ decodeURIComponent: function () {}
}, filename);
}
@@ -28,8 +36,10 @@ describe("browserifyRewire", function () {
before(require("./testHelpers/createFakePackageJSON.js"));
after(require("./testHelpers/removeFakePackageJSON.js"));
it("should attach __set__ and __get__ to the exports-obj", function (done) {
- var context,
+ var pathToBrowserfiyRewire = pathUtil.resolve(__dirname, "../lib/browserify/browserifyRewire.js"),
+ context,
exportsObj = {},
+ moduleObj = {exports: exportsObj},
returnedObj,
browserifyRewire;
@@ -38,13 +48,14 @@ describe("browserifyRewire", function () {
function moduleA() {
"use strict";
- browserifyRewire.register("/a.js", null, null);
+ browserifyRewire.register("/a.js", null, null, null);
returnedObj = browserifyRewire("/a.js", "/b.js");
}
function moduleB() {
"use strict";
- browserifyRewire.register("/b.js", setter, getter);
+
+ browserifyRewire.register("/b.js", moduleObj, setter, getter);
return exportsObj;
}
@@ -53,51 +64,60 @@ describe("browserifyRewire", function () {
return "/b.js";
}
- function fakeRequire(requirePath) {
- if (requirePath === "path") {
- return pathUtil;
- } else {
- return moduleB();
+ function fakeRequire(path, parentModulePath) {
+ var module;
+
+ if (path === "../browser/shims.js") {
+ return;
+ } else if (path === "../getImportGlobalsSrc.js") {
+ return require("../lib/getImportGlobalsSrc.js");
}
+
+ module = moduleB();
+
+ expect(path).to.be("/b.js");
+ expect(parentModulePath).to.be("/a.js");
+ fakeRequire.cache["/b.js"] = module;
+
+ return module;
}
fakeRequire.resolve = fakeResolve;
+ fakeRequire.cache = {};
function setter() {}
function getter() {}
context = {
- require: fakeRequire,
module: {},
console: console,
window: {
- browserifyRequire: {
- modules: {
- "/b.js": {
- _cached : {}
- }
- }
- }
- }
+ browserifyRequire: fakeRequire
+ },
+ require: fakeRequire
};
- fs.readFile(pathUtil.resolve(__dirname, "../lib/browserify/browserifyRewire.js"), "utf8", function onBrowserifyRewireRead(err, src) {
- vm.runInNewContext(src, context);
+ fs.readFile(pathToBrowserfiyRewire, "utf8", function onBrowserifyRewireRead(err, src) {
+ vm.runInNewContext(src, context, pathToBrowserfiyRewire);
browserifyRewire = context.module.exports;
moduleA();
- expect(returnedObj).not.to.be(exportsObj);
expect(returnedObj.__set__).to.be(setter);
expect(returnedObj.__get__).to.be(getter);
- expect(context.window.browserifyRequire.modules["/b.js"]._cached).to.be(returnedObj);
+ // Because browserify does not create the module-object newly when executing the module
+ // again we have to copy the module object deeply and store that copy in the
+ // cache. Therefore we're checking here if the returned exports-object and the
+ // cached module-object are an independent copy.
+ expect(returnedObj).not.to.be(exportsObj);
+ expect(context.window.browserifyRequire.cache["/b.js"]).not.to.be(moduleObj);
done();
});
});
- it("should run all sharedTestCases without exception", function (done) {
+ it("should run all sharedTestCases without exception", function () {
var b = browserify({debug: true}),
middleware = require("rewire").browserify,
- browserOutput = __dirname + "/browser/browseroutput.js",
+ browserOutput = __dirname + "/browserify/bundle.js",
browserBundle,
vmBundle;
@@ -108,6 +128,7 @@ describe("browserifyRewire", function () {
// Setup for mocha
browserBundle += 'window.onload = function () {' +
+ 'console.log("These tests will only work in all browsers with the console being opened");' +
'mocha.setup("bdd");' +
'window.browserifyRequire("/test/testModules/sharedTestCases.js");' +
'mocha.run();' +
@@ -116,11 +137,9 @@ describe("browserifyRewire", function () {
vmBundle += 'window.browserifyRequire("/test/testModules/sharedTestCases.js");';
// Output for browser-testing
- fs.mkdir(__dirname + "/browser", function onMkDir() {
- fs.writeFile(browserOutput, browserBundle, "utf8", done);
+ fs.writeFileSync(browserOutput, browserBundle, "utf8");
- // This should throw no exception.
- runInFakeBrowserContext(vmBundle, browserOutput);
- });
+ // This should throw no exception.
+ runInFakeBrowserContext(vmBundle, browserOutput);
});
}); \ No newline at end of file
diff --git a/test/browser/index.html b/test/browserify/index.html
index 46d63d9..8f869a5 100644
--- a/test/browser/index.html
+++ b/test/browserify/index.html
@@ -2,7 +2,7 @@
<head>
<link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
<script src="../../node_modules/mocha/mocha.js" type="text/javascript"></script>
- <script src="browseroutput.js" type="text/javascript"></script>
+ <script src="bundle.js" type="text/javascript"></script>
</head>
<body>
<div id="mocha"></div>
diff --git a/test/getImportGlobalsSrc.test.js b/test/getImportGlobalsSrc.test.js
index 065bd59..0d9b020 100644
--- a/test/getImportGlobalsSrc.test.js
+++ b/test/getImportGlobalsSrc.test.js
@@ -19,4 +19,24 @@ describe("getImportGlobalsSrc", function () {
expect(actualGlobals).to.eql(expectedGlobals);
expect(actualGlobals.length).to.be.above(1);
});
+ it("should ignore the given variables", function () {
+ var context = {
+ global: global
+ },
+ ignore = ["console", "setTimeout"],
+ src,
+ actualGlobals,
+ expectedGlobals = Object.keys(global);
+
+ src = getImportGlobalsSrc(ignore);
+ expectedGlobals = expectedGlobals.filter(function filterIgnoredVars(value) {
+ return ignore.indexOf(value) === -1;
+ });
+ vm.runInNewContext(src, context);
+ actualGlobals = Object.keys(context);
+ actualGlobals.sort();
+ expectedGlobals.sort();
+ expect(actualGlobals).to.eql(expectedGlobals);
+ expect(actualGlobals.length).to.be.above(1);
+ });
}); \ No newline at end of file
diff --git a/test/testModules/moduleA.js b/test/testModules/moduleA.js
index 6e655ab..1e14071 100644
--- a/test/testModules/moduleA.js
+++ b/test/testModules/moduleA.js
@@ -28,21 +28,67 @@ function readFileSync() {
}
function checkSomeGlobals() {
- if (typeof global === "undefined") {
- throw new ReferenceError("global is undefined");
+ if (typeof global !== "object") {
+ throw new ReferenceError("global is not an object");
}
- if (typeof console === "undefined") {
- throw new ReferenceError("console is undefined");
+ if (typeof console !== "object") {
+ throw new ReferenceError("console is not an object");
}
- if (typeof __filename === "undefined") {
- throw new ReferenceError("__filename is undefined");
+ if (typeof require !== "function") {
+ throw new ReferenceError("require is not a function");
}
- if (typeof __dirname === "undefined") {
- throw new ReferenceError("__dirname is undefined");
+ 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");
+ }
+ //TODO add accurate checks here
if (typeof setTimeout === "undefined") {
throw new ReferenceError("setTimeout is undefined");
}
+ if (typeof clearTimeout === "undefined") {
+ throw new ReferenceError("clearTimeout is undefined");
+ }
+ if (typeof setInterval === "undefined") {
+ throw new ReferenceError("setInterval is undefined");
+ }
+ if (typeof clearInterval === "undefined") {
+ throw new ReferenceError("clearInterval is undefined");
+ }
+ if (typeof Error === "undefined") {
+ throw new ReferenceError("Error is undefined");
+ }
+ if (typeof parseFloat === "undefined") {
+ throw new ReferenceError("parseFloat is undefined");
+ }
+ if (typeof parseInt === "undefined") {
+ throw new ReferenceError("parseInt is undefined");
+ }
+ if (typeof window === "undefined") {
+ if (typeof process === "undefined") {
+ throw new ReferenceError("process is undefined");
+ }
+ if (typeof Buffer === "undefined") {
+ throw new ReferenceError("Buffer is undefined");
+ }
+ } else {
+ if (typeof encodeURIComponent === "undefined") {
+ throw new ReferenceError("encodeURIComponent is undefined");
+ }
+ if (typeof decodeURIComponent === "undefined") {
+ throw new ReferenceError("decodeURIComponent is undefined");
+ }
+ }
}
function getConsole() {
diff --git a/test/testModules/moduleB.js b/test/testModules/moduleB.js
index 6e655ab..b7646bd 100644
--- a/test/testModules/moduleB.js
+++ b/test/testModules/moduleB.js
@@ -28,21 +28,67 @@ function readFileSync() {
}
function checkSomeGlobals() {
- if (typeof global === "undefined") {
- throw new ReferenceError("global is undefined");
+ if (typeof global !== "object") {
+ throw new ReferenceError("global is not an object");
}
- if (typeof console === "undefined") {
- throw new ReferenceError("console is undefined");
+ if (typeof console !== "object") {
+ throw new ReferenceError("console is not an object");
}
- if (typeof __filename === "undefined") {
- throw new ReferenceError("__filename is undefined");
+ if (typeof require !== "function") {
+ throw new ReferenceError("require is not a function");
}
- if (typeof __dirname === "undefined") {
- throw new ReferenceError("__dirname is undefined");
+ 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"); // should be false because we're setting module.exports at the bottom of this file
+ }
+ if (typeof __dirname !== "string") {
+ throw new ReferenceError("__dirname is not a string");
+ }
+ if (typeof __filename !== "string") {
+ throw new ReferenceError("__filename is not a string");
+ }
+ //TODO add accurate checks here
if (typeof setTimeout === "undefined") {
throw new ReferenceError("setTimeout is undefined");
}
+ if (typeof clearTimeout === "undefined") {
+ throw new ReferenceError("clearTimeout is undefined");
+ }
+ if (typeof setInterval === "undefined") {
+ throw new ReferenceError("setInterval is undefined");
+ }
+ if (typeof clearInterval === "undefined") {
+ throw new ReferenceError("clearInterval is undefined");
+ }
+ if (typeof Error === "undefined") {
+ throw new ReferenceError("Error is undefined");
+ }
+ if (typeof parseFloat === "undefined") {
+ throw new ReferenceError("parseFloat is undefined");
+ }
+ if (typeof parseInt === "undefined") {
+ throw new ReferenceError("parseInt is undefined");
+ }
+ if (typeof window === "undefined") {
+ if (typeof process === "undefined") {
+ throw new ReferenceError("process is undefined");
+ }
+ if (typeof Buffer === "undefined") {
+ throw new ReferenceError("Buffer is undefined");
+ }
+ } else {
+ if (typeof encodeURIComponent === "undefined") {
+ throw new ReferenceError("encodeURIComponent is undefined");
+ }
+ if (typeof decodeURIComponent === "undefined") {
+ throw new ReferenceError("decodeURIComponent is undefined");
+ }
+ }
}
function getConsole() {
@@ -54,12 +100,14 @@ function getFilename() {
}
// 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.someOtherModule = someOtherModule; \ No newline at end of file
+module.exports = {
+ setMyNumber: setMyNumber,
+ getMyNumber: getMyNumber,
+ setMyObj: setMyObj,
+ getMyObj: getMyObj,
+ readFileSync: readFileSync,
+ checkSomeGlobals: checkSomeGlobals,
+ getConsole: getConsole,
+ getFilename: getFilename,
+ someOtherModule: someOtherModule
+};
diff --git a/test/testModules/sharedTestCases.js b/test/testModules/sharedTestCases.js
index b705e40..5f0a78b 100644
--- a/test/testModules/sharedTestCases.js
+++ b/test/testModules/sharedTestCases.js
@@ -26,19 +26,13 @@ function cleanRequireCache() {
for (moduleName in testModules) {
if (testModules.hasOwnProperty(moduleName)) {
modulePath = testModules[moduleName];
- if (typeof window === "undefined") {
- delete require.cache[modulePath];
- } else {
- if (typeof window.browserifyRequire.modules[modulePath]._cached === "object") {
- delete window.browserifyRequire.modules[modulePath]._cached;
- }
- }
+ delete require.cache[modulePath];
}
}
}
describe("rewire " + (typeof window === "undefined"? "(node.js)": "(browser)"), function () {
- beforeEach(cleanRequireCache); // ensuring a clean test environment
+ afterEach(cleanRequireCache); // ensuring a clean test environment
it("should work like require()", function () {
expect(rewire("./moduleA.js")).to.be(require("./moduleA.js"));
cleanRequireCache();
@@ -62,7 +56,7 @@ describe("rewire " + (typeof window === "undefined"? "(node.js)": "(browser)"),
expect(require("fs").__set__).to.be(undefined);
expect(require("fs").__get__).to.be(undefined);
});
- it("should not influence global objects by default", function () {
+ it("should not override/influence global objects by default", function () {
// This should throw no exception
rewire("./moduleA.js").checkSomeGlobals();
rewire("./moduleB.js").checkSomeGlobals();
@@ -112,18 +106,14 @@ describe("rewire " + (typeof window === "undefined"? "(node.js)": "(browser)"),
newFilename = "myFile.js";
rewiredModuleA.__set__({
- console: consoleMock
- });
- rewiredModuleA.__set__("__filename", newFilename);
- rewiredModuleB.__set__({
- console: consoleMock
+ console: consoleMock,
+ __filename: newFilename
});
- rewiredModuleB.__set__("__filename", newFilename);
expect(rewiredModuleA.getConsole()).to.be(consoleMock);
- expect(rewiredModuleB.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()).to.be(newFilename);
+ expect(rewiredModuleB.getFilename()).not.to.be(newFilename);
});
it("should cache the rewired module", function () {
var rewired;
@@ -140,20 +130,29 @@ describe("rewire " + (typeof window === "undefined"? "(node.js)": "(browser)"),
rewired = rewire("./someOtherModule.js", false);
expect(require("./moduleA.js").someOtherModule).not.to.be(rewired);
});
- it("should not influence the original node require if nothing has been required within the rewired module", function () {
+ 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 () {
+ it("should preserve the strict mode (not IE)", function () {
var strictModule = rewire("./strictModule.js");
expect(function () {
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"),