Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Syden10/be8570ab47685a8464f9ce91d2bbb670 to your computer and use it in GitHub Desktop.

Select an option

Save Syden10/be8570ab47685a8464f9ce91d2bbb670 to your computer and use it in GitHub Desktop.
[React + Vite]: Jest + React Testing Library setup

Jest + React Testing Library installation and setup

For React + Vite projects

  1. Dependencies without TypeScript:
yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react 
yarn add --dev @testing-library/react @testing-library/dom @types/jest jest-environment-jsdom

With TypeScript (optional):

yarn add --dev jest babel-jest @babel/preset-env @babel/preset-react
yarn add --dev @testing-library/react @testing-library/dom @types/react @types/react-dom
  1. Set package.json scripts:
"scripts: {
  ...
  "test": "jest --watchAll"
  1. Create babel.config.json file:
{
  "presets": [
    ["@babel/preset-env", { "targets": { "esmodules": true } }],
    ["@babel/preset-react", { "runtime": "automatic" }]
  ]
}
  1. Create jest.config.js file:
export default {
  testEnvironment: 'jest-environment-jsdom',
  // setupFiles: ['./jest.setup.js'],
};

Or, if jest.setup.js file will be used (create it at root), include it as setupFiles:

export default {
  testEnvironment: 'jest-environment-jsdom',
  setupFiles: ['./jest.setup.js'],
};
  1. Add jest to eslintrc.cjs file:
"env": {
    "jest": true
}
@Syden10
Copy link
Author

Syden10 commented Jul 11, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment