Skip to content

Instantly share code, notes, and snippets.

@GOJAx64
Last active June 23, 2022 21:26
Show Gist options
  • Save GOJAx64/38a7dbea046d5c88d72f75095a4fb879 to your computer and use it in GitHub Desktop.
Save GOJAx64/38a7dbea046d5c88d72f75095a4fb879 to your computer and use it in GitHub Desktop.
Guide to install and configure Jest + React Testing Library

Installation and configuration of Jest + React Testing Library

In projects of React + Vite

  1. Isntallations:
yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react 
yarn add --dev @testing-library/react @types/jest jest-environment-jsdom
  1. Optional: If we use Fetch API in the project:
yarn add --dev whatwg-fetch
  1. Update the scripts of package.json
"scripts: {
  ...
  "test": "jest --watchAll"
  1. Create babel configuration babel.config.js
module.exports = {
    presets: [
        [ '@babel/preset-env', { targets: { esmodules: true } } ],
        [ '@babel/preset-react', { runtime: 'automatic' } ],
    ],
};
  1. Optional, but eventually necessary, create Jest config and setup:

jest.config.js

module.exports = {
    testEnvironment: 'jest-environment-jsdom',
    setupFiles: ['./jest.setup.js']
}

jest.setup.js

// In case you need to implement the FetchAPI
import 'whatwg-fetch'; // <-- yarn add whatwg-fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment