Replies: 1 comment 5 replies
-
Hey, @thepassle. Thanks for proposing this. Let's discuss it a bit before we come up with the best way to support this. So, here I was writing a really detailed proposal when I noticed that we kind of already guard against this very concurrence issue. Take a look at this: Lines 156 to 177 in f5f31fa The way MSW worker functions right now, it will only signal the One case where I thought issues may arise is if you add runtime handlers via Perhaps starting from an integration test for this two-client scenario is the right approach here. |
Beta Was this translation helpful? Give feedback.
-
We use
@web/test-runner
, which runs each test file in a real browser (via puppeteer/playwright), in its own separate browser tab for concurrency and test isolation.Given:
foo.test.js
bar.test.js
@web/test-runner
would spawn a browser with 2 browser tabs, one forfoo.test.js
and one forbar.test.js
.Considering that there is only one service worker for an origin (in the case of test runs, localhost), this means that potentially race conditions can exist, where
foo.test.js
accidentally overrides mocks inbar.test.js
. We've introduced MSW via @web/mocks at ING, and interestingly I havent ran into this issue yet. There has been one instance of a reported issue where I suspected this might be happening, but then it magically resolved itself and then became unreproducible.In any case, I think we can make MSW more robust in cases where MSW is used in multiple tabs at a time (which I think is also the direction Vitest browser is going). I think this change can be opt-in, and configurable via the
worker.start
configuration. Some initial thoughts on what this could look like is:We can think of a better name than
scopeMocksPerTab
, ofcourse.Given that we already have the
clientId
as a unique identifier for each browser tab in the service worker, we should be able to keep some kind of map to identify which browser tab has registered which mocks, and only respond with mocks to a request if the current request comes from the correspondig browser tab.If everything about this sounds agreeable, I'd be happy to try to take a stab at implementing this and creating a PR.
Beta Was this translation helpful? Give feedback.
All reactions