Comparing version 0.3.1 to 0.3.2
@@ -18,6 +18,2 @@ /** | ||
arguments.src = ""; | ||
arguments.checkExistsSrc = function (varName, varValue) { | ||
return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('Cannot __set__(" + varName + ", " + varValue + "): " + | ||
varName + " is not declared within the module.'); } "; | ||
}; | ||
@@ -32,3 +28,3 @@ if (typeof arguments[0] === "object" && arguments.length === 1) { | ||
arguments.varValue = arguments.env[arguments.varName]; | ||
arguments.src += arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.env." + arguments.varName + ";"; | ||
arguments.src += arguments.varName + " = arguments.env." + arguments.varName + "; "; | ||
} | ||
@@ -40,3 +36,3 @@ } | ||
} | ||
arguments.src = arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.varValue;"; | ||
arguments.src = arguments.varName + " = arguments.varValue;"; | ||
} else { | ||
@@ -43,0 +39,0 @@ throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name"); |
@@ -19,7 +19,5 @@ /** | ||
for (key in globalObj) { | ||
if (globalObj.hasOwnProperty === undefined || globalObj.hasOwnProperty(key)) { // in IE8 window.hasOwnProperty is undefined | ||
if (key !== "global" && ignore.indexOf(key) === -1) { | ||
value = globalObj[key]; | ||
src += "var " + key + " = global." + key + "; "; | ||
} | ||
if (key !== "global" && ignore.indexOf(key) === -1) { // we don't use hasOwnProperty here because in some browsers not all global objects will be enumerated | ||
value = globalObj[key]; | ||
src += "var " + key + " = global." + key + "; "; | ||
} | ||
@@ -26,0 +24,0 @@ } |
{ | ||
"name" : "rewire", | ||
"version" : "0.3.1", | ||
"version" : "0.3.2", | ||
"description" : "Dependency injection for node.js applications", | ||
@@ -33,3 +33,3 @@ "keywords" : [ | ||
"devDependencies": { | ||
"mocha": "1.2.x", | ||
"mocha": "1.3.x", | ||
"expect.js": "0.1.x", | ||
@@ -39,4 +39,4 @@ "browserify": ">=1.13.5 <1.14.x" | ||
"scripts" : { | ||
"test" : "mocha -R spec" | ||
"test" : "node node_modules/mocha/bin/mocha -R spec" | ||
} | ||
} |
@@ -10,3 +10,3 @@ var expect = require("expect.js"), | ||
return function expectReferenceError(err) { | ||
expect(err.constructor.name === ErrConstructor.name).to.be(true); | ||
expect(err.constructor.name).to.be(ErrConstructor.name); | ||
}; | ||
@@ -35,3 +35,3 @@ } | ||
it("should return the initial value", function () { | ||
expect(moduleFake.__get__("myNumber") === 0).to.be(true); | ||
expect(moduleFake.__get__("myNumber")).to.be(0); | ||
expect(moduleFake.__get__("myObj")).to.eql({}); | ||
@@ -44,4 +44,4 @@ }); | ||
moduleFake.setObj(newObj); | ||
expect(moduleFake.__get__("myNumber") === 2).to.be(true); | ||
expect(moduleFake.__get__("myObj") === newObj).to.be(true); | ||
expect(moduleFake.__get__("myNumber")).to.be(2); | ||
expect(moduleFake.__get__("myObj")).to.be(newObj); | ||
}); | ||
@@ -48,0 +48,0 @@ it("should throw a ReferenceError when getting not existing vars", function () { |
@@ -10,3 +10,3 @@ var expect = require("expect.js"), | ||
return function expectReferenceError(err) { | ||
expect(err.constructor.name === ErrConstructor.name).to.be(true); | ||
expect(err.constructor.name).to.be(ErrConstructor.name); | ||
}; | ||
@@ -20,11 +20,4 @@ } | ||
moduleFake = { | ||
myNumber: 0, // copy by value | ||
myObj: {}, // copy by reference | ||
// these variables are used within the set method | ||
// because there is a eval() statement within the set method | ||
// these variables should not override same-named vars of the module | ||
key: "key", | ||
env: "env", | ||
src: "src" | ||
myValue: 0, // copy by value | ||
myReference: {} // copy by reference | ||
}; | ||
@@ -34,18 +27,36 @@ | ||
"__set__ = " + __set__.toString() + "; " + | ||
"getNumber = function () { return myNumber; }; " + | ||
"getObj = function () { return myObj; }; ", | ||
"getValue = function () { return myValue; }; " + | ||
"getReference = function () { return myReference; }; ", | ||
moduleFake | ||
); | ||
}); | ||
it("should set the new number when calling with varName, varValue", function () { | ||
expect(moduleFake.getNumber() === 0).to.be(true); | ||
moduleFake.__set__("myNumber", 2); | ||
expect(moduleFake.getNumber() === 2).to.be(true); | ||
it("should set the new value when calling with varName, varValue", function () { | ||
expect(moduleFake.getValue()).to.be(0); | ||
moduleFake.__set__("myValue", undefined); | ||
expect(moduleFake.getValue()).to.be(undefined); | ||
moduleFake.__set__("myValue", null); | ||
expect(moduleFake.getValue()).to.be(null); | ||
moduleFake.__set__("myValue", 2); | ||
expect(moduleFake.getValue()).to.be(2); | ||
moduleFake.__set__("myValue", "hello"); | ||
expect(moduleFake.getValue()).to.be("hello"); | ||
}); | ||
it("should set the new object when calling with varName, varValue", function () { | ||
var newObj = { hello: "hello" }; | ||
it("should set the new reference when calling with varName, varValue", function () { | ||
var newObj = { hello: "hello" }, | ||
newArr = [1, 2, 3], | ||
regExp = /123/gi; | ||
expect(moduleFake.getObj()).to.eql({}); | ||
moduleFake.__set__("myObj", newObj); | ||
expect(moduleFake.getObj() === newObj).to.be(true); | ||
function newFn() { | ||
console.log("hello"); | ||
} | ||
expect(moduleFake.getReference()).to.eql({}); | ||
moduleFake.__set__("myReference", newObj); | ||
expect(moduleFake.getReference()).to.be(newObj); | ||
moduleFake.__set__("myReference", newArr); | ||
expect(moduleFake.getReference()).to.be(newArr); | ||
moduleFake.__set__("myReference", newFn); | ||
expect(moduleFake.getReference()).to.be(newFn); | ||
moduleFake.__set__("myReference", regExp); | ||
expect(moduleFake.getReference()).to.be(regExp); | ||
}); | ||
@@ -55,25 +66,14 @@ it("should set the new number and the new obj when calling with an env-obj", function () { | ||
expect(moduleFake.getNumber() === 0).to.be(true); | ||
expect(moduleFake.getObj()).to.eql({}); | ||
expect(moduleFake.getValue()).to.be(0); | ||
expect(moduleFake.getReference()).to.eql({}); | ||
moduleFake.__set__({ | ||
myNumber: 2, | ||
myObj: newObj | ||
myValue: 2, | ||
myReference: newObj | ||
}); | ||
expect(moduleFake.getNumber() === 2).to.be(true); | ||
expect(moduleFake.getObj() === newObj).to.be(true); | ||
expect(moduleFake.getValue()).to.be(2); | ||
expect(moduleFake.getReference()).to.be(newObj); | ||
}); | ||
it("should return undefined", function () { | ||
expect(moduleFake.__set__("myNumber", 4) === undefined).to.be(true); | ||
expect(moduleFake.__set__("myValue", 4)).to.be(undefined); | ||
}); | ||
it("should throw a ReferenceError when trying to set non-existing vars", function () { | ||
expect(function () { | ||
moduleFake.__set__("notExisting", 3); | ||
}).to.throwException(); | ||
expect(function () { | ||
moduleFake.__set__({ | ||
notExisting: "bla", | ||
notExistingAsWell: "blabla" | ||
}); | ||
}).to.throwException(expectReferenceError); | ||
}); | ||
it("should throw a TypeError when passing misfitting params", function () { | ||
@@ -80,0 +80,0 @@ expect(function () { |
@@ -39,3 +39,5 @@ var vm = require("vm"), | ||
it("should run all sharedTestCases without exception", function () { | ||
var b = browserify({debug: true}), | ||
var b = browserify({ | ||
//debug: true | ||
}), | ||
middleware = require("rewire").browserify, | ||
@@ -54,5 +56,2 @@ browserOutput = __dirname + "/browserify/bundle.js", | ||
/* | ||
vmBundle += 'window.browserifyRequire("/test/testModules/sharedTestCases.js");'; */ | ||
// Output for browser-testing | ||
@@ -59,0 +58,0 @@ fs.writeFileSync(browserOutput, browserBundle, "utf8"); |
@@ -6,9 +6,9 @@ var expect = require("expect.js"), | ||
it("should detect \"use strict\"; at the beginning of a string and ignore all whitespace before", function () { | ||
expect(detectStrictMode('"use strict";') === true).to.be(true); | ||
expect(detectStrictMode(' "use strict";') === true).to.be(true); | ||
expect(detectStrictMode(' \n "use strict";') === true).to.be(true); | ||
expect(detectStrictMode('"use strict";')).to.be(true); | ||
expect(detectStrictMode(' "use strict";')).to.be(true); | ||
expect(detectStrictMode(' \n "use strict";')).to.be(true); | ||
}); | ||
it("should not detect \"use strict\"; if it occurs in some nested function", function () { | ||
expect(detectStrictMode('function () {"use strict";}') === false).to.be(true); | ||
expect(detectStrictMode('function () {"use strict";}')).to.be(false); | ||
}); | ||
}); |
@@ -31,2 +31,14 @@ "use strict"; // run code in ES5 strict mode | ||
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") { | ||
@@ -56,12 +68,12 @@ throw new ReferenceError("global is not an object"); | ||
} | ||
if (typeof setTimeout !== "function") { | ||
if (typeof setTimeout !== typeOfGlobalFunc) { | ||
throw new ReferenceError("setTimeout is not a function"); | ||
} | ||
if (typeof clearTimeout !== "function") { | ||
if (typeof clearTimeout !== typeOfGlobalFunc) { | ||
throw new ReferenceError("clearTimeout is not a function"); | ||
} | ||
if (typeof setInterval !== "function") { | ||
if (typeof setInterval !== typeOfGlobalFunc) { | ||
throw new ReferenceError("setInterval is not a function"); | ||
} | ||
if (typeof clearInterval !== "function") { | ||
if (typeof clearInterval !== typeOfGlobalFunc) { | ||
throw new ReferenceError("clearInterval is not a function"); | ||
@@ -68,0 +80,0 @@ } |
@@ -31,2 +31,14 @@ "use strict"; // run code in ES5 strict mode | ||
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") { | ||
@@ -48,3 +60,3 @@ throw new ReferenceError("global is not an object"); | ||
if (module.exports === exports) { | ||
throw new Error("module.exports === exports returns true"); // because we're exporting via module.exports they should not be the same. | ||
throw new Error("module.exports === exports returns true"); | ||
} | ||
@@ -57,12 +69,12 @@ if (typeof __dirname !== "string") { | ||
} | ||
if (typeof setTimeout !== "function") { | ||
if (typeof setTimeout !== typeOfGlobalFunc) { | ||
throw new ReferenceError("setTimeout is not a function"); | ||
} | ||
if (typeof clearTimeout !== "function") { | ||
if (typeof clearTimeout !== typeOfGlobalFunc) { | ||
throw new ReferenceError("clearTimeout is not a function"); | ||
} | ||
if (typeof setInterval !== "function") { | ||
if (typeof setInterval !== typeOfGlobalFunc) { | ||
throw new ReferenceError("setInterval is not a function"); | ||
} | ||
if (typeof clearInterval !== "function") { | ||
if (typeof clearInterval !== typeOfGlobalFunc) { | ||
throw new ReferenceError("clearInterval is not a function"); | ||
@@ -69,0 +81,0 @@ } |
@@ -19,3 +19,3 @@ // Don't run code in ES5 strict mode. | ||
function checkForTypeError(err) { | ||
expect(err.constructor === TypeError).to.be(true); | ||
expect(err.constructor).to.be(TypeError); | ||
} | ||
@@ -38,7 +38,7 @@ | ||
it("should work like require()", function () { | ||
expect(rewire("./moduleA.js") === require("./moduleA.js")).to.be(true); | ||
expect(rewire("./moduleA.js")).to.be(require("./moduleA.js")); | ||
cleanRequireCache(); | ||
expect(rewire("../testModules/moduleA.js") === require("../testModules/moduleA.js")).to.be(true); | ||
expect(rewire("../testModules/moduleA.js")).to.be(require("../testModules/moduleA.js")); | ||
cleanRequireCache(); | ||
expect(rewire("./moduleA.js") === require("./moduleA.js")).to.be(true); | ||
expect(rewire("./moduleA.js")).to.be(require("./moduleA.js")); | ||
}); | ||
@@ -56,6 +56,6 @@ it("should modify the module so it provides a __set__ - function", function () { | ||
expect(require("./someOtherModule.js").__set__ === undefined).to.be(true); | ||
expect(require("./someOtherModule.js").__get__ === undefined).to.be(true); | ||
expect(require("fs").__set__ === undefined).to.be(true); | ||
expect(require("fs").__get__ === undefined).to.be(true); | ||
expect(require("./someOtherModule.js").__set__).to.be(undefined); | ||
expect(require("./someOtherModule.js").__get__).to.be(undefined); | ||
expect(require("fs").__set__).to.be(undefined); | ||
expect(require("fs").__get__).to.be(undefined); | ||
}); | ||
@@ -71,7 +71,7 @@ it("should not override/influence global objects by default", function () { | ||
expect(rewiredModuleA.getMyNumber() === 0).to.be(true); | ||
expect(rewiredModuleA.getMyNumber()).to.be(0); | ||
rewiredModuleA.__set__("myNumber", 2); | ||
expect(rewiredModuleA.getMyNumber() === 2).to.be(true); | ||
expect(rewiredModuleA.getMyNumber()).to.be(2); | ||
rewiredModuleA.__set__("myObj", newObj); | ||
expect(rewiredModuleA.getMyObj() === newObj).to.be(true); | ||
expect(rewiredModuleA.getMyObj()).to.be(newObj); | ||
rewiredModuleA.__set__("env", "ENVENV"); | ||
@@ -82,4 +82,4 @@ }); | ||
expect(rewiredModuleA.__get__("myNumber") === rewiredModuleA.getMyNumber()).to.be(true); | ||
expect(rewiredModuleA.__get__("myObj") === rewiredModuleA.getMyObj()).to.be(true); | ||
expect(rewiredModuleA.__get__("myNumber")).to.be(rewiredModuleA.getMyNumber()); | ||
expect(rewiredModuleA.__get__("myObj")).to.be(rewiredModuleA.getMyObj()); | ||
}); | ||
@@ -90,3 +90,3 @@ it("should provide the ability to inject mocks", function (done) { | ||
readFileSync: function (file) { | ||
expect(file === "bla.txt").to.be(true); | ||
expect(file).to.be("bla.txt"); | ||
done(); | ||
@@ -106,3 +106,3 @@ } | ||
someOtherModule = require("./someOtherModule.js"); | ||
expect(someOtherModule.fs === mockedFs).to.be(false); | ||
expect(someOtherModule.fs).not.to.be(mockedFs); | ||
}); | ||
@@ -121,18 +121,18 @@ it("should provide the ability to mock global objects just within the module", function () { | ||
}); | ||
expect(rewiredModuleA.getConsole() === consoleMock).to.be(true); | ||
expect(rewiredModuleB.getConsole() === consoleMock).to.be(false); | ||
expect(console === consoleMock).to.be(false); | ||
expect(rewiredModuleA.getFilename() === newFilename).to.be(true); | ||
expect(rewiredModuleB.getFilename() === newFilename).to.be(false); | ||
expect(console === newFilename).to.be(false); | ||
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() === bufferMock).to.be(true); | ||
expect(rewiredModuleB.getBuffer() === bufferMock).to.be(false); | ||
expect(Buffer === bufferMock).to.be(false); | ||
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() === documentMock).to.be(true); | ||
expect(rewiredModuleB.getDocument() === documentMock === false).to.be(true); | ||
expect(document === documentMock === false).to.be(true); | ||
expect(rewiredModuleA.getDocument()).to.be(documentMock); | ||
expect(rewiredModuleB.getDocument() === documentMock).to.be(false); | ||
expect(document === documentMock).to.be(false); | ||
} | ||
@@ -147,4 +147,4 @@ }); | ||
rewiredModule.__set__("someGlobalVar", "other value"); | ||
expect(global.someGlobalVar === "test").to.be(true); | ||
expect(rewiredModule.__get__("someGlobalVar") === "other value").to.be(true); | ||
expect(global.someGlobalVar).to.be("test"); | ||
expect(rewiredModule.__get__("someGlobalVar")).to.be("other value"); | ||
delete global.someGlobalVar; | ||
@@ -155,5 +155,7 @@ } else { | ||
rewiredModule.__set__("someGlobalVar", "other value"); | ||
expect(window.someGlobalVar === "test").to.be(true); | ||
expect(rewiredModule.__get__("someGlobalVar") === "other value").to.be(true); | ||
delete window.someGlobalVar; | ||
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; | ||
} | ||
} | ||
@@ -165,6 +167,6 @@ }); | ||
rewired = rewire("./someOtherModule.js"); | ||
expect(require("./moduleA.js").someOtherModule === rewired).to.be(true); | ||
expect(require("./moduleA.js").someOtherModule).to.be(rewired); | ||
cleanRequireCache(); | ||
rewired = rewire("./someOtherModule.js", true); | ||
expect(require("./moduleA.js").someOtherModule === rewired).to.be(true); | ||
expect(require("./moduleA.js").someOtherModule).to.be(rewired); | ||
}); | ||
@@ -178,12 +180,12 @@ it("should not cache the rewired module on demand", function () { | ||
rewired = rewire("./someOtherModule.js", false); | ||
expect(require("./moduleA.js").someOtherModule === rewired).to.be(false); | ||
expect(require("./moduleA.js").someOtherModule.fs === "This has been changed").to.be(true); | ||
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__ === undefined).to.be(true); // if restoring the original node require didn't worked, the module would have a setter | ||
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") === rewire("./moduleA.js")).to.be(false); | ||
expect(rewire("./moduleA.js")).not.to.be(rewire("./moduleA.js")); | ||
}); | ||
@@ -203,3 +205,3 @@ it("should preserve the strict mode (not IE)", function () { | ||
rewiredSomeOtherModule = rewire("./someOtherModule.js"); | ||
expect(rewiredSomeOtherModule.fs === "This has been modified").to.be(false); | ||
expect(rewiredSomeOtherModule.fs).not.to.be("This has been modified"); | ||
}); | ||
@@ -211,9 +213,9 @@ describe("#reset", function () { | ||
expect(require("./moduleA.js") === rewiredModuleA).to.be(true); | ||
expect(require("./moduleB.js") === rewiredModuleB).to.be(true); | ||
expect(require("./moduleA.js")).to.be(rewiredModuleA); | ||
expect(require("./moduleB.js")).to.be(rewiredModuleB); | ||
rewire.reset(); | ||
expect(require("./moduleA.js") === rewiredModuleA).to.be(false); | ||
expect(require("./moduleB.js") === rewiredModuleB).to.be(false); | ||
expect(require("./moduleA.js")).not.to.be(rewiredModuleA); | ||
expect(require("./moduleB.js")).not.to.be(rewiredModuleB); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107725
1257