vitest-testing-patterns

Establish Vitest testing patterns for TypeScript projects with mocks and spies.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill vitest-testing-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vitest-testing-patterns
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-vitest/skills/vitest-testing-patterns
Command: npx skills add https://github.com/TheBushidoCollective/han --skill vitest-testing-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides Vitest testing patterns, including unit tests, mocks, spies, and browser mode testing.

Core Features & Use Cases

  • Unit & Integration Tests: Structured testing approach.
  • Mocks & Spies: Isolation of units under test.
  • Browser Mode: Testing in a real browser-like environment.

Quick Start

Write a simple Vitest unit test with a mock dependency.

Frequently Asked Questions about vitest-testing-patterns

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

FAQPage Schema
How do I write unit tests with Vitest in TypeScript?

Vitest unit tests in TypeScript use describe and test blocks to organize assertions. Write test cases by importing your code, setting up inputs, executing the function, and asserting expected outputs. Vitest runs tests in parallel and provides built-in matchers for common assertions.

What's the best way to mock dependencies in Vitest tests?

Mock dependencies in Vitest using vi.mock() to replace modules or vi.fn() to create mock functions. Mocks isolate the unit under test by replacing external calls with controlled stubs, letting you verify interactions and control return values without executing real code.

How do I use spies to track function calls in Vitest?

Spies in Vitest wrap existing functions with vi.spyOn() to monitor calls without replacing implementation. Track how many times a function was called, what arguments it received, and what it returned—useful for verifying side effects and integration points.

Can I test TypeScript code in a browser-like environment with Vitest?

Vitest browser mode simulates a real browser environment for testing DOM interactions and browser APIs. Configure browser mode in your Vitest setup to test code that depends on window, document, or other browser globals without a full browser instance.

What configuration do I need to set up Vitest for TypeScript projects?

Configure Vitest by creating a vitest.config.ts file specifying test environment, include/exclude patterns, and module resolution. TypeScript projects typically need environment set to 'node' or 'jsdom', resolver configuration, and transpiler setup to handle .ts files.

When should I use mocks versus spies in my Vitest tests?

Use mocks when you need complete replacement and control over a dependency's behavior; use spies when you want to observe an existing function without changing it. Mocks suit external services; spies suit internal functions where you verify side effects without stubbing the implementation.