The addon extends the story context with an msw property. You can use msw.use() to control API mocking.
Global Handlers
Define network behaviors for all stories in .storybook/preview.ts using the beforeEach hook.
Story-specific Handlers
Define specific network behaviors for a single story by adding a beforeEach hook directly to that story object.
// Global handlers in .storybook/preview.ts
import { http, HttpResponse } from 'msw'
export default {
beforeEach({ msw }) {
msw.use(
http.get('https://api.acme.com/user', () => {
return HttpResponse.json({ name: 'John Maverick' })
}),
)
},
}
// Story-specific handlers
export const UserProfileNetworkError: Story = {
beforeEach({ msw }) {
msw.use(
http.get('https://api.acme.com/user', () => {
return HttpResponse.error()
}),
)
},
}