prisma-factory
TypeScript icon, indicating that this package has built-in type declarations

0.1.13 • Public • Published

prisma-factory

Factories ❤️ Prisma

Check out the Discussions

This library uses Prisma generators to create type-safe factory functions based on your Prisma schema.

Paired with a faker library like Chance.js, you can create the data your tests need with minimal effort.

Installation

yarn add prisma-factory

Usage

First, add a model to your prisma schema. For example:

model Musician {
  id           Int        @id @default(autoincrement())
  createdAt    DateTime   @default(now())
  updatedAt    DateTime   @updatedAt
  email        String     @unique
  name         String?
  instrument   Instrument @relation(fields: [instrumentId], references: [id])
  instrumentId Int
}

model Instrument {
  id        Int        @id @default(autoincrement())
  name      String
  musicians Musician[]
}

Next, run prisma generate.

yarn prisma generate

Finally, create a factory for that model (we recommend test/factories/), import the newly generated function, and pass in the desired default factory attributes.

import { createMusicianFactory } from '@generated/prisma-factory';

export const MusicianFactory = createMusicianFactory({
  email: 'asdf@wee.net',
  name: `${chance.first()} ${chance.company()}`,
});

Once the factory is defined, you can use it in any of your tests.

import { MusicianFactory } from 'tests/factories/musician';

describe('creating factories', () => {
  beforeEach(async () => {
    // create 2 users
    await MusicianFactory.create();
    await MusicianFactory.create();
  });

  it('tests something', () => {
    // your test here.
  });
});

Furthermore, you can easily create complex relationships using Factory.build and the familiar Prisma syntax:

import { MusicianFactory } from 'tests/factories/musician'
import { InstrumentFactory } from 'tests/factories/instrument'

describe('creating relationships', () => {
  beforeEach(async () => {
    await MusicianFactory.create({
      name: "Rockstar"
      instrument: {
        create: InstrumentFactory.build({name: 'Harmonica'})
      }
    })
  });

  it('tests something', () => {
    // your test here.
  })
});

Package Sidebar

Install

npm i prisma-factory

Weekly Downloads

454

Version

0.1.13

License

MIT

Unpacked Size

22 kB

Total Files

10

Last publish

Collaborators

  • kgajera
  • cball
  • mikecavaliere