Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
jest-enzyme
Advanced tools
The jest-enzyme package provides a set of custom matchers for using Enzyme with Jest, making it easier to write and read tests for React components.
toBeChecked
This matcher checks if a checkbox input is checked.
expect(wrapper.find('input[type="checkbox"]').first()).toBeChecked();
toHaveClassName
This matcher checks if a component has a specific class name.
expect(wrapper.find('.my-class')).toHaveClassName('my-class');
toHaveStyle
This matcher checks if a component has a specific inline style.
expect(wrapper.find('div')).toHaveStyle('display', 'none');
toContainReact
This matcher checks if a component contains a specific React element.
expect(wrapper).toContainReact(<MyComponent />);
toHaveText
This matcher checks if a component contains specific text.
expect(wrapper.find('p')).toHaveText('Hello World');
jest-dom provides a set of custom matchers to test the state of the DOM. It is similar to jest-enzyme but is more focused on testing DOM nodes rather than React components specifically.
Enzyme itself is a testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output. jest-enzyme is built on top of Enzyme to provide Jest matchers.
react-testing-library is a lightweight solution for testing React components. It encourages testing components in a way that is similar to how users interact with them. It can be used with Jest and provides a different approach compared to Enzyme.
We suggest using yarn for installations.
yarn add jest-enzyme --dev
But npm works too!
$ npm install jest-enzyme --save-dev
The simplest setup is to use jest's setupTestFrameworkScriptFile
config.
On older versions of Jest, you'll also need to tell jest to unmock
react
,enzyme
, andjest-enzyme
.
Make sure your package.json
includes the following:
"jest": {
"setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js",
// old versions of jest set the unmocks
"unmockedModulePathPatterns": [
"react",
"enzyme",
"jest-enzyme"
]
},
There is a special environment to simplify using enzyme with jest. Check it out here
If you are using Create React App, instead of adding to your package.json
as above, you will need to add a src/setupTests.js
file to your app, to import jest-enzyme:
// src/setupTests.js
import 'jest-enzyme';
This is documented on Create React App at the bottom of the Testing Components section. There is also more information about Initializing Test Environment.
FAQs
Testing Matchers for Enzyme
We found that jest-enzyme demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.