Testing
Test what a person sees and does, not how a component is built. The components expose real roles and labels, so Testing Library finds them the way a screen reader does, and your tests survive a library update.
Setup#
Vitest with jsdom and Testing Library, for React or Vue:
Each component imports its own CSS. Node can't load a stylesheet, so tell Vitest to run the Orvain packages through Vite, which can:
moduleNameMapper: { "\\.css$": "<rootDir>/empty.js" }.Find things by role#
- Query by role and accessible name:
getByRole("button", { name: "Save" }). For fields,getByLabelText. - Never query by class name. The class names are generated and change between releases.
- Drive the UI with
user-event, notfireEvent. It types, clicks and tabs the way a person does, so a dropdown opens and its options get focus. - If a query can't find something by role, that is often a real accessibility gap. Check the label before reaching for a test id.
Testing a form#
This tests the sign-up form from the forms guide: it submits empty, checks each error and that it is announced, then fills the form in and checks what was submitted.
Roles by component#
What each form and navigation component exposes, and the query that finds it:
getByRole("button", { name: "Save" })getByLabelText("Email address")getByLabelText("Notes")getByRole("checkbox", { name: "Subscribe" })getByRole("switch", { name: "Wi-Fi" })getByRole("combobox", { name: "Role" })getByRole("slider")getByRole("radio", { name: "Week" })getByRole("tab", { name: "Billing" })getByRole("button", { name: "Shipping" })getAllByRole("textbox")Animations#
jsdom doesn't run CSS transitions, so components settle at once and tests don't wait on them. In a real browser, turn on reduced motion. The tokens stylesheet then cuts every transition and animation to almost nothing:
In a real browser#
For layout, focus rings, dark mode and screenshots, use Playwright. The same role queries work there:
To test dark mode, set data-theme="dark" on the page root, or run the browser with colorScheme: "dark" when your root uses data-theme="system".
