BMAD-METHOD/.claude/rules/vitest-unit-testing-cursorr.../README.md

37 lines
2.2 KiB
Markdown

# Vitest Unit Testing Prompt
A specialized .cursorrules prompt for creating comprehensive unit tests using Vitest with TypeScript support.
## What You Can Build
- **Unit Test Suites**: Focused tests for critical business logic and utility functions
- **Mock-Based Testing**: Tests that properly isolate code from external dependencies using vi.mock
- **Data-Driven Tests**: Tests that validate functionality across multiple data scenarios
- **TypeScript Testing**: Strongly-typed tests with proper interface definitions and type assertions
- **Edge Case Coverage**: Tests that handle edge cases like undefined values and type mismatches
## Benefits
- **Modern Testing Framework**: Leverage Vitest's speed and compatibility with Vite projects
- **ESM-First Approach**: Support for ES modules with top-level await and dynamic imports
- **Proper Dependency Isolation**: Consistent mocking of dependencies before imports
- **Complete TypeScript Support**: Full type safety for tested functions and mocked dependencies
- **Comprehensive Test Coverage**: Focus on business logic with various data scenarios
- **Maintainable Test Structure**: Organized tests with clear arrange-act-assert patterns
## Synopsis
This prompt helps developers create high-quality unit tests with Vitest that focus on critical functionality while ensuring proper mocking of dependencies, comprehensive data scenarios, and edge case coverage.
## Overview of .cursorrules Prompt
The .cursorrules prompt guides developers in creating effective unit tests using Vitest with these key elements:
- **TypeScript Detection**: Automatically detects and adapts to TypeScript usage in the project
- **Dependency Mocking**: Guidelines for properly mocking dependencies before imports using vi.mock
- **Best Practices**: Eight essential practices for unit testing, including critical functionality focus, data scenarios, and edge cases
- **Example Test Patterns**: Provides detailed examples of unit tests in both JavaScript and TypeScript with proper structure
- **Maintainable Approach**: Focus on writing a limited number of high-value tests per file
- **Test Organization**: Structure tests using describe/it blocks with descriptive names
- **AAA Pattern**: Examples using the Arrange-Act-Assert pattern for clear test structure