Introduction
Testing helps make sure your Svelte app works right. It finds mistakes early so users have a smooth experience.
Testing helps make sure your Svelte app works right. It finds mistakes early so users have a smooth experience.
import { render, fireEvent } from '@testing-library/svelte'; import Component from './Component.svelte'; test('description', async () => { const { getByText } = render(Component); const button = getByText('Click me'); await fireEvent.click(button); expect(getByText('Clicked')).toBeInTheDocument(); });
Use render() to load your Svelte component in a test.
fireEvent simulates user actions like clicks.
import { render } from '@testing-library/svelte'; import Hello from './Hello.svelte'; test('shows greeting', () => { const { getByText } = render(Hello); getByText('Hello World'); });
import { render, fireEvent } from '@testing-library/svelte'; import Counter from './Counter.svelte'; test('increments count on click', async () => { const { getByText } = render(Counter); const button = getByText('Increment'); await fireEvent.click(button); getByText('Count: 1'); });
This Svelte component shows a button and a count. Clicking the button adds 1 to the count.
<script> let count = 0; function increment() { count += 1; } </script> <button on:click={increment} aria-label="Increment count">Increment</button> <p>Count: {count}</p>
Testing helps catch errors before users see them.
Use accessible labels like aria-label to improve testing and usability.
Run tests often to keep your app stable as you build.
Testing checks if your Svelte app works as expected.
It simulates user actions like clicks to verify behavior.
Good tests make your app reliable and easier to improve.
import { render, fireEvent } from '@testing-library/svelte';
import Counter from './Counter.svelte';
test('increments count on button click', async () => {
const { getByText } = render(Counter);
const button = getByText('Count: 0');
await fireEvent.click(button);
getByText('Count: 1');
});import { render, fireEvent } from '@testing-library/svelte';
import Button from './Button.svelte';
test('button click triggers event', async () => {
const { getByText } = render(Button);
const button = getByText('Click me');
await fireEvent.click(button);
expect(someMockFunction).toHaveBeenCalled();
});Pass the test and this pattern moves unknown