Vitest can reuse Vite’s configuration and transform pipeline, but it is not automatically faster for every project. Measure your own suite and CI environment. React Testing Library helps keep component tests focused on what a user can observe and do.
Configure a DOM Test Environment
Install Vitest, a DOM environment such as jsdom, React Testing Library, user-event, and jest-dom as development dependencies. Configure Vitest to use jsdom; the example below shows the relevant setting:
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: { environment: 'jsdom' },
});
Test an Accessible User Flow
Prefer roles and accessible labels over component state or CSS selectors. This example assumes the form exposes an accessible email label, a Sign in button, and a Welcome heading after a successful submission.
import '@testing-library/jest-dom/vitest';
import { afterEach, expect, test } from 'vitest';
import { cleanup, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { LoginForm } from './LoginForm';
afterEach(() => cleanup());
test('submits the login form and shows a welcome heading', async () => {
const user = userEvent.setup();
render(<LoginForm />);
await user.type(screen.getByRole('textbox', { name: 'Email address' }), 'test@example.com');
await user.click(screen.getByRole('button', { name: 'Sign in' }));
expect(await screen.findByRole('heading', { name: 'Welcome' })).toBeInTheDocument();
});
The jest-dom/vitest import supplies toBeInTheDocument. If the test runner or component uses different labels, update the queries to match the actual accessible interface rather than weakening them to implementation details.
Choose the Right Query and Cleanup
getBy*is for an element expected to exist now; it fails immediately when it does not.findBy*waits for an element that appears asynchronously and returns a Promise.queryBy*is useful when asserting that an element is absent.
Reset only the mocks or timers that each test changes, and avoid shared mutable state. Add parallel workers gradually and check for race conditions before increasing CI concurrency.

