typescript-test-doubles

Creates Vitest test doubles including mocks, stubs, spies, and module mocks for TypeScript tests.

138|5|Updated Sep 19, 2024
One-click install
npx skills add https://github.com/macalbert/envilder --skill typescript-test-doubles-macalbert
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: typescript-test-doubles
Source: https://github.com/macalbert/envilder/tree/main/.github/skills/typescript-test-doubles
Command: npx skills add https://github.com/macalbert/envilder --skill typescript-test-doubles-macalbert

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing consistent test doubles in TypeScript tests is error-prone: developers mix up vi.fn, vi.mock, and vi.spyOn usage, forget to verify mock interactions, and duplicate test data setup across test files. ## Core Features & Use Cases - Five Test Double Patterns: Covers mocks (vi.fn), stubs (mockResolvedValue/mockReturnValue), spies (vi.spyOn), module mocks (vi.mock), and error simulation (mockRejectedValue) with concrete code examples. - Verification Patterns: Provides assertion recipes for call arguments, call counts, call order, and negative verification using Vitest matchers. - Mother Pattern & Port Test Doubles: Defines factory functions for reusable test data and complete mock objects implementing domain interfaces like ISecretProvider and ILogger. - Use Case: When writing a unit test for a service that depends on a secret provider interface, generate a mock with vi.fn(), stub its resolved values, and verify it was called with the correct SSM path. ## Quick Start Write a Vitest unit test for my TypeScript service class using vi.fn() mocks for its port interfaces and verify the mock interactions.

Frequently Asked Questions about typescript-test-doubles

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

FAQPage Schema
How do I mock a function in Vitest with TypeScript?

Use vi.fn() to create a mock function and assign it to an interface property, such as const mockProvider = { getSecret: vi.fn() }. Configure return values with mockResolvedValue for async functions or mockReturnValue for synchronous ones.

What is the difference between vi.fn, vi.spyOn, and vi.mock in Vitest?

vi.fn() creates a standalone controllable mock function, vi.spyOn() observes calls on a real object while preserving its implementation, and vi.mock() replaces an entire module such as an AWS SDK client. Prefer vi.fn() at the port level over module mocks.

How do I simulate errors in Vitest mocks?

Use mockRejectedValue() on an async mock to simulate failures, for example mockProvider.getSecret.mockRejectedValue(new Error('File not found')). This lets you test error paths and exception handling in the system under test.

How do I verify a mock was called with specific arguments in Vitest?

Use expect(mockFn).toHaveBeenCalledWith(value) for exact arguments, toHaveBeenCalledTimes(n) for call counts, and matchers like expect.stringContaining or expect.objectContaining for partial argument matching.

What is the Mother pattern in unit testing?

The Mother pattern uses factory functions that return default test data objects with an overrides parameter, such as createParsedMapFile(overrides). It keeps test data creation reusable and lets each test customize only the fields it cares about.