/vitest-4:writing-vitest-testsfrom vitest-4
Write Vitest tests with describe/test blocks, expect assertions, vi mocking, async testing, and parameterized tests. Use when creating or modifying test files.
Ships in vitest-4. 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.
This skill teaches test structure and patterns for writing effective Vitest tests.
import { test, expect } from 'vitest';
test('adds numbers correctly', () => {
expect(1 + 2).toBe(3);
});
import { describe, test, expect } from 'vitest';
describe('Math utilities', () => {
test('adds numbers', () => {
expect(1 + 2).toBe(3);
});
test('subtracts numbers', () => {
expect(5 - 2).toBe(3);
});
});
For complete test organization patterns, see references/test-organization.md
expect(value).toBe(expected);
expect(value).toEqual(expected);
expect(value).toBeTruthy();
expect(value).toBeFalsy();
expect(array).toContain(item);
expect(array).toHaveLength(3);
expect(object).toHaveProperty('key');
expect(object).toMatchObject({ key: 'value' });
expect(string).toMatch(/pattern/);
expect(number).toBeGreaterThan(3);
expect(number).toBeCloseTo(0.3, 5);
For complete assertion API, see references/assertions.md
import { vi, test, expect } from 'vitest';
test('calls callback', () => {
const callback = vi.fn();
callback('arg1', 'arg2');
expect(callback).toHaveBeenCalledWith('arg1', 'arg2');
expect(callback).toHaveBeenCalledTimes(1);
});
const mockFn = vi.fn();
mockFn.mockReturnValue('result');
expect(mockFn()).toBe('result');
const mockFn = vi.fn();
mockFn.mockImplementation((a, b) => a + b);
expect(mockFn(1, 2)).toBe(3);
For testing runtime type validation with Zod, use the using-runtime-checks skill for patterns on schema validation, error handling, and type inference.
const mockFn = vi.fn();
mockFn.mockResolvedValue('data');
const result = await mockFn();
expect(result).toBe('data');
For testing code that validates external data with Zod, use the validating-external-data skill for patterns on API response validation, error handling, and type-safe validation flows.
For complete mocking patterns, see references/mocking.md
vi.mock('./module', () => ({
method: vi.fn(),
constant: 'mocked',
}));
vi.mock(import('./module'), async (importOriginal) => {
const mod = await importOriginal();
return {
...mod,
specificMethod: vi.fn(),
};
});
For testing Zod validation of database inputs in Vitest, use the validating-query-inputs skill for patterns on Zod schema validation before Prisma operations.
test('fetches data', async () => {
const data = await fetchData();
expect(data).toBeDefined();
});
test('promise resolves', async () => {
await expect(fetchData()).resolves.toBe('data');
});
test('promise rejects', async () => {
await expect(fetchBadData()).rejects.toThrow('error');
});
test.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3],
])('add(%i, %i) -> %i', (a, b, expected) => {
expect(a + b).toBe(expected);
});
test.each([
{ a: 1, b: 1, expected: 2 },
{ a: 1, b: 2, expected: 3 },
{ a: 2, b: 1, expected: 3 },
])('$a + $b should equal $expected', ({ a, b, expected }) => {
expect(a + b).toBe(expected);
});
For testing Zod schema validation in Vitest, use the testing-zod-schemas skill for patterns testing validation logic, error messages, and transformations.
import { describe, test, beforeEach, afterEach, beforeAll, afterAll } from 'vitest';
describe('User service', () => {
beforeAll(() => {
});
afterAll(() => {
});
beforeEach(() => {
});
afterEach(() => {
});
test('creates user', () => {
});
});
test.skip('skipped test', () => {});
test.only('run only this test', () => {});
test.todo('implement later');
test.fails('should fail', () => {});
test.concurrent('runs in parallel', async () => {});
For testing Server Actions in isolation with Vitest, use the testing-server-actions skill for patterns on mocking authentication, database, and form data.
For testing form validation in React components with Vitest, use the validating-forms skill for patterns on form validation, useActionState, and error handling.
import { vi } from 'vitest';
vi.useFakeTimers();
const callback = vi.fn();
setTimeout(callback, 1000);
vi.advanceTimersByTime(1000);
expect(callback).toHaveBeenCalled();
vi.useRealTimers();
vi.setSystemTime(new Date(2022, 0, 1));
expect(Date.now()).toBe(new Date(2022, 0, 1).getTime());
vi.useRealTimers();
test('matches snapshot', () => {
const result = generateOutput();
expect(result).toMatchSnapshot();
});
Update snapshots: vitest -u
test('throws error', () => {
expect(() => {
throw new Error('failed');
}).toThrow('failed');
});
test('async throws', async () => {
await expect(async () => {
throw new Error('failed');
}).rejects.toThrow('failed');
});
import { test as baseTest } from 'vitest';
const test = baseTest.extend({
todos: async ({}, use) => {
const todos = [];
await use(todos);
todos.length = 0;
},
});
test('adds item', ({ todos }) => {
todos.push('item');
expect(todos).toHaveLength(1);
});
For testing custom React hooks with Vitest, use the testing-hooks skill for renderHook patterns and hook-specific assertions.
import { afterEach, vi } from 'vitest';
afterEach(() => {
vi.restoreAllMocks();
});
export default defineConfig({
test: {
restoreMocks: true,
clearMocks: true,
},
});
For testing React 19 components with Vitest browser mode, use the testing-components skill for patterns using @testing-library/react and handling useActionState, Server Actions, and React 19 hooks.
async/awaitafterEach or config optionsFor detailed testing patterns:
For configuration options, see @vitest-4/skills/configuring-vitest-4
For browser testing, see @vitest-4/skills/using-browser-mode
For complete API reference, see @vitest-4/knowledge/vitest-4-comprehensive.md
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub djankies/claude-configs --plugin vitest-45 skills in this plugin
/configuring-vitest-4Configure Vitest 4.x with correct pool architecture, coverage settings, and multi-project setup. Use when creating or modifying vitest.config files or setting up test infrastructure./migrating-to-vitest-4Migrate from Vitest 2.x/3.x to 4.x with pool options, coverage config, workspace to projects, and browser mode updates. Use when upgrading Vitest versions or encountering deprecated patterns./reviewing-vitest-configReview Vitest configuration for deprecated patterns and best practices. Use when reviewing test configuration or vitest setup./using-browser-modeSet up Vitest browser mode with Playwright or WebDriverIO providers, use page and userEvent APIs, test components. Use when testing browser-specific code or UI components.