Vitest

Enforce correct mocking, async assertions, and test organization in Vitest.

1|Updated May 2, 2026
One-click install
npx skills add https://github.com/Levironexe/architect --skill vitest-levironexe
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Vitest
Source: https://github.com/Levironexe/architect/tree/main/skills/patterns/vitest-testing
Command: npx skills add https://github.com/Levironexe/architect --skill vitest-levironexe

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Vitest projects often end up with flaky or misleading tests that don’t actually validate behavior, fail to prevent coverage regressions, or accidentally use the wrong mocking APIs, making refactors risky.

Core Features & Use Cases

  • Correct Vitest mocking patterns: Use vi.mock() for module replacement, vi.fn() for controllable mocks, and vi.spyOn() for spying without mixing toolchains.
  • Clean, deterministic async assertions: Await expect(...).rejects/resolves to ensure failures are never silently ignored.
  • Safe test structure & coverage guardrails: Mirror source structure for unit tests, isolate integration tests from unit behavior, and enforce thresholds in vitest.config.ts.
  • Use case: You’re refactoring a service layer and want confidence that behavior stays stable while controlling dependencies; apply the skill to set up src/tests unit tests, tests/integration for real dependencies, and coverage thresholds to block regressions.

Quick Start

Configure your test files to mirror your source under src/tests, replace dependencies with vi.mock() at the top level, and set coverage thresholds in vitest.config.ts.

Frequently Asked Questions about Vitest

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

FAQPage Schema
How do I write reliable Vitest unit and integration tests that don't fail during refactoring?

Reliable Vitest unit and integration tests require behavior-focused assertions with deterministic state. You achieve this by mirroring source structure for unit tests, isolating integration setups, and using correct mocking patterns to ensure refactors remain risk-free.

What is the correct way to use vi.mock and vi.fn for module replacement in Vitest?

Correct Vitest mocking uses vi.mock() for module replacement, vi.fn() for controllable mocks, and vi.spyOn() for spying. Maintaining this strict separation prevents mixing toolchains and avoids flaky, misleading tests that fail to validate actual behavior.

Why are my async Vitest assertions silently passing when they should fail?

Async Vitest assertions silently pass when you fail to await expect(...).rejects or expect(...).resolves. Properly awaiting these async assertions ensures that test failures are never silently ignored during your JavaScript or TypeScript test runs.

How do I enforce coverage thresholds in vitest.config.ts to prevent regressions?

Enforce coverage thresholds in vitest.config.ts to establish guardrails that block coverage regressions. Configuring these thresholds ensures your test suite maintains strict behavior validation standards across your JavaScript and TypeScript codebase.

Can I use Jest mocking APIs interchangeably with Vitest for testing JavaScript codebases?

Jest mocking APIs should not be used with Vitest. This skill actively enforces anti-pattern avoidance, specifically targeting Jest API misuse, and standardizes correct vi.mock, vi.fn, and vi.clearAllMocks hygiene for deterministic test state.

What's the best way to structure integration tests separately from unit tests in Vitest?

The best way to structure integration tests is to isolate them from unit behavior by placing them in tests/integration, while unit tests mirror the source under src/__tests__. This organization ensures clean, deterministic state for both test types.