valendres / playwright-msw

A Mock Service Worker API for Playwright

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handlers order conflict

lesha1201 opened this issue · comments

After upgrading to v2.0.0, I have a conflict in the order of handlers. For example, in I have such handlers:

export const handlers = [
  rest.get('/documents/months', ...),
  rest.get('/documents/:slug', ...),
];

It has such order, because I want /documents/months to be processed first and other requests with similar path fallback to /documents/:slug.

But playwright-msw converts paths for page.route from Playwright and registers them in the same order:

/documents/months -> /documents/months
/documents/:slug -> /documents/*

However, the latest registered matching route takes precendence in Playwright. So /documents/* is registered at the end and it handles all requests that starts with /documents/... including /documents/months.

I can fix it by reversing handlers for createWorker:

const test = base.extend<{
  worker: MockServiceWorker;
}>({
  worker: createWorkerFixture(handlers.reverse()),
});

Thanks for pointing this out @lesha1201! Having to reverse the handlers that are provided to createWorkerFixture definitely is not intended. I'm working on a fix to ensure that the behaviour is consistent when MSW is running within the browser. Will get this published ASAP.

Sorry for the inconvenience. I'll be adding some more integration tests to make sure this doesn't happen again 😞

A fix for this issue has been published in 2.0.1. Please feel free to close this issue if resolved 🙂

@valendres Thanks. It resolved the issue.