Skip to content

@labdigital/commercetools-mock

A comprehensive mock of the commercetools REST API, built for testing TypeScript code. Create data through the API, run your code against it, and assert on the results — no real commercetools project required.

Testing code that talks to commercetools is hard. Hitting a real project is slow, flaky, requires credentials, and pollutes shared data. Hand-written stubs drift from the real API and never cover update actions, predicates, or reference expansion.

@labdigital/commercetools-mock runs a faithful, in-memory implementation of the commercetools REST API inside your test process. It speaks the same HTTP, so the official @commercetools/platform-sdk and any other HTTP client work against it unchanged.

Faithful behaviour

Real update actions, where predicates, reference expand, price selection, product search and OAuth token flows — not just canned responses.

Fast and isolated

Everything runs in-memory in your test process. No network, no containers, no shared state between tests.

Built on msw

Intercepts requests with Mock Service Worker, so it composes with the rest of your mocked APIs.

Broad coverage

Products, carts, orders, customers, B2B business units, quotes, discounts, custom objects and much more.

Add the mock and its peer dependencies as dev dependencies:

Terminal window
pnpm add -D @labdigital/commercetools-mock msw @commercetools/platform-sdk

See Installation for requirements and peer-dependency details.

import { CommercetoolsMock } from '@labdigital/commercetools-mock'
import { setupServer } from 'msw/node'
const ctMock = new CommercetoolsMock({ defaultProjectKey: 'my-project' })
const server = setupServer()
beforeAll(() => server.listen())
beforeEach(() => ctMock.registerHandlers(server))
afterEach(async () => {
server.resetHandlers()
await ctMock.clear()
})
afterAll(() => server.close())
test('the order is placed', async () => {
// Create the data your code expects, through the API…
await apiRoot.customers().post({ body: customerDraft }).execute()
// …run the code under test (it uses the commercetools SDK)…
const order = await placeOrder('jane@example.com')
// …and assert on the result.
expect(order.orderState).toBe('Open')
})

@labdigital/commercetools-mock is an open-source project from Lab Digital, a digital commerce agency specialising in composable, MACH-based commerce with commercetools — and the team behind Evolve, a composable agentic commerce platform. We build and rely on this mock to test real commercetools integrations every day, and we maintain it in the open under the MIT license. Contributions are welcome.