π§ 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.
