Render React components in Vitest Browser Mode. This library follows testing-library principles and exposes only locators and utilities that encourage you to write tests that closely resemble how your React components are used.
vitest-browser-react aims to deliver a good developer experience in Vitest Browser Mode by incorporating the locators API and retry-ability mechanism directly into the render result. This allows you to call user methods without needing to verify the element's existence or wait for external events (like API calls) to render the element.
Requires vitest 4.0.0 or higher.
import { render } from 'vitest-browser-react'import { expect, test } from 'vitest'test('counter button increments the count', async () => { const screen = await render(<Component count={1} />) await screen.getByRole('button', { name: 'Increment' }).click() await expect.element(screen.getByText('Count is 2')).toBeVisible()})
💡 This library doesn't expose React's act and uses it only to flush operations happening as part of useEffect during initial rendering and unmounting.
Other use cases are handled by CDP and expect.element which both have built-in retry-ability mechanism.
vitest-browser-react also exposes renderHook helper to test React hooks.
vitest-browser-react also automatically injects render method on the page. Example:
// vitest.config.tsimport { defineConfig } from 'vitest/config'export default defineConfig({ test: { setupFiles: ['./setup-file.ts'], browser: { // ... your config }, },})// ./setup-file.ts// add an import at the top of your setup file so TypeScript can pick up typesimport 'vitest-browser-react'
Unlike @testing-library/react, vitest-browser-react performs cleanup of the component before the test begins, allowing you to see the rendered result in the Browser UI. If you prefer to disable auto-cleanup, you can import the render function from vitest-browser-react/pure.
Configuration
You can configure if the component should be rendered in Strict Mode with configure method from vitest-browser-react/pure:
import { configure } from 'vitest-browser-react/pure'configure({ // disabled by default reactStrictMode: true,})
One nice thing that comes out of this approach is that Vitest can keep querying the element during the assertion instead of before. This means that if the element was found, but it has an invalid state, the assertion will continue checking the element until it works. This makes your tests less flaky.
const button = await screen.findByRole('button')// it's possible that the element is in the dom, but its state is invalid// but it will be valid in a few render cyclesexpect(button).toBeVisible()// even if element is in the dom, it might not be visible yet// vitest will continue checking the validityawait expect.element(page.getByRole('button')).toBeVisible()
Another example is with user-event. Vitest provides a similar API to testing-library, but uses CDP instead of faking events which is closer to how browsers work:
In short, this library integrates well with Vitest Browser Locators API, and that is why it's recommended for the Browser Mode, although you can continue using testing-library if you prefer.
One of the reasons why the Vitest team decided to add our own wrapper was because it became confusing to document all the differences with how testing-library works in Vitest: the dom/render libraries work the same, but user-event is not (except sometimes, such as with the preview provider). Streamlining the API made it easier to explain and more approachable for newcomers.