Skip to main content
Learn how to efficiently manage test data using Sonamu’s Fixture system.

Test Helpers Overview

Fixture Loader

Test data reuse Type-safe loading

Fixture Manager

Copy data between DBs Auto relationship handling

Easy Setup

Define once Share across tests

Auto Cleanup

Transaction based Auto rollback

Fixture Loader

createFixtureLoader

Pre-define fixtures for tests and load when needed.

Using in Tests

Benefits

Type safety:
Reusability:
Selective loading:

Complex Fixture Patterns

Fixtures with Relationships

Dynamic Fixtures

Fixture Manager

Overview

FixtureManager provides functionality to copy real data from production DB to test DB. Key features:
  • Extract data from Production → Fixture DB
  • Sync Fixture DB → Test DB
  • Auto relationship resolution (BelongsTo, HasMany, ManyToMany)
  • Duplicate data handling

DB sync

Sync Fixture DB data to Test DB.
Execution process:
  1. Initialize Test DB (delete existing data)
  2. Fixture DB dump (pg_dump)
  3. Restore to Test DB (pg_restore)

importFixture

Import specific data from Production DB to Fixture DB.
Auto relationship resolution:

Practical Examples

Basic Fixture Pattern

Complex Scenario

Best Practices

1. Fixture Naming Convention

2. Minimal Data

3. Fixture Reuse

Cautions

Cautions when using Fixtures: 1. Transaction based: Auto rollback for each test 2. Minimal data: Create only minimum data needed for tests 3. Independence: Each test should run independently 4. Type safety: Ensure type safety with createFixtureLoader 5. Clear names: Fixture names should clearly express purpose

Next Steps

Test Structure

bootstrap, test, testAs

Test Scaffolding

Auto test generation

Model System

Understanding Models

Vitest Docs

Vitest guide