Skip to content

Instantly share code, notes, and snippets.

@arroyostack
Forked from Klerith/vite-testing-config.md
Last active July 13, 2023 10:33
Show Gist options
  • Save arroyostack/391a69780abea5c896414bc323d68024 to your computer and use it in GitHub Desktop.
Save arroyostack/391a69780abea5c896414bc323d68024 to your computer and use it in GitHub Desktop.
Vite + Jest + React Testing Library - Config follow up.

Installs and configuration for Jest + React Testing Library

For React + Vite projects

  1. Necessary installs:
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 package.json scripts
"scripts: {
  ...
  "test": "jest --watchAll"
  1. Create babel configuration file babel.config.js. Warning!: if you get a module type error, rename file to extension .cjs
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