pnpm sonamu fixture command manages consistent data sets for testing. You can copy actual data from the development environment to the test environment for stable and repeatable tests.
Basic Concept
Fixture is a fixed data set for testing:- Consistency: All tests start with the same initial data
- Isolation: Separate test DB from development DB
- Reproducibility: Repeatable tests under same conditions
- Relationship preservation: Foreign key relationships maintained
Commands
init, import, sync, gen, and fetch are dry-runs unless you pass --execute. In a TTY they
ask for confirmation; in non-interactive runs (such as CI), add --confirm. explore only reads data.init - Initialize Test DB
Copy development DB schema to test DB.init dumps the schema of the database for the current NODE_ENV and creates the
Fixture DB and the Test DB from it. It prints a line only for a target it skips: <label>: Database "<name>" Already exists when the database already exists, or (LOCAL) Testing DB: Skipped! when
the Test DB points to the same database as the Fixture DB.
Databases created:
- Fixture DB (remote): Shared fixture storage
- Test DB (local): Local testing DB
init copies schema only. Data is not included.import - Import Data
Save specific records from development DB as fixtures.pnpm sonamu fixture import 1 2 3 --execute) and pick it from a prompt; the IDs are always required. Related records are imported too, and the fixtures are then synced to the test database.
Related data automatically included:
- Records referenced by foreign keys
- Relationship table records
- Reverse reference records (optional)
sync - Synchronize
Apply saved fixtures to test DB.sync terminates connections to the Test DB, drops and recreates it, and copies
the Fixture DB into it with pg_dump and pg_restore. The output is what psql, pg_dump, and
pg_restore print.
gen - Auto Fixture Generation
Automatically generate fixtures using Entity’s cone metadata. Useful when you don’t have real data or need virtual test data.The generation method prompt is only shown when User entity is included in the selection.
If User entity is not selected, the flow proceeds directly to the save method selection.
sign-up/email endpoint.
How this mode works:
FixtureGeneratorgenerates User data (name, email, username, etc.)- Calls better-auth sign-up API via
Sonamu.auth.handler()to register users - Automatically updates
email_verifiedtotruefor all created users - Outputs the list of created credentials (email, password) to the console
generateBatch() flow to insert data directly into the DB. In this case, no better-auth authentication records are created, so login is not possible.
CLI options:
Save methods:
db: Save directly to Fixture DB (default)file: Save totest/fixtures/{entity_table}.jsonfilefile:{filename}: Save with specified filenamenone: Don’t save, just output to console
- Relation — Fields with foreign key relationships automatically create/reference related records.
- cone.note + LLM — When
--use-llmis enabled and the field’s cone has anotedefined, LLM generates realistic values based on thenotecontent. Falls back to the next step if LLM call fails. - cone.fixtureGenerator — If the cone has a
fixtureGenerator(Faker expression, etc.), it is executed. - cone.fixtureDefault — If the cone has a
fixtureDefaultfixed value, it is used. - Type-based default generation — If none of the above strategies apply, values are auto-generated based on the field type (string, number, date, etc.).
When using
--use-llm, LLM takes priority over fixtureGenerator for fields with cone.note. This
allows generating more realistic, domain-appropriate data.If an LLM-generated string exceeds the field’s
length constraint, it is automatically truncated.
For example, if LLM generates 120 characters for a varchar(100) field, only the first 100
characters are used.fixtureCompanions declared, companion Entity fixtures are automatically created alongside the parent fixture. For example, in a better-auth based project, generating a User fixture will also automatically create a credentials Account.
fixtureCompanions is declared in the Entity’s id prop cone:
To retroactively add
fixtureCompanions to existing better-auth projects:
fixtureCompanions configuration to better-auth Entities (such as User) in their entity.json, only when the configuration does not already exist. Entities that already have the configuration are skipped.
parentId Entity Fixture Generation:
Entities with parentId (subtypes in an inheritance relationship) are handled specially during gen. Instead of creating new parent records, the generator finds existing parent records in the DB that don’t yet have a corresponding subtype row, and uses those IDs.
For example, if Paper is a subtype of Achievement (parentId: "Achievement"):
- Queries the
achievementstable for IDs that don’t have a corresponding row in thepaperstable - Creates
Paperfixtures with those IDs, forming the parent-child relationship
fixtureParentOverrides in the id prop’s cone:
fetch - Fetch Data from Production DB
Fetch data from actual production (or development) DB and save to Fixture DB. Use when you need realistic test data.
Data selection strategies:
recent(default): Fetch most recent data firstsample: Random sampling
- Related data is automatically fetched together (up to depth level 2)
- Foreign key referential integrity maintained
explore - Query DB Data
Query only DB data. Useful for quickly checking data without saving.
Data selection strategies:
sample(default): Random samplerecent: Most recent datarandom: Completely randomids: Specify specific IDsquery: Custom query
Usage Workflow
1. Initial Setup
Run once when starting the project.2. Select Needed Data
Import actual data needed for testing.3. Write Tests
user.model.test.ts
4. Run Tests
Fixture Files
Storage Location
File Format
src/fixtures/users.json
- JSON format
- Separate files per Entity
- Related data automatically included
- Version controlled with Git
Relationship Data Handling
Automatic Foreign Key Inclusion
When importing a Post, connected Users are automatically included.N:M Relationship Handling
Many-to-many relationship tables are also automatically included.Test Isolation
Reset Before Each Test
- Each test is independent
- Test order doesn’t matter
- Parallel execution possible
Transaction Isolation
Callbootstrap(vi) once in a test file. Each test() then runs inside a transaction that is
rolled back afterwards, so data created in one test does not leak into the next.
Practical Examples
Complex Relationship Testing
Specific Scenario Data
Troubleshooting
Fixture DB Connection Failed
Problem: Cannot access remote fixture DB.env.test
Missing Relationship Data
Problem: Foreign key error occursFixture Conflict
Problem: ID duplicationBest Practices
1. Minimal Data
2. Meaningful Data
3. Version Control
4. CI/CD Integration
.github/workflows/test.yml
Next Steps
Testing
Learn about Naite test framework
migrate
Manage migrations