You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 26, 2018. It is now read-only.
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 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. With Rewireify you can:
Inject mocks for other modules
Leak private variables
Override variables within the module
Rewireify is compatible with Browserify 3+
Usage
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:
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:
Now you can inspect, modify and override your modules internals in your tests. The __get__ and __set__ methods are the same as Rewire:
varbundle=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: false,https: false});// ...or methods stubbedsubject.__set__("http.get",function(url,cb){cb("This method has been stubbed");});// And everything can be revertedvarrevert=subject.__set__("port",3000);revert();
Rewireify will continue to work in ES6 environments but variables declared as constants (using const) may pose some issues. Constants cannot be reassigned and is now common practice to declare dependencies as such and therefore Rewireify will be unable to to replace them. However, variables declared as constants are not immutable so their individual methods and properties can still accessed and modified. If you must replace an entire dependency in your test environment and you have switched to using const then I recommended checking out Proxyquireify.
About
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.