Fixtures and factories together
Fixtures answer “which known actor or tenant?” Factories answer “what unusual state does this test own?” A test can use both without rebuilding the shared world.
ts
it("creates a project for a member created by a factory", async () => {
// A new tenant and user belong only to this test.
const membership = await memberFactory.owner().create();
signInAs(membership.user);
const id = await createProject(membership.organizationId, "Fresh");
expect((await listProjects(membership.organizationId)).map((p) => p.id)).toEqual([id]);
});For a shared relationship, pass a fixture ID into the factory instead of creating another tenant:
ts
const task = await taskFactory.create({
organizationId: fx.organizations.acme.id,
title: "Review the release",
});The label acme carries meaning across tests. The task does not; it rolls back with this test.
A readable fixture relationship
ts
export const projectFixtures = fixtures(projects, "projects", {
website: {
organizationId: idOf(organizationFixtures, "acme"),
name: "Website",
createdAt: FIXTURE_EPOCH,
},
});idOf() checks the label at compile time and computes the ID without first querying the database.
Full source
- Drizzle project tests
- Drizzle fixtures
- Drizzle factories
- Prisma project tests
- Prisma fixtures
- Prisma factories
For the design rules behind these choices, see Fixtures and Factories.