component-testing-patterns
Vitest browser mode component testing. Use for testing Svelte 5 components with real browsers, locators, accessibility patterns, and reactive state.
When & Why to Use This Skill
This Claude skill provides comprehensive patterns and best practices for Vitest browser mode component testing, specifically optimized for Svelte 5. It enables developers to perform high-fidelity testing using real browsers and Playwright, focusing on semantic locators and accessibility-first patterns to ensure robust, reactive UI components.
Use Cases
- Testing Svelte 5 components with reactive state (Runes) in a real browser environment to ensure correct DOM updates and state synchronization.
- Implementing accessibility-focused testing using semantic queries like getByRole and getByLabelText to improve web standards compliance and user experience.
- Migrating from jsdom-based unit tests to real browser testing with Playwright to catch browser-specific rendering issues and timing-related bugs.
- Developing robust UI test suites by leveraging Vitest's auto-retry locators and asynchronous assertions to reduce test flakiness in complex interactions.
| name | component-testing-patterns |
|---|---|
| description | Vitest browser mode component testing. Use for testing Svelte 5 components with real browsers, locators, accessibility patterns, and reactive state. |
Component Testing Patterns
Quick Start
import { page } from 'vitest/browser';
import { render } from 'vitest-browser-svelte';
render(Button, { label: 'Click' });
await page.getByRole('button', { name: 'Click' }).click();
await expect.element(page.getByRole('button')).toBeInTheDocument();
Core Principles
- Locators, never containers:
page.getByRole()auto-retries - Semantic queries:
getByRole(),getByLabelText()for accessibility - Await assertions:
await expect.element(el).toBeInTheDocument() - Real browsers: Tests run in Playwright, not jsdom
Common Patterns
- Locators:
page.getByRole('button'),.first(),.nth(0),.last() - Interactions:
await input.fill('text'),await button.click() - Runes: Use
.test.svelte.tsfiles,flushSync(),untrack() - Files:
*.svelte.test.ts(browser),*.ssr.test.ts(SSR),*.test.ts(server)
References
- setup-configuration.md - Complete Vitest browser setup
- testing-patterns.md - Comprehensive testing patterns
- locator-strategies.md - Semantic locator guide
- troubleshooting.md - Common issues and fixes