UNPKG

1.88 kBMarkdownView Raw
1# Rewireify ![Build status](https://api.travis-ci.org/i-like-robots/rewireify.png)
2
3Rewireify is a port of [Rewire](https://github.com/jhnns/rewire) for [Browserify](http://browserify.org/) that adds setter and getter methods to each module so that their behaviour can be modified for better unit testing. With Rewireify you can:
4
5- Inject mocks for other modules
6- Leak private variables
7- Override variables within the module
8
9Rewireify is compatible with Browserify 3+
10
11## Usage
12
13First install and save Rewireify into your project's development dependencies:
14
15```sh
16$ npm install rewireify --save-dev
17```
18
19Include the Rewireify transform as part of your test bundle:
20
21```sh
22$ browserify -e app.js -o test-bundle.js -t rewireify -s test-bundle
23```
24
25Rewireify 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:
26
27```sh
28$ browserify -e app.js -o test-bundle.js -t [ rewireify --ignore filename.js,**/*-mixin.js ] -s test-bundle
29```
30
31Now you can inspect, modify and override your modules internals in your tests. The `__get__` and `__set__` methods are the same as Rewire:
32
33```js
34var bundle = require("./path/to/test-bundle");
35
36// Private variables can be leaked...
37subject.__get__("secretKey");
38
39// ...or modified
40subject.__set__("secretKey", 1234);
41
42// Nested properties can be inspected or modified
43subject.__set__("user.firstname", "Joe");
44
45// Dependencies can be mocked...
46subject.__set__("config", {
47 cache: false,
48 https: false
49});
50
51// ...or methods stubbed
52subject.__set__("http.get", function(url, cb) {
53 cb("This method has been stubbed");
54});
55
56// And everything can be reverted
57var revert = subject.__set__("port", 3000);
58
59revert();
60```
61
62For more details check out the [Rewire documentation](https://github.com/jhnns/rewire/blob/master/README.md#api).