Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
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
The npm package jest-enzyme receives a total of 273,583 weekly downloads. As such, jest-enzyme popularity was classified as popular.
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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.