Sveltest Testing Documentation
Testing patterns for Svelte 5 with vitest-browser-svelte
Use locators, never containers
page.getByRole('button', { name: 'Submit' })
container.querySelector('button')
Always await element assertions
await expect.element(locator).toBeVisible()
expect(element).toBeVisible()
Use semantic queries for accessibility
page.getByRole('button'), page.getByLabelText('Email')
page.getByTestId('submit-btn')
import { describe, expect, test } from 'vitest';
import { render } from 'vitest-browser-svelte';
import { page } from '@vitest/browser/context';
import Button from './Button.svelte';
test('renders button with text', async () => {
render(Button, { text: 'Click me' });
await expect.element(page.getByRole('button', { name: 'Click me' })).toBeVisible();
});
test('handles click event', async () => {
const { component } = render(Counter);
await page.getByRole('button', { name: '+' }).click();
await expect.element(page.getByText('Count: 1')).toBeVisible();
});
Clicking submit buttons with SvelteKit enhance
Test form state directly instead of clicking submit
Elements with CSS animations may not be clickable
Use { force: true } for clicking animated elements