pnpm 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 - Initialize Test DB
Copy development DB schema to test DB.- 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.- Records referenced by foreign keys
- Relationship table records
- Reverse reference records (optional)
sync - Synchronize
Apply saved fixtures to test DB.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.| Option | Description | Example |
|---|---|---|
--all | Generate for all Entities | pnpm fixture gen --all |
--include | Include specific Entities only | pnpm fixture gen --include User,Post |
--exclude | Exclude specific Entities (use with —all) | pnpm fixture gen --all --exclude Comment |
--count | Number of records to generate | pnpm fixture gen --include User --count 10 |
--save-to | Specify save method | pnpm fixture gen --save-to db |
--use-llm | Generate realistic data with LLM (fixtureHint-based, requires ANTHROPIC_API_KEY) | pnpm fixture gen --use-llm |
--no-cache | Disable LLM cache (use with --use-llm) | pnpm fixture gen --use-llm --no-cache |
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:
| Property | Description |
|---|---|
entity | Name of the companion Entity to create alongside |
overrides | Fixed override values for the companion fixture. Use {{fieldName}} syntax to reference field values from the parent fixture |
count | Number of companions to create per parent (default: 1) |
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:
| Property | Description |
|---|---|
fixtureParentOverrides | WHERE conditions applied when querying available parent records. For example, if there is a discriminator column for subtypes, filter by that value |
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.| Option | Description | Example |
|---|---|---|
--all | Fetch from all Entities | pnpm fixture fetch --all |
--include | Include specific Entities only | pnpm fixture fetch --include User,Post |
--exclude | Exclude specific Entities | pnpm fixture fetch --all --exclude Log |
--strategy | Data selection strategy | pnpm fixture fetch --strategy recent |
--limit | Number of records to fetch | pnpm fixture fetch --limit 20 |
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.| Option | Description | Example |
|---|---|---|
--include | Entity to explore | pnpm fixture explore --include User |
--strategy | Data selection strategy | pnpm fixture explore --strategy sample |
--limit | Number of records to query | pnpm fixture explore --limit 5 |
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
Practical Examples
Complex Relationship Testing
Specific Scenario Data
Troubleshooting
Fixture DB Connection Failed
Problem: Cannot access remote fixture DBsonamu.config.ts
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