Runs and debugs Vitest unit/component tests for TypeScript React/Next.js projects, including mocks, setup files, and coverage configuration.
How this skill is triggered — by the user, by Claude, or both
Slash command
/paulrberg-agent-skills:vitestThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
**Typical setup:** Vitest v4 with jsdom environment; globals enabled (`describe`, `test`, `expect`, `vi`); path aliases configured per project. In a workspace monorepo, read [references/monorepo-testing.md](references/monorepo-testing.md) for shared-vs-app test strategies, path aliases, and organization.
Typical setup: Vitest v4 with jsdom environment; globals enabled (describe, test, expect, vi); path aliases configured per project. In a workspace monorepo, read references/monorepo-testing.md for shared-vs-app test strategies, path aliases, and organization.
# Run all unit tests
nlx vitest run
# Run tests matching pattern
nlx vitest run tokens
# Run specific test file
nlx vitest run src/utils/format.test.ts
# Run tests with matching name
nlx vitest run -t "adds token"
# Watch mode
nlx vitest
File naming: *.test.ts or *.test.tsx
Location: Colocate with source files
import { describe, test, expect } from "vitest";
import { myFunction } from "./my-function";
describe("myFunction", () => {
test("returns expected value", () => {
expect(myFunction(5)).toBe(10);
});
});
Use visual separators and descriptive blocks:
describe("TokenStore", () => {
/* ----------------------------------------------------------------
* Setup
* ------------------------------------------------------------- */
const validToken = { address: "0x123", symbol: "TEST" };
afterEach(() => {
// Reset state between tests
useTokensStore.getState().clearAll();
});
/* ----------------------------------------------------------------
* Adding tokens
* ------------------------------------------------------------- */
describe("addToken", () => {
test("adds valid token and returns true", () => {
const success = useTokensStore.getState().addToken(validToken);
expect(success).toBe(true);
});
});
});
Always reset state in afterEach():
import { afterEach } from "vitest";
afterEach(() => {
// Reset mocks
vi.clearAllMocks();
// Reset environment
process.env.NODE_ENV = originalEnv;
// Reset stores
});
Global mocks and configuration live in a setup file (e.g., tests/setup.ts):
import { vi } from "vitest";
// Mock logger for all tests
vi.mock("@/utils/logger", () => ({
createLogger: vi.fn(() => ({
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn()
}))
}));
For component tests, complex mocking, async delays/callbacks, snapshot testing, type testing, custom matchers, and parameterized tests, read references/testing-patterns.md when the basics above aren't enough.
For function mocks (vi.fn, spyOn), module mocks (vi.mock, vi.doMock), and timer mocks (vi.useFakeTimers), see references/mocking.md.
Focus on these signals:
For known failure modes and how to recover (timeouts, async assertions, mock not called, snapshot drift, etc.), see references/troubleshooting.md.
nlx vitest --reporter=verbose # Detailed output
nlx vitest --ui # Visual debugging interface
nlx vitest --coverage # See what's tested
nlx vitest --inspect # Node debugger
nlx vitest --run # Disable watch mode
To add coverage:
// vitest.config.ts
export default defineConfig({
test: {
coverage: {
provider: "v8",
reporter: ["text", "html", "json"],
exclude: ["**/*.test.ts", "**/__mocks__/**", "**/node_modules/**"]
}
}
});
Run with: nlx vitest --coverage
Example config: vitest.config.ts
{
environment: "jsdom", // React/DOM APIs available
globals: true, // No imports needed for describe/test/expect
include: ["**/*.test.{js,ts,tsx}"],
exclude: ["**/node_modules/**", "**/e2e/**"],
setupFiles: ["./tests/setup.ts"],
alias: {
"@": "./src",
// Add your project's path aliases
},
}
npx claudepluginhub joshuarweaver/cascade-code-general-misc-1 --plugin paulrberg-agent-skillsGuides Vitest testing for TypeScript/JavaScript projects: installation, config, running tests with watch/UI/coverage, assertions, and mocking.
Generates Vitest tests with Vite-native speed, Jest-compatible API, ESM support, and HMR. Activates on mentions of Vitest, vi.mock, vi.fn, or vitest.config.
Vitest testing framework conventions and practices. Invoke whenever task involves any interaction with Vitest — writing tests, configuring vitest.config.ts, mocking modules, debugging test failures, snapshots, coverage, or migrating from Jest.