Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.js « lib - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a6dbcd6a8a12735cbd401d5bcd8faa944610de29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"use strict"; // run code in ES5 strict mode

var rewireModule = require("./rewire.js");

/**
 * 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, 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 rewireModule(module.parent, request, cache);
}

rewire.reset = rewireModule.reset;

module.exports = rewire;