Chai.js assertions and convenience functions for testing React Components with enzyme
Chai.js assertions and convenience functions for testing React Components with enzyme
Chai.js assertions for enzyme.
checked()
1. className(str)
1. contain(nodeOrNodes)
1. containMatchingElement(node)
1. descendants(selector)
chai-enzyme depends on:
"peerDependencies": {
"chai": "^3.0.0 || ^4.0.0",
"cheerio": "0.19.x || 0.20.x || 0.22.x || ^1.0.0-0",
"enzyme": "^2.7.0 || ^3.0.0",
"react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0",
"react-dom": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0"
}cheerio is already a dependency of enzyme, so most probably you will not have
to install it manually
$ npm install chai-enzyme --save-devimport chai from 'chai'
import chaiEnzyme from 'chai-enzyme'
chai.use(chaiEnzyme()) // Note the invocation at the endYou can also provide a custom debug function that can print useful information
about the wrapper that you are using.
The default one that chai-enzyme comes with, will pretty print the HTML of the wrapper under test.
1) #text (text) (shallow): passes when the actual matches the expected:
AssertionError: expected to have text 'Test test', but it has 'Test'
---------- this is where the debug output starts ----------
HTML:
Test
---------- this is where the debug output ends ----------Here is how you can implement and configure one for yourself:
function myAwesomeDebug (wrapper) {
let html = wrapper.html()
// do something cool with the html
return html
}
chai.use(chaiEnzyme(myAwesomeDebug))It's important to know that all assertions are registered with Chai's overwrite*
methods and therefore this plugin can work next to other Chai.js plugins that have
similar assertions, such as chai-jquery.
At the beginning of each assertion, we verify if the provided object is a
ShallowWrapper, ReactWrapper or a cheerio object and if not we delegate
to the next assertion that responds to the given method.
Note that not all assertions work with every rendering strategy.
If you are wondering what rendering mechanism to use when, refer to enzyme's documentation.
checked()| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the given wrapper is checked:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper.find('#checked')).to.be.checked()
expect(wrapper.find('#not')).to.not.be.checked()className(str)| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the wrapper has a given class:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper.find('span')).to.have.className('child')
expect(wrapper.find('span')).to.not.have.className('root')contain(nodeOrNodes)| render | mount | shallow |
|---|---|---|
| no | yes | yes |
Assert that the wrapper contains a given node or array of nodes:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
import PropTypes from 'prop-types';
class User extends React.Component {
render () {
return (
User {this.props.index}
)
}
}
User.propTypes = {
index: PropTypes.number.isRequired
}
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper).to.contain()
expect(wrapper).to.contain([, ])
expect(wrapper).to.not.contain()containMatchingElement(node)| render | mount | shallow |
|---|---|---|
| no | yes | yes |
Assert that the wrapper contains a matching given node:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
import PropTypes from 'prop-types';
class User extends React.Component {
render () {
return (
User {this.props.index} {this.props.name}
)
}
}
User.propTypes = {
index: PropTypes.number,
name: PropTypes.string.isRequired
}
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper).to.containMatchingElement()
expect(wrapper).to.not.containMatchingElement()descendants(selector)| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the wrapper contains a descendant matching the given selector:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class User extends React.Component {
render () {
return (
User
)
}
}
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper).to.have.descendants('#root')
expect(wrapper).to.have.descendants(User)
expect(wrapper).to.not.have.descendants('#root1')exactly()| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the wrapper contains an exact amount of descendants matching the given selector:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper).to.have.exactly(2).descendants('.item')disabled()| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the given wrapper is disabled:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper.find('#disabled')).to.be.disabled()
expect(wrapper.find('#not')).to.not.be.disabled()blank()| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the given wrapper is empty:
…present()| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the given wrapper exists:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper.find('#parent')).to.be.present()
expect(wrapper.find('#parent')).to.exist // an alias
class NullFixture extends React.Component {
render () {
return null
}
}
const nullWrapper = mount() // mount/render/shallow when applicable
expect(nullWrapper).to.be.present()
expect(nullWrapper).to.exist // an aliashtml(str)| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the wrapper has given html:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper.find('#child')).to.have.html('Test')
expect(wrapper.find('#child')).to.not.have.html('Test 1')
expect(wrapper.find('#child')).to.have.html().match(/Test/)id(str)| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the wrapper has given ID attribute:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper).to.have.id('root')
expect(wrapper).to.not.have.id('child')match(selector)| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the wrapper matches given selector:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper.find('span')).to.match('#child')
expect(wrapper.find('#root')).to.not.match('#child')ref(key)| render | mount | shallow |
|---|---|---|
| no | yes | no |
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper).to.have.ref('test')
expect(wrapper).to.have.ref('random')selected()| render | mount | shallow |
|---|---|---|
| yes | yes | no |
Assert that the given wrapper is selected:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
Test 1
Test 1
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper.find('#test1')).to.be.selected()
expect(wrapper.find('#test2')).to.not.be.selected()tagName(str)| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the given wrapper has the tag name:
import React from 'react'
import {mount, render, shallow} from 'enzyme'
class Fixture extends React.Component {
render () {
return (
)
}
}
const wrapper = mount() // mount/render/shallow when applicable
expect(wrapper).to.have.tagName('div')
expect(wrapper.find('span')).to.have.tagName('span')
expect(wrapper).to.not.have.tagName('a')
expect(wrapper.find('span')).to.not.have.tagName('a')text(str)| render | mount | shallow |
|---|---|---|
| yes | yes | yes |
Assert that the given wrapper has the supplied text:
…type(func)| render | mount | shallow |
|---|---|---|
| no | yes | yes |
Assert that the given wrapper has a given type:
import React from 'react'
import {shallow} from 'enzyme'
class Foo extends React.Component {
render () {
return (
)
}
}
class Bar extends ReaNo open issues yet, or sync has not completed.