Using create-react-app

If you are using create-react-app, then it comes up with default configuration to write the unit test cases using jest. To create a new application using create-react-app, run the following command

create-react-app sample-app

If you are using npx, then run the command

npx create-react-app sample-app

It will create a new folder called "sample-app".

You can view the sample test in the file sample-app/src/App.test.js .

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);

You can run the above test using the command

yarn test

Setting up the environment without create-react-app

If you are not using create-react-app, then you need to install the dependencies manually. Because jest is a independent library for testing, it does not require react to be installed. You can get started with testing using jest by simply creating a module and installing jest as dependency and writing the test case.

You can create a sample project using webpack, and integrate jest into it, although it has its own unique challenges as webpack is deeply integrated with the application. Although the scenario where you will create a custom boilerplate for react from scratch with webpack is rare, but if you do so, then you can learn more about integrating jest with the webpack here