Mocking API requests with @storybook/addon-msw: inconsistent mock availability in test‑runner
0 reputation · 17 Feb 2022, 22:40 UTC
0 reputation · 17 Feb 2022, 22:40 UTC
When integrating a component that fetches data from an external API, we want to run Storybook stories and the Storybook test‑runner without exposing production credentials. The @storybook/addon‑msw addon intercepts network requests in the browser, but the test‑runner executes stories in a Playwright environment where MSW mocks are not automatically applied unless configured globally.
We need a single source of truth for mock definitions that works for both local story development and CI test runs. Two design options exist: (1) centralize handlers in a shared MSW setup file, or (2) define mocks per story using the addon’s msw parameter. Each approach impacts test isolation, maintenance overhead, and the ability to version mock scenarios across unit tests, end‑to‑end tests, and Storybook stories.
Unresolved decision: how to share and version mock scenarios so that the same mock definitions are used consistently across Storybook, unit tests, and Playwright test‑runner executions, while preventing accidental real network calls in both development and CI.
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.