Skip to content
ClapelloNotes by Barath
ReactCh 13 · 2/6
Core4 min readUpdated

React / 71

Set up Vitest (or Jest) with React Testing Library

TL;DR

Install a test runner, a fake browser (jsdom) and the Testing Library packages, then point them at each other with a few lines of config.

Who does what

vitest or jest

the test runner: finds *.test.jsx files, runs them, reports pass/fail

jsdom

a fake browser DOM (document, window, events) running in Node

@testing-library/react

render · screen

puts a component in the fake DOM and finds things in it

@testing-library/user-event

user.click · user.type

acts like a real user

@testing-library/jest-dom

toBeInTheDocument()

extra expect() checks for the DOM
The runner finds and runs your tests. jsdom fakes a browser inside Node. The Testing Library packages render, interact and check.
PackageJob
vitest or jestThe runner: finds test files, runs them, reports results. Also gives you test, expect, mocks.
jsdomA fake browser DOM in Node (document, window, events). Tests don't open a real browser.
@testing-library/react + @testing-library/domrender a component, then find things with screen
@testing-library/jest-domDOM checks like toBeInTheDocument(). Works with Vitest too, despite the name.
@testing-library/user-eventRealistic clicks and typing

The setup

Use Vitest if your app uses Vite (today's default). Use Jest if your project already does, or isn't on Vite. (Vitest 5 needs Vite 6.4+ and Node 22.12+.)

npm install -D vitest jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom @testing-library/user-event
npm install -D jest jest-environment-jsdom babel-jest @babel/core @babel/preset-env @babel/preset-react @testing-library/react @testing-library/dom @testing-library/jest-dom @testing-library/user-event

Vitest

Add a test block to the Vite config you already have:

vite.config.js
/// <reference types="vitest/config" />
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
 
export default defineConfig({
  plugins: [react()],
  test: {
    environment: "jsdom",        // fake browser
    globals: true,               // test/expect without imports + auto cleanup
    setupFiles: "./src/setupTests.js",
  },
});
src/setupTests.js
import "@testing-library/jest-dom/vitest";
package.json (excerpt)
{
  "scripts": {
    "test": "vitest",
    "coverage": "vitest run --coverage"
  }
}

npm test starts in watch mode and re-runs the affected tests on every save; npx vitest run runs once (handy for CI).

Jest

Jest doesn't understand JSX or import on its own, so Babel translates your files first:

babel.config.json
{
  "presets": [
    ["@babel/preset-env", { "targets": { "node": "current" } }],
    ["@babel/preset-react", { "runtime": "automatic" }]
  ]
}
jest.config.json
{
  "testEnvironment": "jsdom",
  "setupFilesAfterEnv": ["<rootDir>/jest.setup.js"]
}
jest.setup.js
import "@testing-library/jest-dom";

Then add "test": "jest" and "test:watch": "jest --watch" to your scripts. (npm init jest@latest can also generate a config by asking you questions.)

Where tests live

Name test files *.test.jsx (or .test.js, .spec.jsx) and put them next to the code they test, or in a __tests__ folder. Both runners find them automatically. Add coverage/ to .gitignore: it's generated output.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.