Set up Vitest testing in any project โ detects type (Cloudflare Workers, React, Node, library), generates vitest.config.ts, test setup, utilities, and a sample test. Covers mocking patterns, coverage config, workspace setup, Jest migration. Use whenever the user mentions adding
Installs just this skill. Get the whole plugin for auto-invocation.
โก How it fires
How this skill gets triggered: by you, by Claude, or both.
Fires itselfClaude auto-loads it when your prompt matches the work.
You can call itInvoke it directly when you want it.
Slash command/vitest
๐๏ธ Context preview
The summary Claude sees to decide when to auto-load this skill.
Set up Vitest testing in any project โ detects type (Cloudflare Workers, React, Node, library), generates vitest.config.ts, test setup, utilities, and a sample test. Covers mocking patterns, coverage config, workspace setup, Jest migration. Use whenever the user mentions adding
๐ Stats
Stars940
Forks96
LanguagePython
LicenseMIT
๐ฆ Ships with jezweb-skills
</> SKILL.md
vitest.SKILL.md
---name: vitest
description: "Set up Vitest testing in any project โ detects type (Cloudflare Workers, React, Node, library), generates vitest.config.ts, test setup, utilities, and a sample test. Covers mocking patterns, coverage config, workspace setup, Jest migration. Use whenever the user mentions adding tests, setting up Vitest, configuring tests, migrating from Jest, fixing testing infrastructure, or asks 'how do I test this'."
license: MIT
---# Vitest Setup
Detect the project type, generate the right Vitest configuration, and produce working test infrastructure. Not a reference card โ this skill creates files.
## Workflow
1. **Detect** โ scan the project to determine type and existing setup
2. **Configure** โ generate vitest.config.ts tailored to the environment
3. **Scaffold** โ create test setup, utilities, and a sample test
4. **Wire up** โ add package.json scripts and TypeScript config
## Step 1: Detect Project Type
Read these files to determine the project:
```
package.json โ dependencies, scripts, type field
tsconfig.json โ paths, compiler options
wrangler.toml โ Cloudflare Workers project
vite.config.ts โ existing Vite setup (extend, don't replace)
vitest.config.ts โ already configured? just fill gaps
jest.config.* โ migration candidate
pnpm remove jest ts-jest @types/jest jest-environment-jsdom babel-jest
```
Use the project's package manager (check for pnpm-lock.yaml, yarn.lock, bun.lockb, or package-lock.json).
## Step 3: Generate vitest.config.ts
### Cloudflare Workers
```typescript
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
export default defineWorkersConfig({
test: {
globals: true,
poolOptions: {
workers: {
wrangler: { configPath: "./wrangler.toml" },
},
},
},
});
```
If the project uses the Cloudflare Vite plugin (`@cloudflare/vite-plugin`), integrate into the existing vite.config.ts instead:
```typescript
/// <reference types="vitest/config" />
import { defineConfig } from "vite";
import { cloudflare } from "@cloudflare/vite-plugin";
export default defineConfig({
plugins: [cloudflare()],
test: {
globals: true,
},
});
```
### React (Vite)
```typescript
/// <reference types="vitest/config" />
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
test: {
globals: true,
environment: "jsdom",
setupFiles: ["./src/test/setup.ts"],
css: true,
},
});
```
If a vite.config.ts already exists, add the `test` block to it rather than creating a new file.
### Node / Hono API
```typescript
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globals: true,
environment: "node",
},
});
```
### With Coverage (add to any config)
```typescript
test: {
// ... existing config
coverage: {
provider: "v8",
reporter: ["text", "html", "lcov"],
exclude: [
"node_modules/",
"**/*.config.*",
"**/*.d.ts",
"**/test/**",
],
},
},
```
## Step 4: Generate Test Setup File
Create `src/test/setup.ts` (React projects only):
```typescript
import "@testing-library/jest-dom/vitest";
```
That single import adds all the custom matchers (toBeInTheDocument, toHaveTextContent, etc.) and registers the Vitest `expect.extend` automatically.
## Step 5: Add TypeScript Config
Add to `tsconfig.json` compilerOptions:
```json
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
```
For projects with multiple tsconfig files (e.g. tsconfig.app.json + tsconfig.node.json), add to the one that covers test files โ usually the root tsconfig.json or create a tsconfig.test.json that extends it.
## Step 6: Add Package.json Scripts
```json
{
"scripts": {
"test": "vitest",
"test:run": "vitest run",
"test:coverage": "vitest run --coverage",
"test:ui": "vitest --ui"
}
}
```
Don't overwrite existing scripts โ merge with what's there.
## Step 7: Generate Sample Test
Write one test file that demonstrates the right patterns for this specific project. Place it next to real source code, not in a separate `__tests__` directory.
### For a Hono API route (e.g. `src/routes/health.ts`):
```typescript
import { describe, it, expect } from "vitest";
import { app } from "../index";
describe("GET /health", () => {
it("returns 200 with status ok", async () => {
const res = await app.request("/health");
expect(res.status).toBe(200);
const body = await res.json();
expect(body).toEqual({ status: "ok" });
});
});
```
### For a React component (e.g. `src/components/Button.tsx`):
```typescript
import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
| `ReferenceError: describe is not defined` | Add `globals: true` to config, or add `types: ["vitest/globals"]` to tsconfig |
| `document is not defined` | Wrong environment โ set `environment: "jsdom"` for React tests |
| `Cannot use import.meta` | Ensure vitest.config uses `.ts` extension and project has `"type": "module"` or Vite handles transforms |
| Workers bindings undefined | Use `@cloudflare/vitest-pool-workers` instead of plain vitest, check wrangler.toml path |
---
## Mocking Reference
These patterns are for writing tests after setup is complete. Include them in the sample test or a `src/test/examples.test.ts` if the user asks for mocking examples.
### Module mocking (vi.mock)
```typescript
import { vi, describe, it, expect } from "vitest";