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"
]
},
Using with Create React App
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.