Skip to main content
Vitest Browser Mode for React Component Tests: Why We Moved Off jsdom, and the Mocks That Broke

Vitest Browser Mode for React Component Tests: Why We Moved Off jsdom, and the Mocks That Broke

November 16, 2026
Testing
8 min read

Headline: Vitest Browser Mode runs React component tests inside a real Chromium, Firefox or WebKit page driven by Playwright, so layout, focus, CSS and observers behave the way users experience them instead of the way jsdom approximates them. Browser Mode has been stable since Vitest 4.0, and the migration cost sits in mocks and environment shims, not in assertions.

For years our React component tests ran in jsdom, and some of them passed while the component was broken: a dropdown rendered off-screen on narrow viewports, a submit button sat under a sticky footer, a dialog let focus escape to the page behind it. jsdom, a JavaScript implementation of the DOM and HTML standards for Node.js, has no layout engine, so none of those failures could appear in a test. At Devya we moved the tests that depend on layout, focus and real input into Vitest Browser Mode and kept everything else in Node.

Key takeaways

• Real browser, same runner: Vitest Browser Mode executes test files in a real browser page; with the @vitest/browser-playwright provider, Playwright launches and drives Chromium, Firefox or WebKit.

• jsdom cannot measure: jsdom has no layout engine, so getBoundingClientRect() returns zeros and any test about position, overflow or overlap is testing a stub.

• Clicks must be reachable: with the Playwright provider, locator clicks go through Playwright's actionability checks, so a button covered by another element fails the test the same way it fails the user.

• Assertions retry: expect.element() polls a locator until the assertion passes or times out, which replaces most waitFor and findBy calls.

• Split, don't migrate everything: pure logic stays in the node environment, and Vitest projects run both groups in one vitest command.

What does jsdom get wrong about React components?

jsdom gets wrong everything that depends on rendering, because jsdom parses HTML and implements DOM APIs but never computes layout or paints a pixel. These are the gaps our team hit most often:

• getBoundingClientRect(), offsetWidth and scrollHeight report 0, so positioning logic for popovers, tooltips and virtualized lists cannot be asserted.

• IntersectionObserver, ResizeObserver and matchMedia are not implemented, so test setups stub them, and a stub reports whatever the test author expected.

• Media queries and container queries never apply, so responsive variants are invisible to the test.

• fireEvent.click() dispatches straight to the target node whether or not a user could reach it through overlapping elements, pointer-events: none or an off-screen position.

Our jsdom setup file had grown a block of polyfills for exactly these APIs. Each polyfill was a place where the component agreed with a fake we wrote, which is the opposite of what a test is for. That block became our list of tests to move first.

How do we set up Vitest Browser Mode for a React project?

Vitest Browser Mode needs three additions beyond Vitest itself: a provider package (@vitest/browser-playwright), a framework renderer (vitest-browser-react) and the browser binary that Playwright downloads with npx playwright install chromium. Since Vitest 4 the provider is a factory function, playwright(), imported from its own package, replacing the older string option provider: 'playwright'.

We keep node and browser tests in one vitest.config.ts with Vitest projects, the option that replaced the separate vitest.workspace file in Vitest 3.2. A unit project runs every .test.ts file under src with environment set to node. A browser project runs every .browser.test.tsx file under src with browser.enabled set to true, provider set to playwright(), headless set to true and one chromium entry in browser.instances. Both projects set extends: true so they inherit the root Vite plugins, including @vitejs/plugin-react.

The browser project's setup file imports the application's global stylesheet, because a browser test against unstyled markup measures a layout nobody ships. On Tailwind CSS v4 that also means adding the @tailwindcss/vite plugin to the Vitest config so utility classes compile.

What changes in the test code itself?

Queries become locators, interactions become awaited real input, and assertions retry. The render() function from vitest-browser-react mounts a component into the real document and returns locators with the familiar Testing Library names: getByRole, getByLabelText and getByText.

