Rewireify is a port of Rewire for Browserify that adds setter and getter methods to each module so that their behaviour can be modified for better unit testing.
Rewireify is compatible with Browserify 3+
First install and save Rewireify into your project's development dependencies:
$ npm install rewireify --save-dev
Include the Rewireify transform as part of your test bundle:
$ browserify -e app.js -o test-bundle.js -t rewireify -s test-bundle
Rewireify can also ignore certain files with the
--ignore option and a filename or glob expression. Multiple files or patterns can be excluded by separating them with commas:
$ browserify -e app.js -o test-bundle.js -t [ rewireify --ignore filename.js,**/*-mixin.js ] -s test-bundle
Now you can inspect, modify and override your modules internals in your tests. The
__set__ methods are the same as Rewire:
var bundle = require"./path/to/test-bundle";// Private variables can be leaked...subject__get__"secretKey";// ...or modifiedsubject__set__"secretKey" 1234;// Nested properties can be inspected or modifiedsubject__set__"user.firstname" "Joe";// Dependencies can be mocked...subject__set__"config"cache: falsehttps: false;// ...or methods stubbedsubject__set__"http.get"cb"This method has been stubbed";;// And everything can be revertedvar revert = subject__set__"port" 3000;revert;
For more details check out the Rewire documentation.