How vitest-browser-react differs from @testing-library/react
mainWhile inspired by @testing-library/react, vitest-browser-react is optimized for Vitest Browser Mode:
- Integrated Locators: It uses Vitest's built-in locators. This allows Vitest to continue querying an element during an assertion. If an element exists but is in an invalid state (e.g., not yet visible), the assertion will retry until the condition is met, reducing flakiness.
- CDP-based Interactions: Instead of faking events (like
user-eventoften does), it uses the Chrome DevTools Protocol (CDP) for interactions like.click(), which more closely mimics real browser behavior. - Simplified API: It streamlines the API to avoid the confusion between DOM/render libraries and user-event libraries that can occur when using standard testing-library in Browser Mode.
// Comparison: Integrated Locators vs findBy
// vitest-browser-react (Integrated)
await expect.element(page.getByRole('button')).toBeVisible()
// @testing-library/react (Standard)
const button = await screen.findByRole('button')
expect(button).toBeVisible()