function rewire(filename) {
return rewireModule(module.parent, filename);
}n/a
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");
}
}
}...
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"),
...function getBuffer() {
return Buffer;
}...
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);
...function getConsole() {
return console;
}...
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);
...function getDocument() {
return document;
}...
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;
...function getFilename() {
return __filename;
}...
function checkForTypeError(err) {
expect(err.constructor).to.be(TypeError);
}
describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + ")
x22;), 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("../testLib/someOtherModule.js").filename).to.eql(require("../testLib/someOtherModule.js").
filename);
});
it("should return a fresh instance of the module", function () {
var someOtherModule = require("./someOtherModule.js"),
...function getMyNumber() {
return myNumber;
}...
// 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");
});
...function getMyObj() {
return myObj;
}...
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");
...function readFileSync() {
fs.readFileSync("bla.txt", "utf8");
}...
function restoreExtensions() {
if ("coffee" in originalExtensions) {
require.extensions[".coffee"] = originalExtensions.coffee;
}
}
function coffeeExtension(module, filename) {
var content = stripBOM(fs.readFileSync(filename, "utf8"));
content = coffee.compile(content, {
filename: filename,
bare: true
});
module._compile(content, filename);
}
...function setMyNumber(newNumber) {
myNumber = newNumber;
}n/a
function setMyObj(newObj) {
myObj = newObj;
}n/a
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");
}
}
}...
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"),
...function getBuffer() {
return Buffer;
}...
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);
...function getConsole() {
return console;
}...
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);
...function getDocument() {
return document;
}...
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;
...function getFilename() {
return __filename;
}...
function checkForTypeError(err) {
expect(err.constructor).to.be(TypeError);
}
describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + ")
x22;), 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("../testLib/someOtherModule.js").filename).to.eql(require("../testLib/someOtherModule.js").
filename);
});
it("should return a fresh instance of the module", function () {
var someOtherModule = require("./someOtherModule.js"),
...function getMyNumber() {
return myNumber;
}...
// 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");
});
...function getMyObj() {
return myObj;
}...
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");
...function readFileSync() {
fs.readFileSync("bla.txt", "utf8");
}...
function restoreExtensions() {
if ("coffee" in originalExtensions) {
require.extensions[".coffee"] = originalExtensions.coffee;
}
}
function coffeeExtension(module, filename) {
var content = stripBOM(fs.readFileSync(filename, "utf8"));
content = coffee.compile(content, {
filename: filename,
bare: true
});
module._compile(content, filename);
}
...function setMyNumber(newNumber) {
myNumber = newNumber;
}n/a
function setMyObj(newObj) {
myObj = newObj;
}n/a
function inject(prelude, appendix) {
Module.wrapper[0] = moduleWrapper0 + prelude;
Module.wrapper[1] = appendix + moduleWrapper1;
}...
// Check if the module uses the strict mode.
// If so we must ensure that "use strict"; stays at the beginning of the module.
src = fs.readFileSync(targetPath, "utf8");
if (detectStrictMode(src) === true) {
prelude = ' "use strict"; ' + prelude;
}
moduleEnv.inject(prelude, appendix);
moduleEnv.load(targetModule);
return targetModule.exports;
}
module.exports = internalRewire;
...function load(targetModule) {
nodeRequire = targetModule.require;
targetModule.require = requireProxy;
currentModule = targetModule;
registerExtensions();
targetModule.load(targetModule.id);
// This is only necessary if nothing has been required within the module
reset();
}...
function load(targetModule) {
nodeRequire = targetModule.require;
targetModule.require = requireProxy;
currentModule = targetModule;
registerExtensions();
targetModule.load(targetModule.id);
// This is only necessary if nothing has been required within the module
reset();
}
function reset() {
Module.wrapper[0] = moduleWrapper0;
...function doSomethingUnstrict() {
var caller = arguments.callee.caller; // this should throw an error as a proof that strict mode is on
}...
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");
if (process.env.running_under_istanbul === "1") {
...