/svelte-5:testing-sveltefrom svelte-5
Fixes and writes Svelte 5 tests using vitest-browser-svelte and Playwright. Helps debug broken/flaky tests, iframe connection issues, strict mode violations, and write unit/SSR/e2e tests.
Ships in svelte-5. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
// browser component test
import { render } from "vitest-browser-svelte";
import { expect, test } from "vitest";
import { page } from "vitest/browser";
// button.svelte: <button onclick={() => count++}>Clicked: {count}</button>
import Button from "./button.svelte";
test("button click increments counter", async () => {
await render(Button);
const button = page.getByRole("button");
// or from the result: const screen = await render(Button); screen.getByRole(...)
await button.click();
await expect.element(button).toHaveTextContent("Clicked: 1"); // exact full text
});
Every test for an interactive component (inputs, autocompletes, buttons, forms) MUST interact with it and assert the result: a render-only test proves only that the component mounts.
if (items.length > 0) that skips the interaction when it failsonChange that passes objects instead of stringspage.getBy*() methods first. A raw querySelector or data-testid needs a one-line comment saying why no accessible locator works (svelte-5:vitest query rule).first(), .nth(), .last() to avoid strict mode violationsFormData and Request, with minimal mockingvi.mock for module boundaries, msw for the network boundary. msw is this rule applied to HTTP: keep the real component and the real fetch, swap only what crosses the wire (svelte-5:msw)vi.mock / vi.hoisted / vi.unmock at the top level only: inside describe, test, or a function they throw in Vitest 5 (was defined outside of the module's top level scope). vi.doMock may go anywhere. Factories, SvelteKit $app/* stubs, component swaps: svelte-5:vitest mocking reference{@attach} wrappers, seeded module state and the clipboard@storybook/addon-vitest): svelte-5:storybook-vitest skill: vitest.config.ts, test.projects entry, pnpm test:storybook (runs the storybook project with --silent), storybookUrl for CI linkspnpm test:unit: the node and browser vitest projects (fast, no dev server needed); pnpm exec vitest run --project browser runs the browser project alone (client in an sv add vitest setup), and pnpm exec vitest run runs every project, storybook included but without --silent (prefer pnpm test:unit plus pnpm test:storybook)package.json lists Vitest 4, upgrade first (svelte-5:vitest, references/vitest-5-upgrade.md)pnpm test, where a project defines it, runs whatever scripts it chains. sv add vitest makes test run test:unit -- --run, and sv add playwright makes it also run test:e2e (in the playwright.config that sv add playwright writes, webServer builds and previews the app first; in another config, its webServer setting decides whether a server must already run). Script names: SETUP.mdpnpm validate: runs test:unit and the lint summary (knip, svelte-check, oxlint, eslint, stylelint) concurrently, then test:storybook (CI pipeline, no e2e; the template in SETUP.md). Read its lint table: the template's lint-summary.ts exits 0 even when a lint task fails (frontend:validate)pnpm lint:tests. One command, 4 linters: oxlint and eslint on the test files matching TEST_GLOBS (src/**/*.test.ts and tests/**/*.test.ts by default), knip and svelte-check (with --tsgo by default) on what knip.tests.jsonc and tsconfig.tests.json cover. Must exit 0. No exceptions. Lint a *.e2e.ts test from sv add playwright, a *.spec.ts test, or a test file outside the TEST_GLOBS patterns with pnpm lint:file. Setup and the cases where a step reports ✓ without checking: frontend:validate and SETUP.mdCannot connect to the iframe…). Forms whose submit handler calls preventDefault() are safe to submit; assert the outcome with await expect.element() (troubleshooting Error 4).svelte infix (foo.svelte.test.ts) lets the test file itself use runes ($state, $effect.root) in a browser project that loads the Svelte plugin; node projects are not covered here. Whether a file runs in the browser or in node is decided by the projects' include globs; .ssr.test.ts (SSR) and server.test.ts (API) are naming conventions for node projectspage from vitest/browser, not @vitest/browser/contextvitest-browser-svelte v3: render is async-only, always await render(...); the returned unmount() is async too. The result is RenderResult (the locator selectors plus container, baseElement, component, wrapper, debug, rerender, unmount and locator, no page field), so get page from vitest/browsergetByText('Save') misses Save draft, and so does getByRole('button', { name: 'Save' }). Use the full text, a RegExp, or { exact: false }. toHaveTextContent is exact too; toMatchTextContent takes partial strings and RegExppage.getByLabelText(...); page.getByLabel does not exist.focus() method: use .click() to focus elements, or locator.element().focus() when a click would trigger the action under test<a href="/page">) navigates the iframe away and fails the file. Hash links and links whose handler calls preventDefault() are safe; for real navigation links assert the href instead (troubleshooting Error 7)click({ force: true }): it skips the actionability checks and passes on elements a user cannot click, while their handler never runs (critical-patterns)await expect.element() for locators; vi.waitFor() or expect.poll() for spies and non-DOM state (both work in browser mode). A fixed setTimeout sleep is only a settle window before a negative assertion, never a wait for a positive onereferences/client-examples.md (opens in a new tab)references/client-server-alignment.md (opens in a new tab)references/core-principles.md (opens in a new tab)references/critical-patterns.md (opens in a new tab)references/foundation-first.md (opens in a new tab)references/server-ssr-examples.md (opens in a new tab)references/troubleshooting.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub fubits1/svelte-skills --plugin svelte-58 skills in this plugin
/code-style-svelteSvelte component code style rules. Auto-invoke when creating .svelte files, editing component structure, transitions or layout reads, or SvelteKit URL state and navigation guards. Covers component docs and Svelte style conventions./composition-svelteUse when composing a Svelte 5 feature from provider/wrapper components, content snippets, reactive state, or third-party DOM bridging (maplibre, leaflet, d3, tippy, popper). Covers overlay wrappers, snippet APIs, and state location./doc-componentDocument a Svelte component using @component JSDoc. Auto-invoke when asked to document a .svelte component. Reads the component, analyzes its interface, and writes a structured @component comment block./mswMocks HTTP with msw 3 in Svelte, Storybook, Vitest and SvelteKit. Use when writing http.get handlers or parameters.msw, upgrading from msw 2, or debugging blank stories, handler order, query strings, onUnhandledFrame or mockServiceWorker.js./storybookStorybook authoring and browser checks for Svelte: MCP instructions, previews, Svelte 4 store wrappers, CSS imports in preview.ts. Use when writing or reviewing .stories.svelte files, moving a CSS import that stories need, or a story renders empty./storybook-vitestSvelte CSF stories as Vitest browser tests via @storybook/addon-vitest: play, tags, decorators, asChild. Use when wiring or debugging them in Vitest/CI, or on 'Vitest failed to find the current suite' or props_invalid_value. Not for React/Vue.Other plugins' skills for the same job