chai-dom、enzyme、jest-dom、react-testing-library はいずれもJavaScript/TypeScriptエコシステムで広く使われるテスト関連パッケージですが、それぞれ役割が異なります。chai-dom はChaiアサーションライブラリ向けのDOM拡張です。enzyme はReactコンポーネントをマウント・操作するためのユーティリティで、かつては主流でしたが現在は非推奨です。jest-dom はJestのexpectマッチャーをDOM要素向けに拡張します。react-testing-library(RTL)はユーザーの視点に立ったコンポーネントテストを促進するライブラリで、現在の業界標準となっています。これらのツールは単体で使うこともありますが、しばしば組み合わせて使われます(例:RTL + jest-dom)。
Reactアプリケーションのテスト戦略はここ数年で大きく変化しました。かつてはコンポーネントの内部状態やpropsを直接操作するアプローチが一般的でしたが、現在は「ユーザーが実際にどう感じるか」を重視するテスト手法が主流です。ここでは、4つの代表的なテスト関連パッケージ — chai-dom、enzyme、jest-dom、react-testing-library — を、実際の開発現場の観点から比較します。
まず重要な前提として、enzyme は公式に非推奨(deprecated)とされています。AirbnbのGitHubリポジトリおよびnpmページには明確にその旨が記載されており、React 18以降のバージョンとの互換性はありません。新規プロジェクトで選ぶべきではありません。一方、他の3つは現役で積極的にメンテナンスされています。
enzyme のアプローチ(非推奨)enzyme はコンポーネントのインスタンスを直接操作し、内部のstateやpropsを検証するスタイルを推奨していました。これは一見便利に見えますが、実装が変わるとテストも壊れやすくなります。
// enzyme: 内部状態に依存したテスト(非推奨)
import { shallow } from 'enzyme';
const wrapper = shallow(<MyButton />);
expect(wrapper.state('clicked')).toBe(false);
wrapper.find('button').simulate('click');
expect(wrapper.state('clicked')).toBe(true);
react-testing-library のアプローチ(推奨)react-testing-library(RTL)は、実際のDOMとユーザーの操作に基づいてテストします。内部のstateやpropsにはアクセスせず、代わりに画面に表示されている内容やユーザーの行動(クリック、入力など)を検証します。
// react-testing-library: ユーザー視点のテスト
import { render, screen, fireEvent } from '@testing-library/react';
render(<MyButton />);
expect(screen.queryByText('Clicked!')).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button'));
expect(screen.getByText('Clicked!')).toBeInTheDocument();
この違いは重要です。RTLを使うことで、コンポーネントの再実装(例:クラス → フック)を行ってもテストが壊れにくくなります。
chai-dom のアサーションchai-dom はChaiのプラグインとして、DOM要素向けのチェーンメソッドを提供します。
// chai-dom: ChaiスタイルのDOMアサーション
import chai, { expect } from 'chai';
import chaiDom from 'chai-dom';
chai.use(chaiDom);
document.body.innerHTML = '<div>Hello</div>';
const el = document.querySelector('div');
expect(el).to.exist;
expect(el).to.have.text('Hello');
expect(el).to.be.visible;
jest-dom のアサーションjest-dom はJestのexpectにDOM向けマッチャーを追加します。react-testing-library と組み合わせて使うのが一般的です。
// jest-dom: JestスタイルのDOMアサーション
import '@testing-library/jest-dom';
document.body.innerHTML = '<div>Hello</div>';
const el = document.querySelector('div');
expect(el).toBeInTheDocument();
expect(el).toHaveTextContent('Hello');
expect(el).toBeVisible();
両者の機能は似ていますが、jest-dom の方がより多くのマッチャー(例:toHaveAttribute、toHaveClass)を提供しており、Jestエコシステムとの統合もスムーズです。
同じボタンコンポーネントを、各ライブラリでどのようにテストするかを見てみましょう。
// Button.jsx
import React, { useState } from 'react';
export default function Button() {
const [clicked, setClicked] = useState(false);
return (
<button onClick={() => setClicked(true)}>
{clicked ? 'Clicked!' : 'Click me'}
</button>
);
}
enzyme でのテスト(非推奨)import { shallow } from 'enzyme';
import Button from './Button';
describe('Button', () => {
it('初期状態で
enzyme は公式ドキュメントおよびnpmページで明確に非推奨(deprecated)とされており、新規プロジェクトでの使用は避けるべきです。React 16.3以降のライフサイクル変更やReact 18の並行モードとの互換性問題があり、メンテナンスも停止されています。既存のEnzymeベースのテストがある場合は、react-testing-library への移行を計画してください。
jest-dom は、Jestと組み合わせてDOM要素に対する直感的なアサーション(例:toBeInTheDocument()、toHaveTextContent())を提供したい場合に最適です。特に react-testing-library と併用することで、テストコードの可読性と信頼性が大幅に向上します。Jestを使用しているプロジェクトでは、ほぼ必須の拡張として導入すべきです。
react-testing-library は、ユーザーの視点に即したテストを書きたい場合に選ぶべき標準ライブラリです。実際のDOM構造やユーザーインタラクションに基づいてコンポーネントをテストできるため、実装詳細に依存しない堅牢なテストが可能です。React公式ドキュメントでも推奨されており、新規プロジェクトではまずこのライブラリを採用すべきです。
chai-dom は、MochaやChaiといったBDDスタイルのテストフレームワークを使っているプロジェクトでDOMアサーションが必要な場合に選択します。ただし、現代のReactテストではJest + RTLの組み合わせが主流であるため、新規プロジェクトでChaiスタックを採用しない限り、このパッケージを選ぶ機会は少ないでしょう。既存のChaiベースのテストスイートを維持する必要がある場合にのみ検討してください。
Enzyme is a JavaScript Testing utility for React that makes it easier to test your React Components' output. You can also manipulate, traverse, and in some ways simulate runtime given the output.
Enzyme's API is meant to be intuitive and flexible by mimicking jQuery's API for DOM manipulation and traversal.
Are you here to check whether or not Enzyme is compatible with React 16? Are you currently using Enzyme 2.x? Great! Check out our migration guide for help moving on to Enzyme v3 where React 16 is supported.
To get started with enzyme, you can simply install it via npm. You will need to install enzyme along with an Adapter corresponding to the version of react (or other UI Component library) you are using. For instance, if you are using enzyme with React 16, you can run:
npm i --save-dev enzyme enzyme-adapter-react-16
Each adapter may have additional peer dependencies which you will need to install as well. For instance,
enzyme-adapter-react-16 has peer dependencies on react and react-dom.
At the moment, Enzyme has adapters that provide compatibility with React 16.x, React 15.x,
React 0.14.x and React 0.13.x.
The following adapters are officially provided by enzyme, and have the following compatibility with React:
| Enzyme Adapter Package | React semver compatibility |
|---|---|
enzyme-adapter-react-16 | ^16.4.0-0 |
enzyme-adapter-react-16.3 | ~16.3.0-0 |
enzyme-adapter-react-16.2 | ~16.2 |
enzyme-adapter-react-16.1 | ~16.0.0-0 || ~16.1 |
enzyme-adapter-react-15 | ^15.5.0 |
enzyme-adapter-react-15.4 | 15.0.0-0 - 15.4.x |
enzyme-adapter-react-14 | ^0.14.0 |
enzyme-adapter-react-13 | ^0.13.0 |
Finally, you need to configure enzyme to use the adapter you want it to use. To do this, you can use
the top level configure(...) API.
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
It is possible for the community to create additional (non-official) adapters that will make enzyme work with other libraries. If you have made one and it's not included in the list below, feel free to make a PR to this README and add a link to it! The known 3rd party adapters are:
| Adapter Package | For Library | Status |
|---|---|---|
enzyme-adapter-preact-pure | preact | (stable) |
enzyme-adapter-inferno | inferno | (work in progress) |
Enzyme is unopinionated regarding which test runner or assertion library you use, and should be compatible with all major test runners and assertion libraries out there. The documentation and examples for enzyme use mocha and chai, but you should be able to extrapolate to your framework of choice.
If you are interested in using enzyme with custom assertions and convenience functions for testing your React components, you can consider using:
chai-enzyme with Mocha/Chai.jasmine-enzyme with Jasmine.jest-enzyme with Jest.should-enzyme for should.js.expect-enzyme for expect.Using Enzyme with React Native
Using Enzyme with Tape and AVA
import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import sinon from 'sinon';
import MyComponent from './MyComponent';
import Foo from './Foo';
describe('<MyComponent />', () => {
it('renders three <Foo /> components', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper.find(Foo)).to.have.lengthOf(3);
});
it('renders an `.icon-star`', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper.find('.icon-star')).to.have.lengthOf(1);
});
it('renders children when passed in', () => {
const wrapper = shallow((
<MyComponent>
<div className="unique" />
</MyComponent>
));
expect(wrapper.contains(<div className="unique" />)).to.equal(true);
});
it('simulates click events', () => {
const onButtonClick = sinon.spy();
const wrapper = shallow(<Foo onButtonClick={onButtonClick} />);
wrapper.find('button').simulate('click');
expect(onButtonClick).to.have.property('callCount', 1);
});
});
Read the full API Documentation
import React from 'react';
import sinon from 'sinon';
import { expect } from 'chai';
import { mount } from 'enzyme';
import Foo from './Foo';
describe('<Foo />', () => {
it('allows us to set props', () => {
const wrapper = mount(<Foo bar="baz" />);
expect(wrapper.props().bar).to.equal('baz');
wrapper.setProps({ bar: 'foo' });
expect(wrapper.props().bar).to.equal('foo');
});
it('simulates click events', () => {
const onButtonClick = sinon.spy();
const wrapper = mount((
<Foo onButtonClick={onButtonClick} />
));
wrapper.find('button').simulate('click');
expect(onButtonClick).to.have.property('callCount', 1);
});
it('calls componentDidMount', () => {
sinon.spy(Foo.prototype, 'componentDidMount');
const wrapper = mount(<Foo />);
expect(Foo.prototype.componentDidMount).to.have.property('callCount', 1);
Foo.prototype.componentDidMount.restore();
});
});
Read the full API Documentation
import React from 'react';
import { expect } from 'chai';
import { render } from 'enzyme';
import Foo from './Foo';
describe('<Foo />', () => {
it('renders three `.foo-bar`s', () => {
const wrapper = render(<Foo />);
expect(wrapper.find('.foo-bar')).to.have.lengthOf(3);
});
it('renders the title', () => {
const wrapper = render(<Foo title="unique" />);
expect(wrapper.text()).to.contain('unique');
});
});
Read the full API Documentation
Enzyme supports react hooks with some limitations in .shallow() due to upstream issues in React's shallow renderer:
useEffect() and useLayoutEffect() don't get called in the React shallow renderer. Related issue
useCallback() doesn't memoize callback in React shallow renderer. Related issue
ReactTestUtils.act() wrapIf you're using React 16.8+ and .mount(), Enzyme will wrap apis including .simulate(), .setProps(), .setContext(), .invoke() with ReactTestUtils.act() so you don't need to manually wrap it.
A common pattern to trigger handlers with .act() and assert is:
const wrapper = mount(<SomeComponent />);
act(() => wrapper.prop('handler')());
wrapper.update();
expect(/* ... */);
We cannot wrap the result of .prop() (or .props()) with .act() in Enzyme internally since it will break the equality of the returned value.
However, you could use .invoke() to simplify the code:
const wrapper = mount(<SomeComponent />);
wrapper.invoke('handler')();
expect(/* ... */);
See the Contributors Guide
Organizations and projects using enzyme can list themselves here.