🧠 Rules by Layer
1. Domain Layer (src/domain)
- •Type: Pure Unit Tests.
- •Mocks: ❌ NONE.
- •Focus: Test business logic, Zod schema validations (
.parse()), and utility functions. - •Example:
expect(productSchema.parse(invalidData)).toThrow().
2. Application Layer (src/application)
- •Type: Unit Tests.
- •Mocks: ✅ Mock Infrastructure Repositories and Zustand Stores.
- •Focus:
- •Test
queryOptionsgeneration keys. - •Test complex hooks logic (use
renderHook). - •Test Zustand actions (state mutations).
- •Test
- •Tooling:
vi.spyOn,mockZustand.
3. Infrastructure Layer (src/infrastructure)
- •Type: Integration Tests.
- •Mocks: ✅ Mock HTTP Network (use
mswornock). - •Focus:
- •Verify Repositories call the correct endpoints.
- •Verify Mappers correctly transform API DTOs -> Domain Entities.
- •Verify DTOs match expected API shapes.
4. Presentation Layer (src/presentation)
- •Type: Component Integration Tests.
- •Mocks: ✅ Mock Application Layer Hooks (e.g.,
useProductDetail). - •Forbidden: ❌ NEVER mock
axios,fetch, oruseQuerydirectly in components. - •Focus:
- •Test UI rendering based on data states (Success, Empty).
- •Test User Interactions (Click, Type) firing handlers.
- •Test Accessibility (
aria-labels).
- •Tooling:
render,screen,userEvent.
📝 Conventions
- •Naming:
Filename.test.tsxorFilename.spec.ts. - •Location: Co-located next to the source file.
- •Async: Always use
await screen.findBy*for async elements, neverwait().
Keywords
vitest, react-testing-library, rtl, user-event, msw, unit-testing, integration-testing, mocks.