|
| 1 | +# Testing React components |
| 2 | + |
| 3 | +## Setting up Babel |
| 4 | + |
| 5 | +The first thing you need to do is to set up `babel` to transpile JSX code from the tests by adding an AVA section to your `package.json`: |
| 6 | + |
| 7 | +```json |
| 8 | +{ |
| 9 | + "ava": { |
| 10 | + "require": ["babel-register"] |
| 11 | + }, |
| 12 | + "babel": { |
| 13 | + "presets": ["react"] |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +You can find more information about setting up `babel` with AVA in the [babelrc recipe](https://github.com/sindresorhus/ava/blob/master/docs/recipes/babelrc.md). |
| 19 | + |
| 20 | +## Using [Enzyme](https://github.com/airbnb/enzyme) |
| 21 | + |
| 22 | +Let's first see how to use AVA with one of the most popular React testing libraries: [Enzyme](https://github.com/airbnb/enzyme). |
| 23 | + |
| 24 | +If you only plan to use [shallow component rendering](https://facebook.github.io/react/docs/test-utils.html#shallow-rendering), you don't need any extra setup. |
| 25 | + |
| 26 | +First install [Enzyme required packages](https://github.com/airbnb/enzyme/#installation): |
| 27 | + |
| 28 | +```console |
| 29 | +$ npm install --save-dev enzyme react-addons-test-utils react-dom |
| 30 | +``` |
| 31 | + |
| 32 | +And you can use Enzyme straight away: |
| 33 | + |
| 34 | +```js |
| 35 | +import test from 'ava'; |
| 36 | +import React from 'react'; |
| 37 | +import {shallow} from 'enzyme'; |
| 38 | + |
| 39 | +const Foo = ({children}) => |
| 40 | + <div className="Foo"> |
| 41 | + <span className="bar">bar</span> |
| 42 | + {children} |
| 43 | + <span className="bar">bar</span> |
| 44 | + </div>; |
| 45 | + |
| 46 | +Foo.propTypes = { |
| 47 | + children: React.PropTypes.any |
| 48 | +}; |
| 49 | + |
| 50 | +test('has a .Foo class name', t => { |
| 51 | + const wrapper = shallow(<Foo/>); |
| 52 | + t.true(wrapper.hasClass('Foo')); |
| 53 | +}); |
| 54 | + |
| 55 | +test('renders two `.Bar`', t => { |
| 56 | + const wrapper = shallow(<Foo/>); |
| 57 | + t.is(wrapper.find('.bar').length, 2); |
| 58 | +}); |
| 59 | + |
| 60 | +test('renders children when passed in', t => { |
| 61 | + const wrapper = shallow( |
| 62 | + <Foo> |
| 63 | + <div className="unique"/> |
| 64 | + </Foo> |
| 65 | + ); |
| 66 | + t.true(wrapper.contains(<div className="unique"/>)); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +Enzyme also has a `mount` and `render` helper to test in an actual browser environment. If you want to use these helpers, you will have to setup a browser environment. Check out the [browser testing recipe](https://github.com/sindresorhus/ava/blob/master/docs/recipes/browser-testing.md) on how to do so. |
| 71 | + |
| 72 | +To see an example of AVA working together with Enzyme, set up for browser testing, have a look at [this sample project](https://github.com/adriantoine/ava-enzyme-demo). |
| 73 | + |
| 74 | +This is a basic example about how to integrate Enzyme with AVA. For more information about using Enzyme for unit testing React component, have a look at [Enzyme's documentation](http://airbnb.io/enzyme/). |
| 75 | + |
| 76 | +## Using JSX helpers |
| 77 | + |
| 78 | +Another approach to testing React component is to use the [`react-element-to-jsx-string`](https://github.com/algolia/react-element-to-jsx-string) package to compare DOM trees as strings. [`jsx-test-helpers`](https://github.com/MoOx/jsx-test-helpers) is a nice library handling [shallow component rendering](https://facebook.github.io/react/docs/test-utils.html#shallow-rendering) and converting JSX to string in order to test React components using AVA assertions. |
| 79 | + |
| 80 | +```console |
| 81 | +$ npm install --save-dev jsx-test-helpers |
| 82 | +``` |
| 83 | + |
| 84 | +Usage example: |
| 85 | + |
| 86 | +```js |
| 87 | +import test from 'ava'; |
| 88 | +import React from 'react'; |
| 89 | +import {renderJSX, JSX} from 'jsx-test-helpers'; |
| 90 | + |
| 91 | +const Foo = ({children}) => |
| 92 | + <div className="Foo"> |
| 93 | + <span className="bar">bar</span> |
| 94 | + {children} |
| 95 | + <span className="bar">bar</span> |
| 96 | + </div>; |
| 97 | + |
| 98 | +Foo.propTypes = { |
| 99 | + children: React.PropTypes.any |
| 100 | +}; |
| 101 | + |
| 102 | +test('renders correct markup', t => { |
| 103 | + const actual = renderJSX(<Foo/>); |
| 104 | + const expected = JSX( |
| 105 | + <div className="Foo"> |
| 106 | + <span className="bar">bar</span> |
| 107 | + <span className="bar">bar</span> |
| 108 | + </div> |
| 109 | + ); |
| 110 | + t.is(actual, expected); |
| 111 | +}); |
| 112 | + |
| 113 | +test('renders children when passed in', t => { |
| 114 | + const actual = renderJSX( |
| 115 | + <Foo> |
| 116 | + <div className="unique"/> |
| 117 | + </Foo> |
| 118 | + ); |
| 119 | + const expected = JSX( |
| 120 | + <div className="Foo"> |
| 121 | + <span className="bar">bar</span> |
| 122 | + <div className="unique"/> |
| 123 | + <span className="bar">bar</span> |
| 124 | + </div> |
| 125 | + ); |
| 126 | + t.is(actual, expected); |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +Note that you have to use variables like `actual` and `expected` because [`power-assert` doesn't handle JSX correctly](https://github.com/power-assert-js/power-assert/issues/34). |
| 131 | + |
| 132 | +This is a basic example about how to use `jsx-test-helpers` with AVA. To see a more advanced usage of this library, have a look at [this annotated test file](https://github.com/MoOx/jsx-test-helpers/blob/master/src/__tests__/index.js). |
| 133 | + |
| 134 | +[This sample project](https://github.com/MoOx/jsx-test-helpers) shows a basic and minimal setup of AVA with `jsx-test-helpers`. |
| 135 | + |
| 136 | +## Using other assertion libraries |
| 137 | + |
| 138 | +In AVA, you can use any assertion library and there is already a few out there allowing to test React components. Here is a list of assertion libraries working well with AVA: |
| 139 | +- [`expect-jsx`](https://github.com/algolia/expect-jsx) ([Example](https://github.com/sindresorhus/ava/issues/186#issuecomment-161317068)) |
| 140 | +- [`unexpected-react`](https://github.com/bruderstein/unexpected-react) ([Sample project with an output example](https://github.com/adriantoine/ava-unexpected-react-demo)) |
| 141 | + |
| 142 | +## Reference |
| 143 | +- [In depth guide of setting up AVA with code coverage on a React project](https://github.com/kentcdodds/react-ava-workshop) |
0 commit comments