You start a new project with a NodeJS back end and a React front end, with TypeScript on top, and it's time to write some tests. Completely unaware of the spaghetti that's to come, you start choosing the libraries to help with that. You pick up Jest for the back end, which needs a transformer, an experimental flag and extra dependencies before it even starts understanding your code. For the front end you go with React Testing Library, which needs a setup file, another round of extra packages and uses a fake browser that will happily assert a button has been clicked while your users can't even see it. Before you end up becoming an Italian chef, there's a single tool that can test both the front and back end with a real browser without the pasta.
We'll be using Jest for the back end, right?!
People have been doing it for years and it works. But if your back end is written in TypeScript, uses ES modules (and in 2026 it probably should) and you don't like pasta, Jest will start cooking you spaghetti before you write a single test: ESM support is still experimental and needs the --experimental-vm-modules flag, while TypeScript files need ts-jest or Babel to be transformed (plus @babel/preset-env and @babel/preset-typescript if you go the Babel route, and ts-node for a jest.config.ts).
On the other hand, Vitest runs on top of Vite, so ESM and TypeScript work out of the box, and it can reuse your Vite configuration (aliases, plugins, you name it). The API is Jest compatible, so describe, test and expect are all there, and jest.fn() becomes vi.fn(). Using it is basically a matter of running npm i -D vitest and creating the vitest.config.ts file.
import { defineConfig } from "vitest/config"; export default defineConfig({ test: { // We'll add another project here further down. projects: [ { test: { name: "main", include: ["**/*.test.ts"], environment: "node", globals: true, }, }, ], }, }); Then create your tests.
import { createSpaghetti } from "./services/kitchen"; test("creates the spaghetti", async () => { vi.spyOn(globalThis, "fetch").mockResolvedValue( new Response(null, { status: 201 }), ); const response = await createSpaghetti({ sauce: "carbonara", servings: 2 }); expect(response.status).toEqual(201); }); Run npx vitest. No transformers, no flags, no meatballs.
Worth knowing
- Migrating from Jest is mostly a find-and-replace of
jestwithvi, but some other things behave slightly differently, so check the full guide; - Globals (
test,expect, etc.) are disabled by default. Either setglobals: truelike above and addvitest/globalsto yourtsconfig.jsontypes, or import them fromvitest; - Watch mode is on by default when you're not in CI, and it only reruns the tests affected by what you changed, which is the closest you'll get to instant feedback without a crystal ball.
But for the front end we'll definitely be using React Testing Library, won't we?!
React Testing Library is great, but it starts creating more spaghetti: customizations to work with Jest or another runner (a setup file importing @testing-library/jest-dom for the matchers) and more dependencies (@testing-library/dom, @testing-library/user-event and jest-environment-jsdom). It's also usually paired with jsdom or happy-dom, which are simulations of a browser written in JavaScript. There's no layout, limited CSS and only synthetic events, so your test happily clicks a button that, in an actual browser, is hidden behind a modal. Your tests pass, your users can't click anything, you watch them burst into rage in the session replay from your analytics tool, everyone is happy.
Vitest has Browser Mode, which became stable in Vitest 4 and runs your component tests in a real browser, using Playwright (or WebdriverIO) under the hood. Install @vitest/browser-playwright, @vitejs/plugin-react and vitest-browser-react, and add a project for the component tests.
import react from "@vitejs/plugin-react"; import { playwright } from "@vitest/browser-playwright"; import { defineConfig } from "vitest/config"; export default defineConfig({ test: { projects: [ { test: { name: "main", include: ["**/*.test.ts"], environment: "node", globals: true, }, }, { plugins: [react()], test: { name: "ui", include: ["**/*.spec.tsx"], globals: true, browser: { enabled: true, headless: true, provider: playwright(), instances: [{ browser: "chromium" }], }, }, }, ], }, }); Then test your component like you usually do.
import { page, userEvent } from "vitest/browser"; import { render } from "vitest-browser-react"; import { Counter } from "./components"; test("increments the counter", async () => { await render(<Counter initialValue={0} />); await userEvent.click(page.getByRole("button", { name: "Increment" })); await expect.element(page.getByText("Count: 1")).toBeVisible(); }); The userEvent here doesn't fake events in JavaScript; it goes through Playwright, so a click is an actual click. And expect.element retries the assertion until it passes or times out, so no more sprinkling waitFor all over the place. As a bonus, since there's a real browser rendering things, you get visual regression testing with toMatchScreenshot, so you'll know when someone chooses violence by nudging the button's text 1px off center.
Worth knowing
- Tests run slower than in jsdom, because, well, there's a whole browser running them. It's the price of tests that actually mean something;
- You need the browser installed (
npx playwright install --with-deps chromium), including in your CI; - Screenshots differ between browsers and operating systems, so
toMatchScreenshotstores one per browser and platform. Generate them in the same environment your CI runs in (a container helps). Otherwise, your pipeline will fail because your laptop renders fonts differently.
Wrap-up
One config file and one way of writing tests, for both the back end and the front end, with fewer dependencies to worry about. You can keep everything as it is and open an Italian restaurant or go with Vitest and keep things organized for you and for your AI agent. It's your call.
