Skip to content

Commit 5a78947

Browse files
authored
Merge pull request #2624 from modernweb-dev/fix/mocks-timing-issue
fix: mocks timing issue
2 parents 9679dbd + 1790685 commit 5a78947

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/mocks': patch
3+
---
4+
5+
workaround for MSW breaking change

packages/mocks/browser.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { _registerMockRoutes } from './registerMockRoutes.js';
44

55
const bypassServiceWorker = new URL(window.location.href).searchParams.has('bypass-sw');
66
const worker = setupWorker();
7-
worker
7+
const workerPromise = worker
88
.start({
99
serviceWorker: {
1010
url: '__msw_sw__.js',
@@ -19,8 +19,21 @@ worker
1919
console.error(`[MOCKS]: Failed to load Service Worker.
2020
2121
Did you forget to use the mockPlugin in the dev server?`);
22+
return Promise.resolve();
2223
});
2324

25+
/**
26+
* It's unfortunate to override native `fetch`, and you should never do it, and please don't take this
27+
* code as an example. We have to do this here because MSW removed this behavior which was released as
28+
* a breaking change in a minor version https://github.com/mswjs/msw/issues/1981
29+
*/
30+
const originalFetch = window.fetch;
31+
window.fetch = async (...args) => {
32+
await workerPromise;
33+
window.fetch = originalFetch;
34+
return window.fetch(...args);
35+
};
36+
2437
/**
2538
* Mock the given mocked routes using a Service Worker.
2639
*

0 commit comments

Comments
 (0)