Seeding data
Most tests start by putting the data your code expects into the mock. The recommended way to do this is to create resources through the commercetools API — the very same calls your production code makes. That keeps your test setup realistic: drafts are validated, defaults are applied, and generated fields (ids, timestamps, versions) behave exactly as they will in production.
Create through the API
Section titled “Create through the API”Use your configured apiRoot (see the
Quick start for how to
build one) and POST the resource draft:
await apiRoot .customers() .post({ body: { email: 'jane@example.com', password: 's3cret', firstName: 'Jane', }, }) .execute()Because this goes through the real create endpoint, the mock validates the draft and returns a fully-formed resource — just like commercetools.
Deterministic references
Section titled “Deterministic references”The API generates the id for you. When later steps or assertions need to refer
to a resource, either capture the returned id or set a key and look it
up by key:
// capture the idconst { body: customer } = await apiRoot .customers() .post({ body: { email: 'jane@example.com', password: 's3cret' } }) .execute()
const customerId = customer.id// or give it a stable keyawait apiRoot .products() .post({ body: { key: 'boots', productType: { typeId: 'product-type', key: 'shoe' }, name: { en: 'Boots' }, slug: { en: 'boots' } } }) .execute()
const product = await apiRoot .products() .withKey({ key: 'boots' }) .get() .execute()Order matters: create dependencies first
Section titled “Order matters: create dependencies first”commercetools resources reference each other (a product needs a product type; a cart references a customer). Create the dependencies first, then the resources that reference them — exactly as you would against a real project.
// 1. product typeawait apiRoot.productTypes().post({ body: { key: 'shoe', name: 'Shoe', description: '', attributes: [] } }).execute()
// 2. product referencing itawait apiRoot.products().post({ body: { key: 'boots', productType: { typeId: 'product-type', key: 'shoe' }, name: { en: 'Boots' }, slug: { en: 'boots' },} }).execute()Choosing which project
Section titled “Choosing which project”If you set defaultProjectKey, build your apiRoot with that key. To target a
different project, build a client (or withProjectKey) for that key.
Clearing data between tests
Section titled “Clearing data between tests”Reset all created data with ctMock.clear() — typically in an afterEach hook —
so tests stay isolated:
afterEach(async () => { mswServer.resetHandlers() await ctMock.clear()})Fixtures
Section titled “Fixtures”For larger or reusable draft bodies, build fixtures with a factory such as fishery. See the Fixtures recipe.
Reading data back
Section titled “Reading data back”To assert on stored state, read it back through the API (get, withId,
withKey, or a query). See
Querying data.