Skip to main content
Learn how to define reusable test data using Sonamu’s createFixtureLoader.

Creating Fixtures Overview

createFixtureLoader

Type-safe definition Auto-completion support

Reusable

Define once Share across tests

Relation Handling

Auto BelongsTo resolution Nested data

Selective Loading

Load only what you need Fast tests

createFixtureLoader

Basic Structure

Type Definition

Features:
  • Type-safe: TypeScript infers fixture names and return types
  • Parallel loading: Multiple fixtures loaded simultaneously with Promise.all
  • Selective: Load only needed fixtures

Practical Examples

Simple Fixtures

Fixtures with Relations

Creating Multiple Data

Complex Scenarios

Using in Tests

Basic Usage

Type Safety

Selective Loading

Best Practices

1. Clear Names

2. Minimal Data

3. Fixture Reuse

4. Consistent Structure

Advanced Patterns

Factory Functions

Cautions

Cautions when writing fixtures: 1. Transaction-based: Auto-rollback after each test 2. Minimal data: Create only minimum data needed for test 3. Independence: Fixtures should be independent of each other 4. Reuse: Actively utilize common fixtures 5. Clear names: Fixture names should clearly express purpose

Next Steps

Test DB

Test DB setup

Loading Fixtures

Import production data

Syncing Fixtures

Fixture → Test DB

Test Helpers

createFixtureLoader details