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
diff options
context:
space:
mode:
authorJohannes <mail@johannesewald.de>2012-06-04 03:29:39 +0400
committerJohannes <mail@johannesewald.de>2012-06-04 03:29:39 +0400
commitf36825ab2607e9444f9a29f5ed666817583d0aa3 (patch)
tree5da79c9a9ede869fbf2f9d5bd28687dabe899cee /lib/index.js
parentb4b2794f5a5e5977e048ff777ac53263144126e6 (diff)
added documentation
Diffstat (limited to 'lib/index.js')
-rw-r--r--lib/index.js21
1 files changed, 17 insertions, 4 deletions
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