playwright-msw icon indicating copy to clipboard operation
playwright-msw copied to clipboard

Behavior of handlers which has same path and different method is not same with MSW.

Open 1010real opened this issue 1 year ago • 1 comments

First, thanks to the contributors.

Because I found a behavior below, I created an issue.

  1. register below handlers.
import { rest } from 'msw'
const handlers = [
  rest.get(`/v1/customer/:id`, (_, res, ctx) => {
    return res(ctx.status(200), ctx.json({ id: 'xxx', name: 'Taro' }))
  }),
  rest.post(`/v1/customer/additionalInfo`, (_, res, ctx) => {
    return res(ctx.status(200), ctx.json({ additionalInfoId: 'yyy' }))
  }),
]
  1. access POST /v1/customer/additionalInfo on application with msw -> work. on playwright (with playwright-msw) -> not work

To make it work on playwright, I need to reverse handlers array or overwrite by worker.use on test case. Probably, playwright-msw's routing depends on path only? https://github.com/valendres/playwright-msw/blob/main/packages/playwright-msw/src/router.ts

1010real avatar Jun 22 '23 12:06 1010real

I suspect this is due to a race condition when registering msw handlers. https://github.com/valendres/playwright-msw/blob/4202f690dadec92324b898c3e209b76257f89641/packages/playwright-msw/src/router.ts#L124-L139

registerMswHandler() checks to see if there's existing route data, and if not proceeds to initialize it. However, this initialization happens asynchronously (due to L135), so two registrations happening concurrently might both think they're the first and initialize the data.

1000hz avatar Jan 19 '24 16:01 1000hz