+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 = shallTop issues
#2429Support for React 17108 comments · 2020-08-11#2086`useEffect` not called when the component is `shallow` renderered68 comments · 2019-04-08#2011React hooks support checklist55 comments · 2019-02-08#1436Create Adapter for React Native & React 1644 comments · 2017-12-15#539Improve shallow rendering for use with higher-order components39 comments · 2016-08-12#2176useContext hook not working with shallow36 comments · 2019-06-27#1553Complete React 16 support36 comments · 2018-03-02#1925ReactWrapper::setProps() can only be called on the root35 comments · 2018-12-04