Create factories, that create objects with Prisma.
Inspired by Python's factory_boy, which is inspired by Ruby's factory_bot.
Install factory-prisma
as a dev dependency, then use it to define
factories for your Prisma models.
import db from 'wherever/your/prisma/client/is'
import { factory, random } from 'factory-prisma'
export const TeamFactory = factory(db.team, () => ({
name: random.string(), // use faker if you want to
key: random.string(),
type: TeamType.CLUB,
website: random.url(),
}));
export const SquadFactory = factory(db.squad, () => ({
team: { create: TeamFactory.fields() },
type: SquadType.SENIOR_MEN,
}));
export const PlayerFactory = factory(db.player, () => ({
key: random.string(),
name: random.string(),
dob: random.date(),
position: PlayerPosition.CM,
}));
Then, use the factories in your tests:
test("no relations", async () => {
await TeamFactory.instance();
expect(await db.team.count()).toBe(1);
});
test("with relations", async () => {
await SquadFactory.instance();
const team = await db.team.findFirstOrThrow();
const squad = await db.squad.findFirstOrThrow();
expect(await db.team.count()).toBe(1);
expect(await db.squad.count()).toBe(1);
expect(team.id).toBe(squad.teamId);
});
test("connect", async () => {
const { id } = await TeamFactory.instance();
await SquadFactory.instance({ team: { connect: { id } } });
const team = await db.team.findFirstOrThrow();
const squad = await db.squad.findFirstOrThrow();
expect(await db.team.count()).toBe(1);
expect(await db.squad.count()).toBe(1);
expect(team.id).toBe(squad.teamId);
});