vitest-mock-implementation-persistence

Reset Vitest mock implementations between tests to prevent leakage.

9|2|Updated Mar 5, 2026
One-click install
npx skills add https://github.com/DavidTeju/shared-skills --skill vitest-mock-implementation-persistence
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vitest-mock-implementation-persistence
Source: https://github.com/DavidTeju/shared-skills/tree/main/skills/vitest-mock-implementation-persistence
Command: npx skills add https://github.com/DavidTeju/shared-skills --skill vitest-mock-implementation-persistence

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Flaky Vitest tests caused by lingering mock implementations across test boundaries, leading to false failures and brittle suites.

Core Features & Use Cases

  • Explicitly reset mock implementations between tests to prevent cross-test leakage.
  • Provide guidance for beforeEach patterns to re-setup full mocks after vi.clearAllMocks().
  • Handle scenarios where incomplete mocks cause TypeErrors by restoring a complete mock object.

Quick Start

Place a beforeEach in your test suite to reinitialize mocks with their full implementations.

Frequently Asked Questions about vitest-mock-implementation-persistence

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

FAQPage Schema
Why do my Vitest tests pass in isolation but fail in the full suite?

Vitest tests pass in isolation but fail together due to mock implementations leaking across test boundaries. Lingering mocks cause false failures in the full suite, requiring explicit resets between tests to prevent cross-test contamination.

How do I prevent mock leakage between tests in Vitest?

Prevent mock leakage in Vitest by reinitializing mock implementations within a beforeEach block. Using vi.resetAllMocks followed by appropriate re-setup ensures a clean mock state for each test boundary.

What is the difference between vi.clearAllMocks and vi.resetAllMocks in Vitest?

vi.clearAllMocks clears mock call history while resetting implementations requires vi.resetAllMocks. After resetting, you must re-setup full mock objects in beforeEach to prevent TypeErrors from incomplete mocks.

How do I fix TypeErrors caused by incomplete mocks in Vitest?

Fix TypeErrors from incomplete Vitest mocks by restoring a complete mock object. Reinitialize the full mockImplementation in your beforeEach setup after clearing mocks to ensure all expected properties and methods exist.

When should I use beforeEach to re-setup mocks in Vitest?

Use beforeEach to re-setup mocks in Vitest when refactoring test suites or using vi.mock to stub modules. This pattern ensures test isolation by providing each test with a fresh, complete mock implementation.

Does vi.mock cause cross-test contamination in Vitest suites?

vi.mock can cause cross-test contamination in Vitest if mock implementations are not explicitly reset. Reinitializing mocks in beforeEach or using vi.resetAllMocks with appropriate re-setup prevents lingering stubs from breaking subsequent tests.