Recipe — Fixtures with fishery
For anything beyond a trivial resource, a fixture factory keeps tests readable and consistent. fishery pairs well with the mock — build draft bodies once and reuse them across tests.
Install
Section titled “Install”pnpm add -D fisheryA customer-draft factory
Section titled “A customer-draft factory”import type { CustomerDraft } from '@commercetools/platform-sdk'import { Factory } from 'fishery'
export const customerDraftFactory = Factory.define<CustomerDraft>( ({ sequence }) => ({ email: `customer-${sequence}@example.com`, password: 's3cret', }),)Use it
Section titled “Use it”import { customerDraftFactory } from '../test/factories/customer'import { apiRoot } from '../test/ct-client'
test('…', async () => { const draft = customerDraftFactory.build({ firstName: 'Jane' })
const { body } = await apiRoot.customers().post({ body: draft }).execute() // body.customer.id is the generated id you can assert against})- Override only the fields a given test cares about; let the factory fill the rest.
- Use the
sequencefor stable, unique emails/keys. - Give resources a stable
keyin the draft so later steps can look them up withwithKeywithout capturing the generated id. - Build related drafts with linked factories (e.g. an order draft that references a customer created by another factory) so references resolve when you expand them.