Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not reset handlers if msw is not defined for story, fix #25 #79

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 15 additions & 17 deletions packages/msw-addon/src/mswDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,24 @@ export const mswDecorator: DecoratorFunction = (
parameters: { msw },
} = context

if (api) {
if (api && msw) {
api.resetHandlers()

if (msw) {
if (Array.isArray(msw) && msw.length > 0) {
// Support an Array of request handlers (backwards compatability).
api.use(...msw)
} else if ('handlers' in msw && msw.handlers) {
// Support an Array named request handlers handlers
// or an Object of named request handlers with named arrays of handlers
const handlers = Object.values(msw.handlers)
.filter(Boolean)
.reduce(
(handlers, handlersList) => handlers.concat(handlersList),
[] as RequestHandler[]
)
if (Array.isArray(msw) && msw.length > 0) {
// Support an Array of request handlers (backwards compatability).
api.use(...msw)
} else if ('handlers' in msw && msw.handlers) {
// Support an Array named request handlers handlers
// or an Object of named request handlers with named arrays of handlers
const handlers = Object.values(msw.handlers)
.filter(Boolean)
.reduce(
(handlers, handlersList) => handlers.concat(handlersList),
[] as RequestHandler[]
)

if (handlers.length > 0) {
api.use(...handlers)
}
if (handlers.length > 0) {
api.use(...handlers)
}
}
}
Expand Down