question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API mocking with MSW fails after upgrade to miniflare 2.2.0

See original GitHub issue

Hey @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:closed
  • Created 2 years ago
  • Comments:19

github_iconTop GitHub Comments

5reactions
mrbbotcommented, Mar 4, 2022

Hey @SupremeTechnopriest! 👋 Unfortunately, you won’t be able to use setGlobalDispatcher inside jest-environment-miniflare. Miniflare’s undici version is imported outside the Jest environment in Node. Jest and Node don’t share a module cache, so trying to import undici 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 global fetch function. Something like… (untested 😅)

const fetchSpy = jest.spyOn(globalThis, "fetch");
fetchSpy.mockImplementation((input, init) => {
  const req = new Request(input, init);
  const url = new URL(req.url);
  if (req.method === "GET" && url.pathname === "/") {
    return new Response("bar");
  }
  return new Response(null, { status: 404 });
});

...

jest.restoreAllMocks();

I’d definitiely like to start a discussion on this though. Maybe a global API like getMiniflareMockAgent() that returned a correctly-setup MockAgent?

4reactions
msaiducarcommented, Jun 1, 2022

@SupremeTechnopriest and @LukePammant, I can confirm that mocking the globalThis.fetch doesn’t work with miniflare.dispatchFetch. We have some fetch() 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found