Rewire
GitHub - jhnns/rewire: Easy monkey-patching for node.js unit tests
Limitations
Babel's ES module emulation
During the transpilation step from ESM to CJS modules, Babel renames internal variables. Rewire will not work in these cases (see #62). Other Babel transforms, however, should be fine. Another solution might be switching to babel-plugin-rewire.
Variables inside functions
Variables inside functions can not be changed by rewire. This is constrained by the language.
// myModule.js
(function () {
// Can't be changed by rewire
var someVariable;
})()
Modules that export primitives
rewire is not able to attach the __set__- and __get__-method if your module is just exporting a primitive. Rewiring does not work in this case.
// Will throw an error if it's loaded with rewire()
module.exports = 2;
Globals with invalid variable names
rewire imports global variables into the local scope by prepending a list of var declarations:
var someGlobalVar = global.someGlobalVar; If someGlobalVar is not a valid variable name, rewire just ignores it. In this case you're not able to override the global variable locally.
Special globals
Please be aware that you can't rewire eval() or the global object itself.
Backlinks