valendres / playwright-msw

A Mock Service Worker API for Playwright
MIT License
166 stars 23 forks source link

fix: trying to get headers after page is closed #27

Closed lesha1201 closed 2 years ago

lesha1201 commented 2 years ago

This change helps to fix https://github.com/valendres/playwright-msw/issues/26 in my case. But I don't really know why it fixes it so feel free to close this PR if it actually doesn't fix anything.

valendres commented 2 years ago

Thanks for making a PR to address this @lesha1201. My apologies on the delay for reviewing it, it's been a super busy week. Between preparing to move countries and finishing up some things at work before my last day, I haven't found time to actually look at open source stuff 😞

It makes sense this would fix the issue. From what I understand, when the playwright test finishes executing, the browser context is immediately closed. I suspect if there was a network call right at the end of the test, it might be possible for the promise to not have had a chance to resolve in time. Adding the await here makes sense. I'm actually not sure why I chose to not await it in the past, seems like a bad decision now 😅

lesha1201 commented 2 years ago

@valendres No problems. Thank you for your work.

The interesting part is that, from Playwright source code, it should wait callback for page.route even without returning Promise because it internally wraps it into a promise which is resolved once route.fullfill or others are called. So that's why I don't really understand why it fixes in my case since route.fullfill and route.continue are properly called in playwright-msw (both for onUnhandledRequest and onMockedResponse though not for onPassthroughResponse but even handling that doesn't fix it). Maybe there are some other async code which is executed after the context is closed.

image image image