Overview
Gima uses a modern testing stack:- Test Runner: Vitest - Fast, ESM-native, Vite-powered
- React Testing: @testing-library/react
- API Mocking: MSW (Mock Service Worker)
- Coverage: Vitest Coverage (v8 provider)
Quick Start
Running Tests
package.json:14-16
Test Configuration
Vitest is configured invitest.config.ts:1-29:
Test Setup
Global Setup File
Location:tests/config/vitest.setup.ts
The setup file configures:
-
Testing Library matchers
-
Fake timers (for strict timing control)
-
Global mocks:
crypto.randomUUID→ predictable UUIDsnavigator.mediaDevices→ for voice testslocalStorage→ for persistence testswindow.matchMedia→ for theme tests
-
Lifecycle hooks:
tests/config/vitest.setup.ts:1-64
Writing Tests
Unit Tests
Testing Configuration Validation
Example:app/config/__tests__/env.test.ts
app/config/__tests__/env.test.ts:1-141
Integration Tests
Testing API Routes
Example:app/api/chat/__tests__/route.test.ts
app/api/chat/__tests__/route.test.ts:1-107
Component Tests
Testing React Hooks
Example: Testing a custom hookTesting React Components
Mocking
Mocking Services
Mocking API Routes with MSW
Setup:tests/setup.msw.ts
Mocking Environment Variables
Performance Tests
Testing Chat Performance
Example:tests/performance/chat-performance.test.ts
Coverage
Viewing Coverage Reports
- Text output: In terminal
- HTML report:
coverage/index.html - JSON data:
coverage/coverage-final.json
Coverage Exclusions
The following are excluded from coverage (seevitest.config.ts:14-21):
node_modules/test/directories- Config files (
*.config.*) .next/build outputapp/layout.tsx(Next.js boilerplate)app/globals.css
Best Practices
Test Behavior, Not Implementation
Focus on what components do, not how they do it. Test user-facing behavior.
Use Fake Timers
Control time in tests with
vi.useFakeTimers() for predictable async tests.Mock External Dependencies
Mock API calls, file system, and external services to keep tests fast and isolated.
Clean Up After Tests
Use
afterEach to reset mocks, timers, and DOM to prevent test pollution.Testing Patterns
Arrange-Act-Assert (AAA)
Given-When-Then (BDD)
Debugging Tests
Running Single Test
Using Debug Output
Inspecting Failed Tests
- Test execution flow
- Console logs
- Error stack traces
- Component renders
Continuous Integration
Example GitHub Actions workflow:Common Testing Scenarios
Testing Async Operations
Testing Error Boundaries
Testing Forms
Next Steps
Configuration
Learn how to configure test environment variables
Backend Integration
Test backend API integration with mocks