A typical test renders a combobox, clicks the input locator, sends ArrowDown, ArrowDown and Enter through userEvent.keyboard(), then awaits expect.element(input).toHaveValue('Giza') and expect.element(input).toHaveFocus(). A second test calls page.viewport(360, 640), opens the listbox and checks that the listbox's getBoundingClientRect().right is at most 360 pixels. The second test cannot exist in jsdom: there is no viewport to resize and no rectangle to measure.

• userEvent from vitest/browser is not @testing-library/user-event. The Testing Library package simulates events in JavaScript; the Vitest import sends input through the provider, so the browser produces trusted events with real focus and default actions.

• Every interaction is a promise. A missing await on click() races the next assertion, so we lint for floating promises in browser test files.

• expect.element() polls. It re-queries the locator until the matcher passes or the timeout expires, so assertions on state that settles after a transition need no manual waiting.

Which tests should stay out of the browser?

Only tests that need rendering belong in Vitest Browser Mode, because each browser project adds browser startup time and a Playwright binary to every CI run. This is the split we use:

• Pure functions, reducers, Zod schemas and formatters: Vitest with the node environment. No DOM is needed and feedback is fastest.

• Components where only text, roles and ARIA state matter: jsdom or Browser Mode. Both are accurate, so we move these opportunistically.

• Layout, overlap, focus order, observers, media queries and container queries: Vitest Browser Mode, because jsdom cannot represent them.

• Server Components, Server Actions, routing and middleware: Playwright end-to-end tests against next start, because Vite does not run the Next.js compiler or server runtime.

The last rule matters for Next.js projects. Vitest Browser Mode serves files through Vite, so it tests client components in isolation, not the App Router. Anything that needs the Next.js server belongs in end-to-end tests.

What broke when we moved tests into the browser?

The mocks broke, not the assertions. Four fixes covered almost everything:

• vi.spyOn on module exports: native ES module namespace objects in the browser are not writable, so vi.spyOn(api, 'fetchCities') fails. The fix is vi.mock('./api', { spy: true }), which keeps the real implementation but wraps every export in a spy that vi.mocked(fetchCities).mockResolvedValue() can override.

• Next.js-only modules: useRouter() from next/navigation throws outside an App Router tree, and next/font depends on the Next.js compiler. We mock next/navigation per test file and keep components that import next/font out of browser tests.

• process.env does not exist in the browser: code that reads process.env.NEXT_PUBLIC_API_URL crashes in a Vite-served page. A define entry in the browser project replaces those references at transform time.

• State survives between tests in one file: vitest-browser-react unmounts rendered components between tests, but localStorage, cookies and module-level caches persist, so we clear them in beforeEach.

In CI we install only the browser the suite uses with npx playwright install --with-deps chromium. Vitest Browser Mode can save a screenshot of the failing page through the browser.screenshotFailures option, and Vitest 4 added browser.trace for Playwright traces, so a red build arrives with evidence instead of a stack trace alone.

FAQ

Q: Is Vitest Browser Mode stable enough for production test suites?

A: Yes. Vitest Browser Mode left experimental status in Vitest 4.0, released in October 2025, and the provider packages such as @vitest/browser-playwright are versioned in lockstep with Vitest itself.

Q: Does Vitest Browser Mode replace Playwright end-to-end tests?

A: No. Vitest Browser Mode mounts a single component in a Vite-served page, while Playwright end-to-end tests drive the whole application, including server rendering, routing and the backend.

Q: Can we keep our React Testing Library queries?

A: Mostly. vitest-browser-react exposes getByRole, getByLabelText, getByText and related queries as locators, so query lines port almost unchanged; interactions and assertions must become awaited.

Q: Which browsers can Vitest Browser Mode run?

A: With @vitest/browser-playwright it runs Chromium, Firefox and WebKit, and each entry in browser.instances runs the same tests again in that browser. @vitest/browser-webdriverio and @vitest/browser-preview are the other providers; the preview provider uses your own browser and is meant for local debugging, not CI.

Q: Can Vitest Browser Mode test Next.js Server Components?

A: No. Vitest Browser Mode serves code through Vite in a browser page, and async Server Components need the Next.js server runtime, so we cover them with Playwright end-to-end tests against a production build.