Overview
KAIU Natural Living uses Vitest as the testing framework with React Testing Library for component tests. Tests run in a jsdom environment to simulate browser behavior.Test Configuration
The test setup is defined invitest.config.ts:
- Environment: jsdom (browser simulation)
- Globals: Enabled (no need to import
describe,test,expect) - Setup Files:
src/test/setup.tsruns before all tests - File Pattern:
*.test.ts,*.test.tsx,*.spec.ts,*.spec.tsx - Path Alias:
@resolves to./src
Running Tests
Run All Tests
Watch Mode
Run Specific Tests
Run Tests Matching Pattern
Coverage Report
Test Structure
Component Tests
Place test files next to components:API/Backend Tests
Utility Function Tests
Testing Library Setup
Thesrc/test/setup.ts file configures the test environment:
Common Testing Patterns
Testing User Interactions
Testing Async Components
Mocking API Calls
Testing Context Providers
Testing Forms
Database Testing
Setup Test Database
Use a separate test database:Seed Before Tests
Snapshot Testing
Testing Best Practices
- Test User Behavior - Focus on how users interact with the app
- Avoid Implementation Details - Don’t test internal state or props
- Use Semantic Queries - Prefer
getByRole,getByLabelTextovergetByTestId - Keep Tests Independent - Each test should run in isolation
- Mock External Dependencies - API calls, third-party libraries
- Clean Up - Clear database, reset mocks after each test
Coverage Goals
Aim for:- Components: 80%+ coverage
- Business Logic: 90%+ coverage
- Utilities: 100% coverage
Continuous Integration
Add to CI pipeline:Troubleshooting
”Cannot find module”
Check path aliases invitest.config.ts match tsconfig.json.
”ReferenceError: window is not defined”
Ensure test environment is set tojsdom:
Tests Timeout
Increase timeout for slow tests:Next Steps
- Review Debugging Tips
- Learn about Code Style Guide
- Explore Contributing Guidelines