CommercetoolsMock
The main entry point. Construct one per test file (or share one and clear()
between tests).
import { CommercetoolsMock } from '@labdigital/commercetools-mock'
const ctMock = new CommercetoolsMock(options)Constructor
Section titled “Constructor”new CommercetoolsMock(options?: Partial<CommercetoolsMockOptions>)All options are optional. See Configuration options for the full list and defaults.
Methods
Section titled “Methods”project(projectKey?)
Section titled “project(projectKey?)”project(projectKey?: string): ProjectAPIReturns a ProjectAPI scoped
to the given project. Falls back to defaultProjectKey when called without an
argument; throws if neither is set.
ctMock.project() // uses defaultProjectKeyctMock.project('other-project')registerHandlers(server)
Section titled “registerHandlers(server)”registerHandlers(server: SetupServer): voidRegisters the mock’s request handlers on an existing msw server. Equivalent to
server.use(...ctMock.getHandlers()). See
msw integration.
getHandlers()
Section titled “getHandlers()”getHandlers(): AnyHandler[]Returns the raw msw handler array (OAuth + API HEAD/GET/POST/DELETE), so
you can construct the server yourself.
clear()
Section titled “clear()”clear(): Promise<void>Wipes all data from the storage backend. Call it in afterEach to isolate
tests. Always await it.
runServer(port?)
Section titled “runServer(port?)”runServer(port = 3000): Promise<void>Starts a real HTTP server on 0.0.0.0:{port}. See
Standalone server.
authStore()
Section titled “authStore()”authStore(): OAuth2StoreReturns the in-memory OAuth token store, so you can inspect issued tokens in tests. See Authentication.
mswServer()
Section titled “mswServer()”mswServer(): SetupServer | undefinedReturns the msw server the mock is aware of, if any.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
app | FastifyInstance | The underlying Fastify application. |
server | http.Server | The Node HTTP server (app.server). |
options | CommercetoolsMockOptions | The resolved options (defaults merged with yours). |