|
| 1 | +## Complete Examples |
| 2 | + |
| 3 | +### Example 1: Client-Side Component Test |
| 4 | + |
| 5 | +Real browser testing with user interactions: |
| 6 | + |
| 7 | +```typescript |
| 8 | +// button.svelte.test.ts |
| 9 | +import { render } from 'vitest-browser-svelte'; |
| 10 | +import { test, expect, describe } from 'vitest'; |
| 11 | +import { userEvent } from '@vitest/browser/context'; |
| 12 | +import Button from './button.svelte'; |
| 13 | + |
| 14 | +describe('Button Component', () => { |
| 15 | + test('increments counter on click', async () => { |
| 16 | + const { page } = render(Button, { props: { label: 'Click me' } }); |
| 17 | + |
| 18 | + const button = page.getByRole('button', { name: /click me/i }); |
| 19 | + |
| 20 | + await userEvent.click(button); |
| 21 | + await expect.element(button).toHaveTextContent('Clicked: 1'); |
| 22 | + |
| 23 | + await userEvent.click(button); |
| 24 | + await expect.element(button).toHaveTextContent('Clicked: 2'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('supports keyboard interaction', async () => { |
| 28 | + render(Button, { props: { label: 'Press me' } }); |
| 29 | + |
| 30 | + const button = page.getByRole('button', { name: /press me/i }); |
| 31 | + await button.focus(); |
| 32 | + await userEvent.keyboard('{Enter}'); |
| 33 | + |
| 34 | + await expect.element(button).toHaveTextContent('Clicked: 1'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('handles multiple buttons with .first()', async () => { |
| 38 | + render(ButtonGroup); // Renders multiple buttons |
| 39 | + |
| 40 | + // Handle multiple buttons explicitly |
| 41 | + const firstButton = page.getByRole('button').first(); |
| 42 | + const secondButton = page.getByRole('button').nth(1); |
| 43 | + |
| 44 | + await firstButton.click(); |
| 45 | + await expect.element(firstButton).toHaveTextContent('Clicked: 1'); |
| 46 | + |
| 47 | + await secondButton.click(); |
| 48 | + await expect |
| 49 | + .element(secondButton) |
| 50 | + .toHaveTextContent('Clicked: 1'); |
| 51 | + }); |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +### Example 2: Testing Svelte 5 Runes |
| 56 | + |
| 57 | +```typescript |
| 58 | +// counter.svelte.test.ts |
| 59 | +import { render } from 'vitest-browser-svelte'; |
| 60 | +import { test, expect } from 'vitest'; |
| 61 | +import { untrack, flushSync } from 'svelte'; |
| 62 | +import Counter from './counter.svelte'; |
| 63 | + |
| 64 | +test('$state and $derived reactivity', async () => { |
| 65 | + const { component } = render(Counter); |
| 66 | + |
| 67 | + // Access $state value directly |
| 68 | + expect(component.count).toBe(0); |
| 69 | + |
| 70 | + // Update state |
| 71 | + component.increment(); |
| 72 | + |
| 73 | + // Force synchronous update |
| 74 | + flushSync(() => {}); |
| 75 | + |
| 76 | + // Access $derived value with untrack |
| 77 | + const doubled = untrack(() => component.doubled); |
| 78 | + expect(doubled).toBe(2); |
| 79 | +}); |
| 80 | + |
| 81 | +test('form validation lifecycle', async () => { |
| 82 | + const { component } = render(FormComponent); |
| 83 | + |
| 84 | + // Initially valid (no validation run yet) |
| 85 | + expect(untrack(() => component.isFormValid())).toBe(true); |
| 86 | + |
| 87 | + // Trigger validation |
| 88 | + component.validateAllFields(); |
| 89 | + |
| 90 | + // Now invalid (empty required fields) |
| 91 | + expect(untrack(() => component.isFormValid())).toBe(false); |
| 92 | + |
| 93 | + // Fix validation errors |
| 94 | + component.email.value = 'test@example.com'; |
| 95 | + component.validateAllFields(); |
| 96 | + |
| 97 | + // Valid again |
| 98 | + expect(untrack(() => component.isFormValid())).toBe(true); |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +### Example 3: Server-Side API Test |
| 103 | + |
| 104 | +Test with real FormData/Request objects: |
| 105 | + |
| 106 | +```typescript |
| 107 | +// api/users/server.test.ts |
| 108 | +import { test, expect, describe, vi } from 'vitest'; |
| 109 | +import { POST } from './+server'; |
| 110 | +import * as database from '$lib/server/database'; |
| 111 | + |
| 112 | +vi.mock('$lib/server/database'); |
| 113 | + |
| 114 | +describe('POST /api/users', () => { |
| 115 | + test('creates user with valid data', async () => { |
| 116 | + // Mock only external services |
| 117 | + vi.mocked(database.createUser).mockResolvedValue({ |
| 118 | + id: '123', |
| 119 | + email: 'user@example.com', |
| 120 | + }); |
| 121 | + |
| 122 | + // Use real FormData |
| 123 | + const formData = new FormData(); |
| 124 | + formData.append('email', 'user@example.com'); |
| 125 | + formData.append('password', 'securepass123'); |
| 126 | + |
| 127 | + // Use real Request object |
| 128 | + const request = new Request('http://localhost/api/users', { |
| 129 | + method: 'POST', |
| 130 | + body: formData, |
| 131 | + }); |
| 132 | + |
| 133 | + const response = await POST({ request }); |
| 134 | + const data = await response.json(); |
| 135 | + |
| 136 | + expect(response.status).toBe(201); |
| 137 | + expect(data.email).toBe('user@example.com'); |
| 138 | + expect(database.createUser).toHaveBeenCalledWith({ |
| 139 | + email: 'user@example.com', |
| 140 | + password: 'securepass123', |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + test('rejects invalid email format', async () => { |
| 145 | + const formData = new FormData(); |
| 146 | + formData.append('email', 'invalid-email'); |
| 147 | + formData.append('password', 'pass123'); |
| 148 | + |
| 149 | + const request = new Request('http://localhost/api/users', { |
| 150 | + method: 'POST', |
| 151 | + body: formData, |
| 152 | + }); |
| 153 | + |
| 154 | + const response = await POST({ request }); |
| 155 | + const data = await response.json(); |
| 156 | + |
| 157 | + expect(response.status).toBe(400); |
| 158 | + expect(data.errors.email).toBeDefined(); |
| 159 | + expect(database.createUser).not.toHaveBeenCalled(); |
| 160 | + }); |
| 161 | + |
| 162 | + test('handles missing required fields', async () => { |
| 163 | + const formData = new FormData(); |
| 164 | + // Missing email and password |
| 165 | + |
| 166 | + const request = new Request('http://localhost/api/users', { |
| 167 | + method: 'POST', |
| 168 | + body: formData, |
| 169 | + }); |
| 170 | + |
| 171 | + const response = await POST({ request }); |
| 172 | + const data = await response.json(); |
| 173 | + |
| 174 | + expect(response.status).toBe(400); |
| 175 | + expect(data.errors.email).toBeDefined(); |
| 176 | + expect(data.errors.password).toBeDefined(); |
| 177 | + }); |
| 178 | +}); |
| 179 | +``` |
| 180 | + |
| 181 | +--- |
0 commit comments