Test System Overview
Vitest
Fast test executionVite integration
Context Based
Authentication testingPermission simulation
Transaction
Auto rollbackIsolated tests
Fixture System
Test dataReusable
Vitest Configuration
Sonamu project test environment is configured withvitest.config.ts and global.ts.
vitest.config.ts
Sonamu provides thegetSonamuTestConfig() function to easily configure test settings.
api/vitest.config.ts
getSonamuTestConfig Options
getSonamuTestConfig() supports all Vitest options and provides Sonamu-optimized defaults.
global.ts Setup
global.ts is a global setup file that runs once before all tests. Export Sonamu’s setup function to initialize the test environment.
api/src/testing/global.ts
export { setup } from "sonamu/test" reads the test settings from sonamu.config.ts to automatically configure parallel test environments (multiple test DBs).Advanced Configuration Example
Here’s an advanced configuration with custom sequencer and reporters.api/vitest.config.ts
bootstrap Function
Sonamu initializes the test environment with thebootstrap function.
What bootstrap Does
- Sonamu initialization: Initialize framework in test mode
- Transaction management: Auto rollback for each test
- Timer reset: Reset Vitest’s fake timers
- Test reporting: Pass results to Naite system
test Function
Sonamu provides a customtest function that wraps Vitest’s test.
Basic Usage
Context Injection
Thetest function automatically injects Mock Context.
Full Mock Context Properties
Mock Context is composed of the minimum values that satisfy the actualContext type (src/api/context.ts). The request/reply fields, which are populated from real HTTP requests, are not used in the Mock.
Base Properties
Authentication
Naite Logging
Disabled in Mock
Context Usage Example
Context under testAs
Customizing Context
You can directly modify mutable Mock Context properties when needed:Context property summary:
- Base: transport, headers, locale
- Auth: user, session
- Naite: naiteStore
- HTTP-only (disabled in Mock): request, reply, createSSE
testAs Function
Test in an authenticated state as a specific user.Basic Usage
Permission Testing
test.skip, test.only, test.todo
You can use Vitest’s features as-is.test.each
Repeat the same test with multiple input values.Transaction Auto Rollback
Each test runs in an isolated Transaction and automatically rolls back.- Isolation between tests
- No cleanup needed
- Fast execution (no actual INSERT/DELETE)
Test File Structure
Accessing Context
You can directly use Context within tests.Async Tests
All tests must beasync functions.
Cautions
Next Steps
Test Scaffolding
Auto test generation
Test Helpers
Fixtures and utilities
Vitest Docs
Vitest guide
Model System
Understanding Models