A comprehensive example project demonstrating vitest-browser-svelte testing patterns for modern Svelte 5 applications. Built over a weekend as a companion piece to my blog post: Migrating from @testing-library/svelte to vitest-browser-svelte.
Sveltest is both a comprehensive reference project and a CLI tool for
vitest-browser-svelte - the modern testing solution for Svelte
applications. Use it as:
This project demonstrates my opinionated approach to testing with:
The Problem: Server unit tests with heavy mocking can pass while production breaks due to client-server mismatches. Forms send data in one format, servers expect another, and mocked tests miss the disconnect.
My Solution: This project demonstrates a multi-layer testing approach with minimal mocking:
This methodology gives you fast unit test feedback while maintaining confidence that client and server actually work together in production.
Sveltest includes a CLI tool designed specifically for LLMs to access testing patterns and examples. When working with your AI assistant (Claude, ChatGPT, etc.), you can get instant help with testing patterns:
# Your AI assistant can use this to fetch testing patterns
pnpx sveltest list # List all available patterns
pnpx sveltest get button # Get button testing examples
pnpx sveltest search form # Search for form testing patterns
Usage Example: Tell your AI assistant:
"I need to test a login form with validation. Use
pnpx sveltest search formto get the testing patterns."
Your AI can then fetch relevant examples and adapt them to your specific needs. The CLI provides access to all the testing patterns in this repository, making it easy to integrate proven testing approaches into your workflow.
# No installation needed - use with npx/pnpx
pnpx sveltest list # List all patterns
pnpx sveltest get <component> # Get specific examples
pnpx sveltest search <keyword> # Search patterns
Perfect for quickly accessing testing patterns while working with AI assistants.
# Clone the repository
git clone https://github.com/spences10/sveltest.git
cd sveltest
# Install dependencies
pnpm install
# Run the development server
pnpm dev
# Run tests
pnpm test:unit
# Run specific test suites
pnpm test:client # Component tests in browser
pnpm test:server # Server-side tests
pnpm test:ssr # SSR tests
import { render } from 'vitest-browser-svelte';
import { page } from 'vitest/browser';
import { expect, test } from 'vitest';
import Button from './button.svelte';
test('button renders with correct variant', async () => {
render(Button, { variant: 'primary', children: 'Click me' });
const button = page.getByRole('button', { name: 'Click me' });
await expect.element(button).toBeInTheDocument();
await expect.element(button).toHaveClass('btn-primary');
});
import { describe, it, expect } from 'vitest';
import { GET } from './+server.ts';
describe('API Route', () => {
it('should authenticate valid requests', async () => {
const request = new Request('http://localhost/api/secure-data', {
headers: { Authorization: 'Bearer valid-token' },
});
const response = await GET({ request });
expect(response.status).toBe(200);
});
});
import { render } from 'svelte/server';
import { expect, test } from 'vitest';
import Layout from './+layout.svelte';
test('layout renders navigation on server', () => {
const { html } = render(Layout);
expect(html).toContain('<nav');
expect(html).toContain('aria-label="Main navigation"');
});
This is a monorepo project with multiple packages:
sveltest/
โโโ apps/
โ โโโ website/ # Main Sveltest website and examples
โ โโโ src/
โ โโโ lib/
โ โ โโโ components/ # Reusable components with tests
โ โ โ โโโ button.svelte
โ โ โ โโโ button.svelte.test.ts
โ โ โ โโโ button.ssr.test.ts
โ โ โ โโโ input.svelte
โ โ โ โโโ modal.svelte
โ โ โ โโโ card.svelte
โ โ โ โโโ login-form.svelte
โ โ โ โโโ todo-manager.svelte
โ โ โ โโโ calculator.svelte
โ โ โ โโโ nav.svelte
โ โ โโโ utils/ # Utility functions with tests
โ โโโ routes/
โ โโโ api/ # API routes with tests
โ โโโ components/ # Component showcase pages
โ โโโ docs/ # Documentation pages
โ โโโ examples/ # Example pages
โ
โโโ packages/
โโโ cli/ # Sveltest CLI for AI Assistants
โโโ src/
โ โโโ index.ts # CLI entry point
โ โโโ commands/ # List, get, search commands
โโโ README.md # CLI documentation
Each component is co-located with its tests (.svelte.test.ts for
client tests, .ssr.test.ts for SSR tests).
One of the key outcomes of this project was creating comprehensive AI assistant rules and tools that help teams adopt this testing methodology more easily. I'm onboarding my team to use this approach!
The pnpx sveltest CLI works with any AI assistant (Claude, ChatGPT,
Cursor, Windsurf, etc.):
.cursor/rules/testing.mdc).windsurf/rules/testing.md)These rules files contain:
For Teams: Use the CLI for on-demand access, or copy the rule files to your projects to ensure consistent testing patterns across your team. The AI assistants will automatically follow the established patterns when writing or reviewing tests.
I use specific naming conventions to help me identify code I've written versus external libraries:
login-form.svelte.test.ts)submit_button,
error_message) - This helps me instantly recognize my own codedata-testid="submit-button")describe()This project was inspired by my experience migrating from
@testing-library/svelte to vitest-browser-svelte. Read the full
story in my blog post:
Migrating from @testing-library/svelte to vitest-browser-svelte.
You can also check the comprehensive Migration Guide which documents:
This project serves as a reference implementation of my testing methodology. Feel free to:
Keep in mind that the conventions used here are opinionated and reflect my personal coding style preferences.
MIT License - see LICENSE for details.
Sveltest - My comprehensive vitest-browser-svelte testing patterns for modern Svelte applications.