Preact + Vitest
Vite-native test runner, with a sample counter component test. This page shows exactly what pickstack adds to a Preact 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 preact --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 { render } from "preact";
import { act } from "preact/test-utils";
import { describe, expect, it } from "vitest";
import Counter from "./Counter";
describe("Counter", () => {
it("increments when +1 is clicked", () => {
const container = document.createElement("div");
document.body.append(container);
act(() => render(<Counter />, container));
const plus = [...container.querySelectorAll("button")].find(
(b) => b.textContent?.trim() === "+1",
);
act(() => plus?.click());
expect(container.querySelector("p")?.textContent).toBe("1");
render(null, container);
container.remove();
});
});
Changes to existing files
+/// <reference types="vitest/config" /> import preact from "@preact/preset-vite"; import { defineConfig } from "vite"; export default defineConfig({ plugins: [preact()],+ test: {+ environment: "jsdom",+ }, });
Project files
.gitignoreREADME.mdindex.htmlpackage.jsonsrc/App.tsxsrc/components/Counter.test.tsxsrc/components/Counter.tsxsrc/index.csssrc/main.tsxtsconfig.app.jsontsconfig.jsontsconfig.node.jsonvite.config.ts