vitest-patterns

Establish Vitest test conventions covering imports, structure, and assertions.

9.6k|901|Updated Dec 17, 2021
One-click install
npx skills add https://github.com/bluesky-social/atproto --skill vitest-patterns-bluesky-social
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vitest-patterns
Source: https://github.com/bluesky-social/atproto/tree/main/.claude/skills/vitest-patterns
Command: npx skills add https://github.com/bluesky-social/atproto --skill vitest-patterns-bluesky-social

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides clear guidelines and best practices for writing effective and maintainable Vitest tests, ensuring consistency and quality across the project's test suite.

Core Features & Use Cases

  • Standardized Imports: Enforces consistent import statements for Vitest utilities.
  • Structured Test Suites: Guides the organization of tests using describe blocks for clarity.
  • Parameterized Testing: Promotes the use of for...of loops for efficient test case iteration.
  • Assertion Best Practices: Details preferred methods for type narrowing and error checking.
  • Use Case: When developing a new feature, use this Skill to ensure all associated tests follow project conventions, making them easier to read, debug, and maintain.

Quick Start

Follow the patterns in this skill when writing new Vitest tests for the project.

Frequently Asked Questions about vitest-patterns

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

FAQPage Schema
How do I structure parameterized tests in Vitest?

Structure parameterized tests in Vitest by using `for...of` loops to iterate over test cases, which provides a clean and readable alternative to traditional test.each syntax for efficient test case iteration.

What is the best way to handle promise rejections in Vitest assertions?

Handle promise rejections in Vitest assertions by using the `rejects.toSatisfy` method, which allows you to assert that an asynchronous operation rejects with a specific error condition while ensuring proper type narrowing.

How do I use TypeScript with Vitest to expect intentional errors?

Use the `// @ts-expect-error` directive in your Vitest test files to intentionally suppress TypeScript errors, ensuring that your tests correctly validate invalid type assertions without breaking the build.

What's the recommended pattern for mocking functions in a Vitest test suite?

The recommended pattern for mocking functions in a Vitest test suite is using `vi.fn` to create mock functions, allowing you to track calls, define return values, and verify interaction behaviors within your test fixtures.

How do I organize a new Vitest test suite for better maintainability?

Organize a new Vitest test suite using `describe` blocks to group related tests logically, enforcing standardized import statements and consistent test fixture definitions to ensure long-term maintainability and readability.

Can I globally stub methods across multiple Vitest tests?

Yes, you can globally stub methods across multiple Vitest tests by establishing global stubbing conventions within your project, ensuring consistent mock behavior and setup throughout your entire test suite.