- Notifications
You must be signed in to change notification settings - Fork0
Easy monkey-patching for CodeRoad
License
ShMcK/rewire-coderoad
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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
process
- leak private variables
- override variables within the module.
rewire doesnot load the file and eval the contents to emulate node's require mechanism. In fact it uses node's ownrequire to load the module. Thus your module behaves exactly the same in your test environment as under regularcircumstances (except your modifications).
Good news to all caffeine-addicts: rewire works also withCoffee-Script. Note that in thiscase CoffeeScript needs to be listed in your devDependencies.
If you want to use rewire also on the client-side take a look atclient-side bundlers
npm install rewire
Imagine you want to test this module:
// lib/myModule.js// With rewire you can change all these variablesvarfs=require("fs"),path="/somewhere/on/the/disk";functionreadSomethingFromFileSystem(cb){console.log("Reading from file system ...");fs.readFile(path,"utf8",cb);}exports.readSomethingFromFileSystem=readSomethingFromFileSystem;
Now within your test module:
// test/myModule.test.jsvarrewire=require("rewire");varmyModule=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.
varfsMock={readFile:function(path,encoding,cb){expect(path).to.equal("/somewhere/on/the/disk");cb(null,"Success!");}};myModule.__set__("fs",fsMock);myModule.readSomethingFromFileSystem(function(err,data){console.log(data);// = Success!});
You can also set different variables with one call.
myModule.__set__({fs:fsMock,path:"/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:{log:function(){/* be quiet */}},process:{argv:["testArg1","testArg2"]}});
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 idempotentlike mongoose models.
Changing globals
Be careful, if you do something like this you'll change your global console instance.
myModule.__set__("console.log",function(){/* be quiet */});
##API
###rewire(filename): rewiredModule
- filename:
Path to the module that shall be rewired. Use it exactly like require().
###rewiredModule.__set__(name, value)
- name:
Name of the variable to set. The variable should be global or defined withvar
in the top-leve scope of the module. - value:
The value to set.
###rewiredModule.__set__(env)
- env:
Takes all keys as variable names and sets the values respectively.
###rewiredModule.__get__(name): value
Returns the private variable.
##Client-Side Bundlers
###webpackSeerewire-webpack
###browserifyIf you're using browserify and want to use rewire with browserifyplease let me know.
##License
MIT
About
Easy monkey-patching for CodeRoad
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- JavaScript99.9%
- CoffeeScript0.1%