Testing React Apps
At Facebook, we use Jest to test React applications.
Setup
Setup with Create React App
If you are new to React, we recommend using Create React App. It is ready to use and ships with Jest! You will only need to add react-test-renderer
for rendering snapshots.
Run
- npm
- Yarn
- pnpm
npm install --save-dev react-test-renderer
yarn add --dev react-test-renderer
pnpm add --save-dev react-test-renderer
Setup without Create React App
If you have an existing application you'll need to install a few packages to make everything work well together. We are using the babel-jest
package and the react
babel preset to transform our code inside of the test environment. Also see using babel.
Run
- npm
- Yarn
- pnpm
npm install --save-dev jest babel-jest @babel/preset-env @babel/preset-react react-test-renderer
yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react react-test-renderer
pnpm add --save-dev jest babel-jest @babel/preset-env @babel/preset-react react-test-renderer