A Babel plugin that adds the ability to rewire module dependencies.
It is inspired by rewire.js and transfers its concepts to es6 using babel.
It is useful for writing tests, specifically to mock the dependencies of the module under test.
Therefore for each module it adds and exports the methods __GetDependency__, __Rewire__, and __ResetDependency__. These methods allow to rewire the module under test. Furthermore in case of a default export these methods are assigned to the existing default export. For compatibility reasons with rewire.js, the methods __get__ and __set__ are assigned to the default export as well.
##Example ###React Component
import ChildComponent from 'child-component-module';
export default class MyFancyWrapperComponent extends React.Component {
render() {
return (<div className="wrapper-style">
<ChildComponent {...this.props} />
</div>);
}
}
import ComponentToTest from 'my-fancy-wrapper-component-module';
ComponentToTest.__Rewire__('ChildComponent', React.createClass({
render: function() { return <div {...this.props}></div>; }
}));
....
ComponentToTest.__ResetDependency__('ChildComponent');
$ npm install babel babel-plugin-rewire
$ babel --plugins rewire
or:
require("babel").transform("code", { plugins: ["rewire"] });
with webpack
use the following loader:
{test: /src\/js\/.+\.js$/, loader: 'babel-loader?plugins=rewire' }
- 0.1.0 Initial release
- 0.1.1 Bugfix: moved to peer dependencies
- 0.1.2 Added __set__ and __get__ to provide compatibility with rewire.js
- 0.1.3 Added handling for the export of named declarations like classes or functions
The ISC License (ISC)
Copyright (c) 2015, Robert Binna [email protected]
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.