Installations
npm install enzyme-shallow-equal
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
21.2.0
NPM Version
10.2.3
Statistics
19,954 Stars
2,026 Commits
2,010 Forks
251 Watching
44 Branches
368 Contributors
Updated on 28 Nov 2024
Languages
JavaScript (99.98%)
Shell (0.02%)
Total Downloads
Cumulative downloads
Total Downloads
549,990,586
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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
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.
Installation
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() });
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 React Native
Using Enzyme with Tape and AVA
Basic Usage
Shallow Rendering
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
Full DOM Rendering
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
Static Rendered Markup
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
React Hooks support
Enzyme supports react hooks with some limitations in .shallow()
due to upstream issues in React's shallow renderer:
-
useEffect()
anduseLayoutEffect()
don't get called in the React shallow renderer. Related issue -
useCallback()
doesn't memoize callback in React shallow renderer. Related issue
ReactTestUtils.act()
wrap
If 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(/* ... */);
Future
Contributing
See the Contributors Guide
In the wild
Organizations and projects using enzyme
can list themselves here.
License
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
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/enzymejs/.github/SECURITY.md:1
- Info: Found linked content: github.com/enzymejs/.github/SECURITY.md:1
- Warn: One or no descriptive hints of disclosure, vulnerability, and/or timelines in security policy
- Info: Found text in security policy: github.com/enzymejs/.github/SECURITY.md:1
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
- Warn: no topLevel permission defined: .github/workflows/node-pretest.yml:1
- Warn: no topLevel permission defined: .github/workflows/node.yml:1
- Warn: no topLevel permission defined: .github/workflows/rebase.yml:1
- Warn: no topLevel permission defined: .github/workflows/require-allow-edits.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node-pretest.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node-pretest.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/node-pretest.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node-pretest.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:43: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/node.yml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:141: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:142: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/node.yml:150: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/node.yml:159: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/node.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/rebase.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/rebase.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/rebase.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/rebase.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/require-allow-edits.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/enzymejs/enzyme/require-allow-edits.yml/master?enable=pin
- Info: 0 out of 8 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 7 third-party GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 3 are checked with a SAST tool
Score
4.6
/10
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 MoreOther packages similar to 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.