Skip to main content
Learn about Sonamu’s Vitest-based test system and structure.

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 with vitest.config.ts and global.ts.

vitest.config.ts

Sonamu provides the getSonamuTestConfig() 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 the bootstrap function.

What bootstrap Does

Key features:
  1. Sonamu initialization: Initialize framework in test mode
  2. Transaction management: Auto rollback for each test
  3. Timer reset: Reset Vitest’s fake timers
  4. Test reporting: Pass results to Naite system

test Function

Sonamu provides a custom test function that wraps Vitest’s test.

Basic Usage

Context Injection

The test function automatically injects Mock Context.

Full Mock Context Properties

Mock Context is composed of the minimum values that satisfy the actual Context 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:
Cautions:
  1. Mock Context is for tests: it may differ from real HTTP requests.
  2. No session persistence: all state is reset when the test ends.
  3. Direct mutation discouraged: prefer testAs() over hand-editing Context.
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.
Benefits:
  • 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 be async functions.

Cautions

Cautions when writing tests:
  1. bootstrap(vi) required: Call in every test file
  2. async required: All test functions must be async
  3. Transaction based: Auto rollback after test ends
  4. Context injection: test/testAs auto-configure Context
  5. Isolated tests: No dependencies between tests

Next Steps

Test Scaffolding

Auto test generation

Test Helpers

Fixtures and utilities

Vitest Docs

Vitest guide

Model System

Understanding Models