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.
- Inject mocks for other modules
- Leak private variables
- Override variables within the module
First install and save Rewireify into your project dependencies:
$ npm install rewireify --save-dev
Include the Rewireify transform as part of your Browserify test build:
$ 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. Multiple files can be excluded by separating them with commas:
$ browserify -e app.js -o test-bundle.js -t [ rewireify --ignore filename,second-filename ] -s test-bundle
Now you can inspect, modify and override your modules internals in your tests:
var bundle = require"./path/to/test-bundle";// Private variables can be leaked...subject__get__"secretKey";// ...or modifiedsubject__set__"secretKey" 1234;// Multiple, nested properties can be changedsubject__set__"user.firstname": "Joe""user.lastname": "Bloggs";// Dependencies can be mocked...subject__set__"config"cache: falsehttps: false;// ...or methods stubbedsubject__set__"http.get"cb"This method has been stubbed";;
Name of the variable to get. The variable should be defined with var in the top-level scope of the module.
Name of the variable to set. The variable should be defined with var in the top-level scope of the module.
The value to set.
Takes all keys as variable names and sets their values respectively.