writing-vitest-tests

Generate Vitest tests with describe/test blocks, assertions, and mocking.

Updated Nov 21, 2025
One-click install
npx skills add https://github.com/djankies/claude-configs --skill writing-vitest-tests
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: writing-vitest-tests
Source: https://github.com/djankies/claude-configs/tree/main/vitest-4/skills/writing-vitest-tests
Command: npx skills add https://github.com/djankies/claude-configs --skill writing-vitest-tests

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Teaches test structure and patterns for writing Vitest tests.

Core Features & Use Cases

  • Test organization: describe/test blocks, lifecycle hooks.
  • Mocking: vi.fn(), mocks, and assertions.
  • Use Case: structure comprehensive unit tests for a library.

Quick Start

Create a simple test file with describe, test, and mock examples.

Frequently Asked Questions about writing-vitest-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write Vitest tests with describe and test blocks?

Vitest tests use describe blocks to organize related test cases and test blocks for individual assertions. Structure your test file by importing Vitest, wrapping related tests in describe, and using test or it to define each case with expect assertions.

How do I mock functions in Vitest?

Vitest provides vi.fn() to create mock functions and vi.mock() to mock modules. Use vi.fn() to track calls and return values, then assert on mock behavior with expect(mockFn).toHaveBeenCalled() or similar matchers.

Can I test async code with Vitest?

Yes, Vitest supports async testing by using async/await in test functions or returning promises. Mark your test callback as async and await promises, or return the promise directly for Vitest to handle completion.

What's the best way to organize unit tests for a library?

Organize tests using describe blocks by module or feature, apply setup and teardown with beforeEach and afterEach hooks, and use parameterized tests for data-driven cases to reduce duplication and improve readability.

Does Vitest work with both JavaScript and TypeScript projects?

Vitest supports both JavaScript and TypeScript natively. Write tests in either language using the same APIs—describe, test, expect, and vi mocking—with full type support in TypeScript test files.

What assertions does Vitest provide for testing?

Vitest includes expect assertions for value matching (toBe, toEqual), truthiness (toBeTruthy), exceptions (toThrow), and mock calls (toHaveBeenCalled). Combine matchers for precise test validation across unit and integration scenarios.