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

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

Open
1010real opened this issue Jun 22, 2023 · 1 comment

Comments

@1010real
Copy link

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
Copy link

1000hz commented Jan 19, 2024

I suspect this is due to a race condition when registering msw handlers.

private async registerMswHandler(handler: RequestHandler): Promise<void> {
const path = getHandlerPath(handler, this.config);
const existingRouteData = this.getRouteData(path);
if (existingRouteData) {
this.setRouteData({
...existingRouteData,
requestHandlers: [...existingRouteData.requestHandlers, handler],
});
} else {
this.setRouteData({
path,
routeHandler: await this.registerPlaywrightRoute(path),
requestHandlers: [handler],
});
}
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants