Overview
AniDev uses Vitest as its testing framework, providing fast, Vite-native testing for both unit and integration tests. The testing strategy focuses on critical business logic, API endpoints, and service layers.Testing Setup
Running Tests
Configuration
Vitest configuration is defined inpackage.json:
package.json
astro.config.mjs, including:
- TypeScript path aliases
- Module resolution
- Environment variables
Test Structure
Test File Organization
{feature}.test.ts or {feature}.spec.ts
Basic Test Example
src/domains/shared/test/metadata-service.test.ts
Testing Patterns
1. Service Layer Testing
Test business logic and error handling:2. Repository Layer Testing
Test database interactions:anime-repository.test.ts
3. Controller Testing
Test request validation and response formatting:anime-controller.test.ts
4. Error Handling Testing
error-handling.test.ts
5. Integration Testing
Test API endpoints end-to-end:api-animes.test.ts
Test Utilities
Mock Factories
test/factories/anime.factory.ts
Test Helpers
test/helpers/api.helper.ts
Best Practices
Test Isolation
Each test should be independent. Use
beforeEach to reset state and mocks.AAA Pattern
Structure tests with Arrange, Act, Assert for clarity.
Mock External Services
Always mock Supabase, Redis, and external APIs in unit tests.
Test Edge Cases
Cover error conditions, null values, and boundary cases.
Use Type Safety
Leverage TypeScript for type-safe test expectations.
Fast Tests
Keep unit tests fast (under 100ms) by mocking I/O operations.
Coverage Goals
Priority Testing Areas
- Business Logic (Services) - 80%+ coverage
- Data Access (Repositories) - 70%+ coverage
- Request Validation (Controllers) - 80%+ coverage
- Error Handling - 100% coverage
- Utilities - 90%+ coverage
Running Coverage Reports
- Line coverage
- Branch coverage
- Function coverage
- Statement coverage
Continuous Integration
Tests run automatically on:- Every pull request
- Commits to main branch
- Pre-deployment checks
.github/workflows/test.yml
Tests are the safety net for refactoring and adding new features. Write tests for critical paths, edge cases, and any code that handles user data or authentication.
