├── .editorconfig ├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── lib └── index.js ├── package.json └── test ├── build.js ├── spec.js └── template ├── dependency.js ├── ignore-me.js └── module.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | indent_style = space 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | 10 | [*.md] 11 | trim_trailing_whitespace = false 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.log 3 | *.dat 4 | 5 | .DS_Store 6 | Thumbs.db 7 | 8 | node_modules 9 | bower_components 10 | 11 | heroku 12 | heroku.pub 13 | 14 | test/bundle.js 15 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.12" 5 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | This software is released under the MIT license: 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ⚠️ **This module is no longer maintained** ⚠️ 2 | 3 | # Rewireify ![Build status](https://api.travis-ci.org/i-like-robots/rewireify.png) 4 | 5 | Rewireify 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: 6 | 7 | - Inject mocks for other modules 8 | - Leak private variables 9 | - Override variables within the module 10 | 11 | Rewireify is compatible with Browserify 3+ 12 | 13 | ## Usage 14 | 15 | First install and save Rewireify into your project's development dependencies: 16 | 17 | ```sh 18 | $ npm install rewireify --save-dev 19 | ``` 20 | 21 | Include the Rewireify transform as part of your test bundle: 22 | 23 | ```sh 24 | $ browserify -e app.js -o test-bundle.js -t rewireify -s test-bundle 25 | ``` 26 | 27 | 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: 28 | 29 | ```sh 30 | $ browserify -e app.js -o test-bundle.js -t [ rewireify --ignore filename.js,**/*-mixin.js ] -s test-bundle 31 | ``` 32 | 33 | Now you can inspect, modify and override your modules internals in your tests. The `__get__` and `__set__` methods are the same as Rewire: 34 | 35 | ```js 36 | var bundle = require("./path/to/test-bundle"); 37 | 38 | // Private variables can be leaked... 39 | subject.__get__("secretKey"); 40 | 41 | // ...or modified 42 | subject.__set__("secretKey", 1234); 43 | 44 | // Nested properties can be inspected or modified 45 | subject.__set__("user.firstname", "Joe"); 46 | 47 | // Dependencies can be mocked... 48 | subject.__set__("config", { 49 | cache: false, 50 | https: false 51 | }); 52 | 53 | // ...or methods stubbed 54 | subject.__set__("http.get", function(url, cb) { 55 | cb("This method has been stubbed"); 56 | }); 57 | 58 | // And everything can be reverted 59 | var revert = subject.__set__("port", 3000); 60 | 61 | revert(); 62 | ``` 63 | 64 | For more details check out the [Rewire documentation](https://github.com/jhnns/rewire/blob/master/README.md#api). 65 | 66 | ## Usage with ES6 67 | 68 | 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](https://github.com/thlorenz/proxyquireify). 69 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var minimatch = require("minimatch"); 2 | var through = require("through"); 3 | var path = require("path"); 4 | 5 | var __get__ = require("rewire/lib/__get__").toString(); 6 | var __set__ = require("rewire/lib/__set__").toString(); 7 | 8 | console.warn("Injecting Rewireify into modules"); 9 | 10 | module.exports = function rewireify(file, options) { 11 | options = { 12 | ignore: options.ignore || "" 13 | }; 14 | 15 | var ignore = ["__get__.js", "__set__.js", "**/*.json"].concat(options.ignore.split(",")); 16 | var relativeFile = file.replace(process.cwd(), ""); 17 | var fileName = path.basename(file); 18 | 19 | var matches = ignore.filter(function(pattern) { 20 | return ignore.indexOf(fileName) > -1 || minimatch(relativeFile, pattern); 21 | }); 22 | 23 | if (matches.length) { 24 | return through(); 25 | } 26 | 27 | var data = ""; 28 | var post = ""; 29 | 30 | function write(buffer) { 31 | data += buffer; 32 | } 33 | 34 | function end() { 35 | post += "/* This code was injected by Rewireify */\n"; 36 | post += "if ((typeof module.exports).match(/object|function/) && \n" 37 | post += "Object.isExtensible(module.exports)) {\n"; 38 | post += "Object.defineProperty(module.exports, '__get__', { value: " + __get__ + ", writable: true });\n"; 39 | post += "Object.defineProperty(module.exports, '__set__', { value: " + __set__ + ", writable: true });\n"; 40 | post += "}\n"; 41 | 42 | this.queue(data); 43 | this.queue(post); 44 | this.queue(null); 45 | } 46 | 47 | return through(write, end); 48 | }; 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rewireify", 3 | "version": "0.2.5", 4 | "description": "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.", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "node test/build.js && node test/spec.js" 8 | }, 9 | "keywords": [ 10 | "browserify-transform", 11 | "browserify", 12 | "transform", 13 | "testing", 14 | "inspect", 15 | "debug", 16 | "mock", 17 | "stub", 18 | "test" 19 | ], 20 | "dependencies": { 21 | "minimatch": "^2.0.7", 22 | "rewire": "^2.3.3", 23 | "through": "^2.3.7" 24 | }, 25 | "devDependencies": { 26 | "browserify": "10.x", 27 | "sinon": "^1.14.0", 28 | "vows": "^0.8.0" 29 | }, 30 | "repository": { 31 | "type": "git", 32 | "url": "git@github.com:i-like-robots/rewireify.git" 33 | }, 34 | "homepage": "https://github.com/i-like-robots/rewireify", 35 | "author": "Matt Hinchliffe", 36 | "license": "MIT" 37 | } 38 | -------------------------------------------------------------------------------- /test/build.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var path = require("path"); 3 | var browserify = require("browserify"); 4 | var rewireify = require("../lib/index"); 5 | 6 | console.log("Building test bundle..."); 7 | 8 | var browserifyOptions = { 9 | basedir: __dirname, 10 | standalone: "test-bundle" 11 | }; 12 | 13 | var rewireifyOptions = { 14 | ignore: "**/*-me.js" 15 | }; 16 | 17 | browserify("./template/module.js", browserifyOptions) 18 | .transform(rewireify, rewireifyOptions) 19 | .bundle(function(err, output) { 20 | if (err) { 21 | console.error(err); 22 | } else { 23 | fs.writeFileSync(path.join(__dirname, "bundle.js"), output); 24 | } 25 | }); 26 | -------------------------------------------------------------------------------- /test/spec.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var vows = require("vows"); 3 | var sinon = require("sinon"); 4 | var assert = require("assert"); 5 | var fixture = require("./bundle"); 6 | 7 | function reset(target) { 8 | fixture.__get__(target).restore && fixture.__get__(target).restore(); 9 | } 10 | 11 | vows.describe("Injecting methods").addBatch({ 12 | 13 | "Methods are injected into bundle": { 14 | topic: function() { 15 | fs.readFile(require.resolve("./bundle.js"), { encoding: "utf8" }, this.callback) 16 | }, 17 | "to leak variables": function(err, contents) { 18 | assert.isNull(err); 19 | assert.include(contents, "Object.defineProperty(module.exports, '__get__'"); 20 | }, 21 | "to modify variables": function(err, contents) { 22 | assert.isNull(err); 23 | assert.include(contents, "Object.defineProperty(module.exports, '__set__'"); 24 | } 25 | }, 26 | "Files can be ignored": { 27 | topic: function() { 28 | return fixture.exposeIgnoredDependency(); 29 | }, 30 | "so the getter and setter are not appended": function(topic) { 31 | assert.equal(topic.__get__, undefined); 32 | assert.equal(topic.__set__, undefined); 33 | } 34 | } 35 | 36 | }).run(); 37 | 38 | vows.describe("Getters and setters").addBatch({ 39 | "are not exposed": { 40 | topic: function() { 41 | return fixture; 42 | }, 43 | "publicly": function(topic) { 44 | var keys = Object.keys(topic); 45 | assert.notInclude(keys, '__get__'); 46 | assert.notInclude(keys, '__set__'); 47 | } 48 | }, 49 | 50 | "Private variables": { 51 | "can be inspected": { 52 | topic: function() { 53 | return fixture.__get__("inspectPrivate"); 54 | }, 55 | "with the getter": function(topic) { 56 | assert.equal(topic, "I am private"); 57 | } 58 | }, 59 | "can be modified": { 60 | "individually": { 61 | topic: function() { 62 | fixture.__set__("modifyIndividual", "I have been changed"); 63 | return fixture.__get__("modifyIndividual"); 64 | }, 65 | "with the setter": function(topic) { 66 | assert.equal(topic, "I have been changed"); 67 | } 68 | }, 69 | "within objects": { 70 | topic: function() { 71 | fixture.__set__("modifyWithin.key", "I have been changed"); 72 | return fixture.__get__("modifyWithin.key"); 73 | }, 74 | "using dot notation": function(topic) { 75 | assert.equal(topic, "I have been changed"); 76 | } 77 | }, 78 | "en masse": { 79 | topic: function() { 80 | fixture.__set__({ 81 | modifyEnMasseA: "I have been changed", 82 | modifyEnMasseB: "I have been changed, too" 83 | }); 84 | 85 | return [ 86 | fixture.__get__("modifyEnMasseA"), 87 | fixture.__get__("modifyEnMasseB") 88 | ]; 89 | }, 90 | "by passing an object": function(topic) { 91 | assert.equal(topic[0], "I have been changed"); 92 | assert.equal(topic[1], "I have been changed, too"); 93 | } 94 | } 95 | }, 96 | "can be restored": { 97 | topic: function() { 98 | return fixture.__set__("modifyIndividual", "I have been changed _again_"); 99 | }, 100 | "by a function returned from __set__": function(topic) { 101 | assert.isFunction(topic); 102 | }, 103 | "that when called": { 104 | topic: function(revert) { 105 | revert(); 106 | return fixture.__get__("modifyIndividual"); 107 | }, 108 | "will revert to the original value": function(topic) { 109 | assert.equal(topic, "I will be changed"); 110 | } 111 | } 112 | } 113 | }, 114 | 115 | "Dependencies": { 116 | "can be inspected": { 117 | "with a spy": { 118 | topic: function() { 119 | reset("privateDependency.exampleMethod"); 120 | sinon.spy(fixture.__get__("privateDependency"), "exampleMethod")(); 121 | return fixture.__get__("privateDependency.exampleMethod"); 122 | }, 123 | "using sinon.spy": function(topic) { 124 | assert.equal(topic.calledOnce, true); 125 | } 126 | } 127 | }, 128 | "can be modified": { 129 | "with a stub": { 130 | topic: function() { 131 | reset("privateDependency.exampleMethod"); 132 | 133 | sinon.stub(fixture.__get__("privateDependency"), "exampleMethod", function() { 134 | return "I am a stub"; 135 | }); 136 | 137 | return fixture.methodUsingDependency(); 138 | }, 139 | "using sinon.stub": function(topic) { 140 | assert.equal(topic, "I am a stub"); 141 | } 142 | }, 143 | "with a double": { 144 | topic: function() { 145 | reset("privateDependency.exampleMethod"); 146 | 147 | fixture.__set__("privateDependency", { 148 | exampleMethod: function() { 149 | return "I am a double"; 150 | } 151 | }); 152 | 153 | return fixture.methodUsingDependency(); 154 | }, 155 | "with a test double": function(topic) { 156 | assert.equal(topic, "I am a double"); 157 | } 158 | } 159 | } 160 | } 161 | 162 | }).run(); 163 | -------------------------------------------------------------------------------- /test/template/dependency.js: -------------------------------------------------------------------------------- 1 | exports.exampleMethod = function() { 2 | return "I am an example"; 3 | }; 4 | -------------------------------------------------------------------------------- /test/template/ignore-me.js: -------------------------------------------------------------------------------- 1 | exports = {}; 2 | -------------------------------------------------------------------------------- /test/template/module.js: -------------------------------------------------------------------------------- 1 | var inspectPrivate = "I am private" 2 | 3 | var modifyIndividual = "I will be changed"; 4 | 5 | var modifyWithin = { 6 | key: "I will be changed" 7 | }; 8 | 9 | var modifyEnMasseA = modifyEnMasseB = "I will be changed"; 10 | 11 | var privateDependency = require("./dependency"); 12 | 13 | exports.methodUsingDependency = function() { 14 | return privateDependency.exampleMethod(); 15 | }; 16 | 17 | exports.exposeIgnoredDependency = function() { 18 | return require("./ignore-me"); 19 | }; 20 | --------------------------------------------------------------------------------