API mocking with MSW fails after upgrade to miniflare 2.2.0
See original GitHub issueHey @mrbbot, thanks for all the work on miniflare, it’s been a game-changer!
With miniflare 1, I’ve been using mock service worker to mock API responses. When attempting to upgrade to miniflare 2.2.0, my tests failed. I’ve no idea where to start looking for a solution, so I’ve put up a fairly minimal demo, hoping that’ll make it easier to pinpoint the cause. HEAD is using miniflare 2.2.0, while the miniflare-1.4.1 branch shows what it looks like when it’s working. Running npm test
should show failing tests for HEAD and passing for the latter.
Issue Analytics
- State:
- Created 2 years ago
- Comments:19
Top Results From Across the Web
Debugging uncaught requests - Recipes - Mock Service Worker
The library comes with a built-in mechanism to react to unhandled requests. You can configure the onUnhandledRequest option of your worker/ ...
Read more >A Comprehensive Guide to Mock Service Worker (MSW)
Discover our comprehensive guide to Mock Service Worker (MSW) and use the API mocking tool for testing, development, debugging to deliver ...
Read more >Chapter 3.2.2.2: Using Mock Service Worker with Vitest
So MSW enables us to avoid using vi.mock() to mock parts of our ... The abstraction makes it easier to mock API endpoints...
Read more >mock service worker, sad path test failure - Stack Overflow
Fixing the mock server. The problem is that the collect function is expecting a JSON response even in case of an error, but...
Read more >java/util/HashMap->keySet()Ljava/util/Set; - Zhkl0228/Unidbg
Issue Title Created Date Comment Count Updated Date
Auto dep install breaks build 6 2022‑08‑05 2022‑08‑10
Does it need tf btwn IMU and GNSS device...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @SupremeTechnopriest! 👋 Unfortunately, you won’t be able to use
setGlobalDispatcher
insidejest-environment-miniflare
. Miniflare’sundici
version is imported outside the Jest environment in Node. Jest and Node don’t share a module cache, so trying to importundici
inside Jest will always give you a different version.Currently, I think the best option for
jest-environment-miniflare
is to use Jest’s function mocks to mock the globalfetch
function. Something like… (untested 😅)I’d definitiely like to start a discussion on this though. Maybe a global API like
getMiniflareMockAgent()
that returned a correctly-setupMockAgent
?@SupremeTechnopriest and @LukePammant, I can confirm that mocking the
globalThis.fetch
doesn’t work withminiflare.dispatchFetch
. We have somefetch()
calls to some external APIs inside our worker, we wanted to test those calls, but spying fetch was not possible. We used this library to workaround. We start a local HTTP server and use this server instead of external APIs.