Test Fixtures
Manage reusable test data with fixtures, factories, and JSON snapshot files.
What you'll learn
- What a fixture is and why reusable test data matters
- Loading static fixtures from JSON files
- Building dynamic fixtures with factory functions and faker
- Keeping fixtures small, realistic, and isolated
Concept
Why Fixtures
Tests need data — users, orders, products, API responses. Duplicating that data inline in every test makes the suite verbose and inconsistent: one test's user has an id, another forgets it, and they drift. A fixture is a named, reusable piece of test data that keeps every test working with the same realistic shape, defined in one place.
Static Fixtures
The simplest fixture is a JSON file in a __fixtures__ directory, imported or read at test time. This works well for API response snapshots, configuration objects, and other data that rarely changes. Static fixtures are easy to read and diff, but they are rigid — every test gets the exact same values, which can cause tests to pass for the wrong reasons if they accidentally depend on a specific id.
Factory Functions
A factory function generates fresh test data with sensible defaults and per-test overrides: makeUser({ role: "admin" }). Combined with a library like @faker-js/faker, factories produce realistic, varied data (names, emails, dates) so tests do not over-fit to one example. Factories scale better than static JSON because each test can customize only the fields it cares about.
Keep Them Lean
Fixtures should contain only the fields the test actually exercises — bloated fixtures obscure intent. Store shared factories in a test/factories folder and import them across the suite. Never let a fixture depend on external state (the current date, a database id sequence); inject those values explicitly so tests stay deterministic and independent.
In Practice
A mature test suite usually has a test/factories folder with one factory per domain entity, each using @faker-js/faker for realistic defaults and accepting a partial-overrides argument. Seed faker in your test setup (faker.seed(12345)) so that the random data is reproducible in CI — unseeded randomness makes flaky failures impossible to replay. For API response fixtures, store real captured responses as JSON in __fixtures__ and load them with import or fs.readFileSync; this preserves the exact shape the server returns, including fields you might not care about but that the parser must tolerate. Keep fixtures close to the tests that use them when they are specific, and share them from a central folder when they are reused. The investment in good fixtures pays off every time a new test needs data — it is one line instead of twenty.
Example
// test/factories/user.ts
import { faker } from "@faker-js/faker";
export interface User {
id: number;
name: string;
email: string;
role: "admin" | "member";
}
export function makeUser(overrides: Partial<User> = {}): User {
return {
id: faker.number.int({ min: 1, max: 1000 }),
name: faker.person.fullName(),
email: faker.internet.email(),
role: "member",
...overrides, // test-specific fields win
};
}
// In a test
import { makeUser } from "../factories/user";
it("allows admins to delete posts", () => {
const admin = makeUser({ role: "admin" });
expect(canDelete(admin)).toBe(true);
});
makeUser returns a realistic user with random data by default; each test overrides only the field it cares about. faker keeps the data varied so tests do not over-fit.
Try it
- JSON Formatter
Format the JSON fixture files your tests load.
- JSON Tree Viewer
Explore nested fixture structures visually.
Common mistakes
The mistake
Using one giant fixture for every test
The fix
Build small, focused fixtures or use factory overrides. A 50-field object in every test hides which fields the test actually depends on.
The mistake
Letting faker generate data without seeding
The fix
Seed faker in CI (faker.seed(123)) so failures are reproducible. Unseeded random data makes flaky failures impossible to rerun.
Practice Test Fixtures
7 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.