Fixtures & Test Data
On this page
What Fixtures Are
Fixtures are repeatable test inputs: objects, JSON payloads, database rows, and files that represent realistic cases. Good fixtures are small, readable, and intentionally cover edge cases.
Why Fixtures Matter
Random data can hide bugs and create flaky tests. Hardcoded inline objects everywhere create duplication. Fixtures give you consistency and reuse across suites.
TypeScript Fixture Pattern
export function userFixture(overrides?: Partial<{ id: string; email: string; role: string }>) {
return {
id: 'u_1',
email: 'user@example.com',
role: 'viewer',
...overrides
};
}
Using Fixtures in Tests
import test from 'node:test';
import assert from 'node:assert/strict';
import { userFixture } from './fixtures';
test('fixture overrides are predictable', () => {
const u = userFixture({ role: 'admin' });
assert.equal(u.role, 'admin');
assert.equal(u.email, 'user@example.com');
});
Database Fixtures
For integration tests, fixtures often mean seed scripts or helper functions that insert rows. Keep them minimal and explicit. Prefer building only the rows the test needs, not loading giant seed datasets.
Production Guidance
- Use fixtures to encode edge cases: missing fields, huge payloads, weird unicode
- Make fixtures readable: failures should tell a story
- Reset state deterministically between tests