SvelteKit + Vitest
Vite-native test runner, with a sample counter component test. This page shows exactly what pickstack adds to a SvelteKit project for Vitest: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.
$ npm create pickstack@latest my-app -- --framework sveltekit --modules vitest --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
jsdom | ^31.0.1 | dev |
vitest | ^5.1.1 | dev |
New files
import { flushSync, mount, unmount } from "svelte";
import { describe, expect, it } from "vitest";
import Counter from "./Counter.svelte";
describe("Counter", () => {
it("increments when +1 is clicked", () => {
const component = mount(Counter, { target: document.body });
const plus = [...document.querySelectorAll("button")].find(
(b) => b.textContent?.trim() === "+1",
);
plus?.click();
flushSync();
expect(document.querySelector("p")?.textContent).toBe("1");
unmount(component);
});
});
Changes to existing files
<section class="page"> <h1 class="title">About</h1>+ <p class="text">Built with SvelteKit, Svelte 5, TypeScript, Vitest.</p>− <p class="text">Built with SvelteKit, Svelte 5, TypeScript.</p> </section>
import adapter from "@sveltejs/adapter-static"; import { sveltekit } from "@sveltejs/kit/vite";+import { defineConfig } from "vitest/config";−import { defineConfig } from "vite"; export default defineConfig({ plugins: [⋯ 6 unchanged lines adapter: adapter(), }), ],+ test: {+ expect: { requireAssertions: true },+ projects: [+ {+ extends: "./vite.config.ts",+ resolve: { conditions: ["browser"] },+ test: {+ name: "client",+ environment: "jsdom",+ include: ["src/**/*.svelte.{test,spec}.{js,ts}"],+ },+ },+ {+ extends: "./vite.config.ts",+ test: {+ name: "server",+ environment: "node",+ include: ["src/**/*.{test,spec}.{js,ts}"],+ exclude: ["src/**/*.svelte.{test,spec}.{js,ts}"],+ },+ },+ ],+ }, });
Project files
.gitignoreREADME.mdpackage.jsonsrc/app.d.tssrc/app.htmlsrc/lib/Counter.sveltesrc/lib/Counter.svelte.test.tssrc/lib/index.tssrc/routes/+layout.sveltesrc/routes/+layout.tssrc/routes/+page.sveltesrc/routes/about/+page.sveltesrc/routes/layout.cssstatic/robots.txttsconfig.jsonvite.config.ts