diff options
author | Johannes <mail@johannesewald.de> | 2012-06-04 03:29:39 +0400 |
---|---|---|
committer | Johannes <mail@johannesewald.de> | 2012-06-04 03:29:39 +0400 |
commit | f36825ab2607e9444f9a29f5ed666817583d0aa3 (patch) | |
tree | 5da79c9a9ede869fbf2f9d5bd28687dabe899cee /lib | |
parent | b4b2794f5a5e5977e048ff777ac53263144126e6 (diff) |
added documentation
Diffstat (limited to 'lib')
-rw-r--r-- | lib/getInjectionSrc.js | 15 | ||||
-rw-r--r-- | lib/getLeakingSrc.js | 9 | ||||
-rw-r--r-- | lib/index.js | 21 | ||||
-rw-r--r-- | lib/rewire.js | 9 |
4 files changed, 43 insertions, 11 deletions
diff --git a/lib/getInjectionSrc.js b/lib/getInjectionSrc.js index 67c15ec..74174d0 100644 --- a/lib/getInjectionSrc.js +++ b/lib/getInjectionSrc.js @@ -2,7 +2,16 @@ var toSrc = require("toSrc");
-function getMonkeyPatchSrc(obj) {
+/**
+ * Returns the source code for injecting vars.
+ *
+ * e.g.:
+ * "var console=123;"
+ *
+ * @param {Object} obj
+ * @return {String}
+ */
+function getInjectionSrc(obj) {
function walkObj(obj, level) {
var key,
value,
@@ -12,7 +21,7 @@ function getMonkeyPatchSrc(obj) { if (obj.hasOwnProperty(key)) {
value = obj[key];
if (level === 0) {
- src += "var "; // on the top level, we need a var statement to override variables
+ src += "var "; // on the top level we need a var statement
}
src += key + "=" + toSrc(value, 9999) + ";";
}
@@ -25,4 +34,4 @@ function getMonkeyPatchSrc(obj) { return walkObj(obj, 0);
}
-module.exports = getMonkeyPatchSrc;
\ No newline at end of file +module.exports = getInjectionSrc;
\ No newline at end of file diff --git a/lib/getLeakingSrc.js b/lib/getLeakingSrc.js index c827411..8475908 100644 --- a/lib/getLeakingSrc.js +++ b/lib/getLeakingSrc.js @@ -1,5 +1,14 @@ "use strict"; // run code in ES5 strict mode
+/**
+ * Returns the source code that will leak private vars.
+ *
+ * e.g.:
+ * "exports.__ = {myPrivateVar: myPrivateVar};"
+ *
+ * @param {Array<String>} leaks
+ * @return {String}
+ */
function getLeakingSrc(leaks) {
var src = "exports.__ = {",
varName,
diff --git a/lib/index.js b/lib/index.js index 097a7b8..321b65c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,26 @@ "use strict"; // run code in ES5 strict mode
-var rewire = require("./rewire.js");
+var rewireModule = require("./rewire.js");
-module.exports = function (request, mocks, injections, leaks, cache) {
+/**
+ * This function is needed to determine the calling parent module.
+ * Thus rewire acts exactly the same like require() in the test module.
+ *
+ * @param {!String} request Path to the module that shall be rewired. Use it exactly like require().
+ * @param {Object} mocks An object with mocks. Keys should be the exactly same like they're required in the target module. So if you write require("../../myModules/myModuleA.js") you need to pass {"../../myModules/myModuleA.js": myModuleAMock}.
+ * @param {Object} injections If you pass an object, all keys of the object will be vars within the module. You can also eval a string. Please note: All scripts are injected at the end of the module. So if there is any code in your module that is executed during require(), your injected variables will be undefined at this point. For example: passing {console: {...}} will cause all calls of console.log() to throw an exception if they're executed during require().
+ * @param {Array} leaks An array with variable names that should be exported. These variables are accessible via myModule.__
+ * @param {Boolean} cache Indicates whether the rewired module should be cached by node so subsequent calls of require() will return the rewired module. Subsequent calls of rewire() will always overwrite the cache.
+ * @return {*} the rewired module
+ */
+function rewire(request, mocks, injections, leaks, cache) {
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date
if (cache === undefined) {
cache = true;
}
- return rewire(module.parent, request, mocks, injections, leaks, cache);
-};
\ No newline at end of file + return rewireModule(module.parent, request, mocks, injections, leaks, cache);
+}
+
+module.exports = rewire;
\ No newline at end of file diff --git a/lib/rewire.js b/lib/rewire.js index 0f96e4d..6f1ac7d 100644 --- a/lib/rewire.js +++ b/lib/rewire.js @@ -10,7 +10,10 @@ function restoreOriginalWrappers() { Module.wrapper[1] = nodeWrapper1;
}
-function rewire(parentModule, filename, mocks, injections, leaks, cache) {
+/**
+ * Does actual rewiring the module. For further documentation @see index.js
+ */
+module.exports = function doRewire(parentModule, filename, mocks, injections, leaks, cache) {
var testModule,
nodeRequire,
wrapperExtensions = "";
@@ -60,6 +63,4 @@ function rewire(parentModule, filename, mocks, injections, leaks, cache) { restoreOriginalWrappers(); // this is only necessary if nothing has been required within the module
return testModule.exports;
-}
-
-module.exports = rewire;
\ No newline at end of file +};
\ No newline at end of file |