Vue Storefront Middleware ↔ commercetools: Testing Cart/Checkout Flows Without Production Credentials
0 reputation · 22 Jan 2022, 16:07 UTC
0 reputation · 22 Jan 2022, 16:07 UTC
Vue Storefront's commercetools middleware (@vsf-enterprise/commercetools-api) requires valid projectKey, clientId, and clientSecret at startup; there is no configuration flag to enable a mock adapter.
createApiClient() is instantiated once per process; replacing it with a test double is not documented or supported.@vsf-enterprise/commercetools-api?35775 reputation · 23 Jan 2022, 00:15 UTC
There is no supported configuration flag in @vsf-enterprise/commercetools-api that switches the middleware to a mock commercetools adapter, and replacing createApiClient() with a test double is undocumented. Do not fork the package just to run cart or checkout tests. Test against a non-production commercetools project using short-lived OAuth credentials, or drive the SDK through a local HTTP proxy that returns recorded cart/checkout responses.
The middleware validates projectKey, clientId, and clientSecret when the API client is constructed, and it creates that client once per process. Because no mock or adapter option is exposed in the configuration schema, tests cannot simply flip a mode flag to bypass commercetools.
clientId and clientSecret as masked secrets. Do not commit or long-store access tokens.I need the exact Vue Storefront major version and the installed @vsf-enterprise/commercetools-api package version. Client instantiation, configuration keys, and any override options vary by release, and that determines whether a baseURL override or constructor option is available without forking.
Unverified: The 60-day trial inactivity expiry is reported by the questioner but not confirmed here; commercetools account lifecycle terms are release-dependent.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 22 Jan 2022, 21:28 UTC
The middleware's createApiClient() has accepted an optional fetch parameter since roughly v2.0.0, which is the documented extension point for injecting a test double without forking the package. In practice this works reliably when the client is instantiated per test suite (not as a process singleton) — call createApiClient({ fetch: mockFetch }) fresh in each test file. At the HTTP layer you can then use msw or nock to replay recorded HAR files that cover cart discounts, inventory reservation, and B2B approval flows; the commercetools Node SDK v2+ cleanly separates the HTTP middleware from business logic, so mock fidelity is high. For CI, the commercetools-recommended pattern is a dedicated service-account project with non-expiring credentials stored in a secret manager, not per-developer sandbox trials (which only rotate if manually regenerated). Contract testing with Pact adds maintenance overhead; most VSF teams prefer HTTP-level contract tests via recorded responses.