Gathering detailed insights and metrics for enzyme-shallow-equal
Gathering detailed insights and metrics for enzyme-shallow-equal
Gathering detailed insights and metrics for enzyme-shallow-equal
Gathering detailed insights and metrics for enzyme-shallow-equal
fast-shallow-equal
[![][npm-badge]][npm-url] [![][travis-badge]][travis-url]
is-equal-shallow
Does a shallow comparison of two objects, returning false if the keys or values differ.
shallow-equal
Typescript-compatible minimalistic shallow equality check for arrays/objects
@wordpress/is-shallow-equal
Test for shallow equality between two objects or arrays.
npm install enzyme-shallow-equal
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
19,954 Stars
2,026 Commits
2,010 Forks
251 Watching
44 Branches
368 Contributors
Updated on 28 Nov 2024
JavaScript (99.98%)
Shell (0.02%)
Cumulative downloads
Total Downloads
Last day
-11.5%
418,612
Compared to previous day
Last week
2.6%
2,423,952
Compared to previous week
Last month
8.6%
9,953,948
Compared to previous month
Last year
-13%
105,952,878
Compared to previous year
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:
1npm 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.
1import Enzyme from 'enzyme'; 2import Adapter from 'enzyme-adapter-react-16'; 3 4Enzyme.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
1import React from 'react'; 2import { expect } from 'chai'; 3import { shallow } from 'enzyme'; 4import sinon from 'sinon'; 5 6import MyComponent from './MyComponent'; 7import Foo from './Foo'; 8 9describe('<MyComponent />', () => { 10 it('renders three <Foo /> components', () => { 11 const wrapper = shallow(<MyComponent />); 12 expect(wrapper.find(Foo)).to.have.lengthOf(3); 13 }); 14 15 it('renders an `.icon-star`', () => { 16 const wrapper = shallow(<MyComponent />); 17 expect(wrapper.find('.icon-star')).to.have.lengthOf(1); 18 }); 19 20 it('renders children when passed in', () => { 21 const wrapper = shallow(( 22 <MyComponent> 23 <div className="unique" /> 24 </MyComponent> 25 )); 26 expect(wrapper.contains(<div className="unique" />)).to.equal(true); 27 }); 28 29 it('simulates click events', () => { 30 const onButtonClick = sinon.spy(); 31 const wrapper = shallow(<Foo onButtonClick={onButtonClick} />); 32 wrapper.find('button').simulate('click'); 33 expect(onButtonClick).to.have.property('callCount', 1); 34 }); 35});
Read the full API Documentation
1import React from 'react'; 2import sinon from 'sinon'; 3import { expect } from 'chai'; 4import { mount } from 'enzyme'; 5 6import Foo from './Foo'; 7 8describe('<Foo />', () => { 9 it('allows us to set props', () => { 10 const wrapper = mount(<Foo bar="baz" />); 11 expect(wrapper.props().bar).to.equal('baz'); 12 wrapper.setProps({ bar: 'foo' }); 13 expect(wrapper.props().bar).to.equal('foo'); 14 }); 15 16 it('simulates click events', () => { 17 const onButtonClick = sinon.spy(); 18 const wrapper = mount(( 19 <Foo onButtonClick={onButtonClick} /> 20 )); 21 wrapper.find('button').simulate('click'); 22 expect(onButtonClick).to.have.property('callCount', 1); 23 }); 24 25 it('calls componentDidMount', () => { 26 sinon.spy(Foo.prototype, 'componentDidMount'); 27 const wrapper = mount(<Foo />); 28 expect(Foo.prototype.componentDidMount).to.have.property('callCount', 1); 29 Foo.prototype.componentDidMount.restore(); 30 }); 31});
Read the full API Documentation
1import React from 'react'; 2import { expect } from 'chai'; 3import { render } from 'enzyme'; 4 5import Foo from './Foo'; 6 7describe('<Foo />', () => { 8 it('renders three `.foo-bar`s', () => { 9 const wrapper = render(<Foo />); 10 expect(wrapper.find('.foo-bar')).to.have.lengthOf(3); 11 }); 12 13 it('renders the title', () => { 14 const wrapper = render(<Foo title="unique" />); 15 expect(wrapper.text()).to.contain('unique'); 16 }); 17});
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:
1const wrapper = mount(<SomeComponent />); 2act(() => wrapper.prop('handler')()); 3wrapper.update(); 4expect(/* ... */);
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:
1const wrapper = mount(<SomeComponent />); 2wrapper.invoke('handler')(); 3expect(/* ... */);
See the Contributors Guide
Organizations and projects using enzyme
can list themselves here.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 3/30 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More