Skip to content

Configuration options

The CommercetoolsMock constructor accepts a partial options object. Everything is optional; sensible defaults are applied.

import { CommercetoolsMock } from '@labdigital/commercetools-mock'
const ctMock = new CommercetoolsMock({
defaultProjectKey: 'my-project',
// …other options
})
OptionTypeDefaultDescription
defaultProjectKeystring | undefinedundefinedProject key used when you call ctMock.project() without an argument. Set this to avoid repeating the key in every test.
apiHoststring | RegExphttps://api.*.commercetools.comHost the mock intercepts for API requests. The default is a wildcard pattern that matches every commercetools API region, so a client pointed at a real commercetools host is intercepted automatically.
authHoststring | RegExphttps://auth.*.commercetools.comHost the mock intercepts for OAuth token requests. Defaults to a wildcard matching every commercetools auth region.
enableAuthenticationbooleanfalseWhen true, attaches auth middleware to the API routes so requests are checked for a bearer token. See Authentication.
validateCredentialsbooleanfalseWhen true (and enableAuthentication is on), requires a bearer token that was actually issued by the mock. When false, tokens are optional and not verified.
silentbooleantrueSuppresses request logging. Set to false to see Fastify logs — useful when debugging.
strictbooleanfalseEnables stricter behaviour in the repositories/validation. The standalone server runs with strict: true.
storageAbstractStorage | undefinedundefined (→ InMemoryStorage)A custom storage backend.
loggerFastifyBaseLogger | undefinedundefinedA custom logger instance. When provided it overrides silent and is used as the Fastify logger.

The defaults are wildcard patterns that match every commercetools region:

apiHost: https://api.*.commercetools.com
authHost: https://auth.*.commercetools.com

The * is an msw path/host wildcard. This means that if your code is already configured for a real commercetools project (for example https://api.europe-west1.gcp.commercetools.com), the mock intercepts those requests without any host configuration on your side.

To intercept a specific host instead — e.g. a local dev host — set them explicitly:

const ctMock = new CommercetoolsMock({
apiHost: 'https://api.example.test',
authHost: 'https://auth.example.test',
defaultProjectKey: 'my-project',
})

You may also pass a RegExp to match a family of hosts — handy when the host is computed at runtime.

By default the mock is silent. For debugging, either flip silent or pass your own logger:

import pino from 'pino'
const ctMock = new CommercetoolsMock({
silent: false, // enable Fastify's built-in logger at "info"
})
// or supply your own instance (takes precedence over `silent`)
const ctMock2 = new CommercetoolsMock({
logger: pino({ transport: { target: 'pino-pretty' } }),
})

Leave storage unset to use the default in-memory backend. Pass an instance to use SQLite or a custom backend:

import { CommercetoolsMock, InMemoryStorage } from '@labdigital/commercetools-mock'
const ctMock = new CommercetoolsMock({
storage: new InMemoryStorage(),
})
{
enableAuthentication: false,
validateCredentials: false,
defaultProjectKey: undefined,
apiHost: 'https://api.*.commercetools.com',
authHost: 'https://auth.*.commercetools.com',
silent: true,
strict: false,
storage: undefined, // → new InMemoryStorage()
logger: undefined,
}