GithubHelp home page GithubHelp logo

andrewraycode / babel-plugin-rewire Goto Github PK

View Code? Open in Web Editor NEW

This project forked from speedskater/babel-plugin-rewire

0.0 3.0 0.0 102 KB

A babel plugin adding the ability to rewire modul dependency. This enables to mock modules for testing purposes.

JavaScript 100.00%

babel-plugin-rewire's Introduction

babel-plugin-rewire

A Babel plugin that adds the ability to rewire module dependencies.

Build Status

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>);
	}
}

Test Code

import ComponentToTest from 'my-fancy-wrapper-component-module';

ComponentToTest.__Rewire__('ChildComponent', React.createClass({
    render: function() { return <div {...this.props}></div>; }
}));
....

ComponentToTest.__ResetDependency__('ChildComponent');

Installation

$ npm install babel babel-plugin-rewire

Usage

$ 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' }

Release History

  • 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

License

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.

babel-plugin-rewire's People

Contributors

jedrichards avatar speedskater avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.