Star on GitHub

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:

Terminal
npm install -D vitest jsdom @vitejs/plugin-react @testing-library/react @testing-library/user-event @testing-library/jest-dom

Each component imports its own CSS. Node can't load a stylesheet, so tell Vitest to run the Orvain packages through Vite, which can:

// vitest.config.ts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
test: {
environment: "jsdom",
setupFiles: ["./vitest.setup.ts"],
css: true,
// the components import their own CSS: let Vite handle it
server: { deps: { inline: [/@orvain\//] } },
},
});
vitest.setup.ts
import "@testing-library/jest-dom/vitest";
import "@orvain/tokens/css";
On Jest, which can't import CSS, map stylesheets to an empty module with 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, not fireEvent. 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.

// SignUpForm.test.tsx
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { expect, test, vi } from "vitest";
import { SignUpForm } from "./SignUpForm";
test("shows errors, then submits valid values", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<SignUpForm onSubmit={onSubmit} />);
// submit empty: every field reports its error
await user.click(screen.getByRole("button", { name: "Create account" }));
expect(await screen.findByText("Enter a valid email address")).toBeInTheDocument();
expect(screen.getByText("Pick a role")).toBeInTheDocument();
expect(screen.getByText("Accept the terms to continue")).toBeInTheDocument();
// the error is wired for screen readers too
const email = screen.getByLabelText("Email address");
expect(email).toHaveAttribute("aria-invalid", "true");
expect(email).toHaveAccessibleDescription(/valid email/);
// fill it in the way a person would
await user.type(email, "ada@example.com");
await user.click(screen.getByRole("combobox", { name: "Role" }));
await user.click(screen.getByRole("option", { name: "Engineer" }));
await user.click(screen.getByRole("checkbox", { name: "I accept the terms" }));
await user.click(screen.getByRole("button", { name: "Create account" }));
expect(onSubmit).toHaveBeenCalledWith({ email: "ada@example.com", role: "eng", terms: true }, expect.anything());
});

Roles by component#

What each form and navigation component exposes, and the query that finds it:

ComponentRoleQuery
FlatButton, LinkButton
button
getByRole("button", { name: "Save" })
Input
textbox (by its label)
getByLabelText("Email address")
TextArea (variant v1)
textbox (by its label)
getByLabelText("Notes")
Checkbox, CheckboxWithLabel
checkbox
getByRole("checkbox", { name: "Subscribe" })
Switch, SwitchWithLabel
switch
getByRole("switch", { name: "Wi-Fi" })
DropdownField
combobox, then option
getByRole("combobox", { name: "Role" })
Slider
slider
getByRole("slider")
SegmentedControl
radiogroup, radio
getByRole("radio", { name: "Week" })
HorizontalTabMenu, VerticalTabMenu
tablist, tab
getByRole("tab", { name: "Billing" })
Accordion
button, region
getByRole("button", { name: "Shipping" })
OtpInput
group of textboxes
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:

playwright.config.ts
export default defineConfig({
use: { reducedMotion: "reduce" },
});

In a real browser#

For layout, focus rings, dark mode and screenshots, use Playwright. The same role queries work there:

signup.spec.ts
import { test, expect } from "@playwright/test";
test("sign-up form", async ({ page }) => {
await page.goto("/signup");
await page.getByLabel("Email address").fill("ada@example.com");
await page.getByRole("combobox", { name: "Role" }).click();
await page.getByRole("option", { name: "Engineer" }).click();
await page.getByRole("checkbox", { name: "I accept the terms" }).check();
await expect(page).toHaveScreenshot("signup-filled.png");
});

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".