12 - "wrap.find().simulate('click')"
13 - "wrap.contains(<div/>)"
15 [Enzyme](http://airbnb.io/enzyme) lets you write unit tests for React components. This guide covers Enzyme 3.x.
24 import {shallow, mount} from 'enzyme'
29 wrap = shallow(<MyComponent />)
33 wrap = mount(<MyComponent />)
36 Shallow wrapping doesn't descend down to sub-components.
37 A full mount also mounts sub-components.
39 See: [Shallow rendering](http://airbnb.io/enzyme/docs/api/shallow.html),
40 [Full rendering](http://airbnb.io/enzyme/docs/api/mount.html)
45 console.log(wrap.debug())
48 Shows HTML for debugging purposes.
50 See: [debug()](http://airbnb.io/enzyme/docs/api/ReactWrapper/debug.html)
59 import { shallow } from 'enzyme'
60 import MyComponent from '../MyComponent'
67 <MyComponent name='Groot' />
70 expect(wrap.text()).toEqual('I am Groot')
79 wrap.setProps({ name: 'Moe' })
80 wrap.setState({ show: true })
86 expect(wrap.prop('name')).toEqual('Moe')
87 expect(wrap.state('show')).toEqual(true)
91 expect('name' in wrap.props()).toEqual('Moe')
92 expect('show' in wrap.state()).toEqual(true)
99 wrap.containsMatchingElement(
100 <span>I am groot</span>
105 `containsMatchingElement()` is probably the most useful assertion in Jest.
110 expect(wrap).toMatchSnapshot()
113 Be sure you've set up enzyme-to-json for snapshots (see [Installing](#installing) below).
119 wrap.find('button').text()
123 Use `.find()` to traverse down to nodes. It will return wrapper objects, too.
125 ### Simulating events
128 wrap.find('input').simulate('click')
131 #### With event object props
134 wrap.find('input').simulate('change', {
135 target: { value: 'hello' }
144 npm install --save-dev enzyme \
145 enzyme-adapter-react-16 \
153 import Enzyme from 'enzyme'
154 import Adapter from 'enzyme-adapter-react-16'
156 Enzyme.configure({ adapter: new Adapter() })
169 This configures Enzyme for React v16, and Jest to automatically configure Enzyme for you. There are other adapters in Enzyme's installation instructions.
171 See: [Installation](http://airbnb.io/enzyme/#installation)
176 npm install --save-dev enzyme-to-json
184 "snapshotSerializers": [
185 "enzyme-to-json/serializer"
194 wrap = mount(<MyComponent />)
195 expect(wrap).toMatchSnapshot()
199 Optional, but recommended: This allows you to use Enzyme wrappers with Jest snapshots.
201 See: [enzyme-to-json](https://www.npmjs.com/package/enzyme-to-json)
208 wrap.find('button') // → ReactWrapper
209 wrap.filter('button') // → ReactWrapper
210 wrap.not('span') // → ReactWrapper (inverse of filter())
211 wrap.children() // → ReactWrapper
212 wrap.parent() // → ReactWrapper
213 wrap.closest('div') // → ReactWrapper
214 wrap.childAt(0) // → ReactWrapper
215 wrap.at(0) // → ReactWrapper
216 wrap.first() // → ReactWrapper
217 wrap.last() // → ReactWrapper
221 wrap.get(0) // → ReactElement
222 wrap.getElement() // → ReactElement
223 wrap.getElements() // → Array<ReactElement>
224 wrap.getDOMNode() // → DOMComponent
227 See: [Full rendering API](http://airbnb.io/enzyme/docs/api/mount.html)
232 wrap.simulate('click')
238 wrap.setState({ ··· })
239 wrap.setProps({ ··· })
240 wrap.setContext({ ··· })
244 wrap.state() // get full state
245 wrap.props() // get full props
246 wrap.context() // get full context
250 wrap.state('key') // → any
251 wrap.prop('key') // → any
252 wrap.context('key') // → any
256 wrap.instance() // → ReactComponent
264 wrap.update() // calls forceUpdate()
270 wrap.debug() // → string
271 wrap.html() // → string
272 wrap.text() // → string
273 wrap.type() // → string | function
274 wrap.name() // → string
275 wrap.is('.classname') // → boolean
276 wrap.hasClass('class') // → boolean
277 wrap.exists() // → boolean
278 wrap.contains(<div />) // → boolean
279 wrap.contains([ <div /> ]) // → boolean
280 wrap.some('.child') // → boolean
282 wrap.someWhere(n => n.hasClass('foo'))
284 wrap.containsMatchingElement(<div />) // → boolean
285 wrap.containsAllMatchingElements([ <div /> ]) // → boolean
286 wrap.containsAnyMatchingElements([ <div /> ]) // → boolean
291 - [Enzyme website](https://airbnb.io/enzyme) _(airbnb.io)_
292 - [Enzyme v2 cheatsheet](./enzyme@2) _(devhints.io)_ (old version)