enzymejs /enzyme

JavaScript Testing utilities for React

★ 19,800⑂ 1,959👁 243⑃ 818● 1,773▤ 0
JavaScriptMITCreated 2015-11-10enzymejs.github.io/enzyme/
+0
日增长
19,800
stars
1,959
forks
243
watchers
818
prs
1,773
issues
0
releases

README

Enzyme
=======



   


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.

Upgrading from Enzyme 2.x or React ~16.0.0-0 &124;&124; ~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.

js
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });


3rd Party Adapters
=============

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

Running Enzyme Tests
===========

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 Mocha

Using Enzyme with Karma

Using Enzyme with Browserify

Using Enzyme with SystemJS

Using Enzyme with Webpack

Using Enzyme with JSDOM

Using Enzyme with React Native

Using Enzyme with Jest

Using Enzyme with Lab

Using Enzyme with Tape and AVA

Basic Usage
===========

 Shallow Rendering

javascript
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('', () = {
  it('renders three  components', () = {
    const wrapper = shallow();
    expect(wrapper.find(Foo)).to.have.lengthOf(3);
  });

  it('renders an .icon-star', () = {
    const wrapper = shallow();
    expect(wrapper.find('.icon-star')).to.have.lengthOf(1);
  });

  it('renders children when passed in', () = {
    const wrapper = shallow((
      
        
      
    ));
    expect(wrapper.contains()).to.equal(true);
  });

  it('simulates click events', () = {
    const onButtonClick = sinon.spy();
    const wrapper = shall

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON