Testing your app
Unit and end-to-end tests for screens built with Glyph.
Glyph components are ordinary React components with accessible roles and names, so you test them the way users find them: by role, label and text. This guide covers Vitest with Testing Library for components, and Playwright for whole flows.
Unit tests
npm install -D vitest jsdom @testing-library/react @testing-library/user-event @testing-library/jest-dom
Render screens inside a GlyphProvider, as your app does. Two props make tests faster and steadier: motion="none" turns animation off, and storageKey={null} keeps the color mode out of localStorage.
import { GlyphProvider } from '@glyphui/ui'
import { render } from '@testing-library/react'
import type { ReactElement } from 'react'
export function renderWithGlyph(ui: ReactElement) {
return render(
<GlyphProvider motion="none" storageKey={null}>
{ui}
</GlyphProvider>,
)
}
jsdom lacks a few browser APIs that Radix and Glyph use. Stub them once in your setup file:
import '@testing-library/jest-dom/vitest'
window.matchMedia ??= (query: string) =>
({
matches: false,
media: query,
onchange: null,
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
dispatchEvent: () => false,
}) as MediaQueryList
window.HTMLElement.prototype.scrollIntoView ??= () => {}
window.HTMLElement.prototype.hasPointerCapture ??= () => false
window.HTMLElement.prototype.releasePointerCapture ??= () => {}
globalThis.ResizeObserver ??= class {
observe() {}
unobserve() {}
disconnect() {}
}
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: { environment: 'jsdom', setupFiles: ['./src/test/setup.ts'] },
})
Then test behavior, not markup. Use userEvent rather than fireEvent, so focus and pointer events happen in the order a browser sends them:
import { Button, useConfirm } from '@glyphui/ui'
import { screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { expect, it, vi } from 'vitest'
import { renderWithGlyph } from './test/render'
function DeleteButton({ onDelete }: { onDelete: () => void }) {
const confirm = useConfirm()
return (
<Button
onClick={async () => {
if (await confirm({ title: 'Delete project?', tone: 'danger' })) onDelete()
}}
>
Delete
</Button>
)
}
it('deletes only after confirming', async () => {
const onDelete = vi.fn()
renderWithGlyph(<DeleteButton onDelete={onDelete} />)
await userEvent.click(screen.getByRole('button', { name: 'Delete' }))
// The confirm dialog loads on first use, so find it rather than get it.
const dialog = await screen.findByRole('alertdialog', { name: 'Delete project?' })
await userEvent.click(within(dialog).getByRole('button', { name: 'Delete' }))
expect(onDelete).toHaveBeenCalledOnce()
})
Overlays (dialogs, menus, selects, toasts) render at the end of document.body, so query them through screen, not the container your component rendered into.
Forms
Test what users see: fill fields by label, submit, and look for the message.
import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { expect, it } from 'vitest'
import { SignupForm } from './signup-form'
import { renderWithGlyph } from './test/render'
it('shows a field error for an invalid email', async () => {
renderWithGlyph(<SignupForm />)
await userEvent.type(screen.getByRole('textbox', { name: /email/i }), 'not-an-email')
await userEvent.click(screen.getByRole('button', { name: 'Create account' }))
expect(await screen.findByText('Enter a valid email address')).toBeInTheDocument()
})
End-to-end tests
Playwright runs your built app in real browsers. A few things specific to apps built with Glyph and React Router:
- Wait for content, not URLs. React Router 7 renders the next page in a transition, so the URL changes a moment before the page does. Typing right after
toHaveURLcan land in the previous page's field. - Find overlays by role. Menus are
menu, dialogsdialog, confirmationsalertdialog. Find toasts by their text. - Turn motion off for screenshots. Use
reducedMotion: 'reduce'in the Playwright config; Glyph then skips every animation.
import { expect, test } from '@playwright/test'
test('creates an invoice', async ({ page }) => {
await page.goto('/app')
await page.getByRole('link', { name: 'Invoices' }).click()
// The page itself, not just its URL, before interacting.
await expect(page.getByRole('heading', { name: 'Invoices' })).toBeVisible()
await page.getByRole('button', { name: 'New invoice' }).click()
const dialog = page.getByRole('dialog', { name: 'New invoice' })
await dialog.getByRole('spinbutton', { name: 'Amount' }).fill('1250')
await dialog.getByRole('button', { name: 'Create invoice' }).click()
await expect(page.getByText('Invoice created')).toBeVisible()
await expect(dialog).toBeHidden()
})
Add axe checks to the same suite; Accessibility has the setup.
Live page: https://glyphui.xyz/docs/testing