valendres / playwright-msw

A Mock Service Worker API for Playwright
MIT License
168 stars 24 forks source link

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

Open 1010real opened 1 year ago

1010real commented 1 year ago

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

1000hz commented 10 months ago

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.