Easy dependency injection for node.js unit testing
Easy dependency injection for node.js unit testing.
rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may
- inject mocks for other modules or globals like
- leak private variables
- override variables within the module.
rewire does not load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).
Good news to all caffeine-addicts: rewire works also with Coffee-Script. Note that in this case CoffeeScript needs to be listed in your devDependencies.
If you want to use rewire also on the client-side take a look at client-side bundlers
Imagine you want to test this module:
// With rewire you can change all these variablesvar fs = require"fs"path = "/somewhere/on/the/disk";console.log"Reading from file system ...";fsreadFilepath "utf8" cb;exportsreadSomethingFromFileSystem = readSomethingFromFileSystem;
Now within your test module:
var rewire = require"rewire";var myModule = rewire"../lib/myModule.js";
rewire acts exactly like require. Just with one difference: Your module will now export a special setter and getter for private variables.
myModule__set__"path" "/dev/null";myModule__get__"path"; // = '/dev/null'
This allows you to mock everything in the top-level scope of the module, like the fs module for example. Just pass the variable name as first parameter and your mock as second.
var fsMock =expectpathtoequal"/somewhere/on/the/disk";cbnull "Success!";;myModule__set__"fs" fsMock;myModulereadSomethingFromFileSystemconsole.logdata; // = Success!;
You can also set multiple variables with one call.
myModule__set__fs: fsMockpath: "/dev/null";
You may also override globals. These changes are only within the module, so you don't have to be concerned that other modules are influenced by your mock.
myModule__set__console:/* be quiet */process:argv: "testArg1" "testArg2";
__set__ returns a function which reverts the changes introduced by this particular
var revert = myModule__set__"port" 3000;// port is now 3000revert;// port is now the previous value
For your convenience you can also use the
__with__ method which reverts the given changes after it finished.
myModule__with__port: 3000// within this function port is 3000;// now port is the previous value again
__with__ method is also aware of promises. If a thenable is returned all changes stay until the promise has either been resolved or rejected.
myModule__with__port: 3000return ;then// now port is the previous value again;// port is still 3000 here because the promise hasn't been resolved yet
Difference to require()
Every call of rewire() executes the module again and returns a fresh instance.
rewire"./myModule.js" === rewire"./myModule.js"; // = false
This can especially be a problem if the module is not idempotent like mongoose models.
Be careful, if you do something like this you'll change your global console instance.
myModule__set__"console.log" /* be quiet */ ;
Globals with invalid variable names
rewire imports global variables into the local scope by prepending a list of
var someGlobalVar = globalsomeGlobalVar;
someGlobalVar is not a valid variable name, rewire just ignores it. In this case you're not able to override the global variable locally.
Please be aware that you can't rewire
eval() or the global object itself.
Returns a rewired version of the module found at
rewire() exactly like
Sets the internal variable
name to the given
value. Returns a function which can be called to revert the change.
Takes all enumerable keys of
obj as variable names and sets the values respectively. Returns a function which can be called to revert the change.
Returns the private variable with the given
Returns a function which - when being called - sets
obj, executes the given
callback and reverts
callback returns a promise,
obj is only reverted after the promise has been resolved or rejected. For your convenience the returned function passes the received promise through.
###webpack See rewire-webpack
###browserify If you're using browserify and want to use rewire with browserify please let me know.