Test workers: how to mock fetch?

Hi,

I am using unstable_dev (and vitest) to test my workers endpoints. It works well until I reach an endpoint that does a call to an external API. I’d like to mock that API to test multiple scenarios. I am trying to mock fetch to handle theses cases.
Mocking the global.fetch works inside the test himself but it does not change fetch implementation inside the worker under test.
Is there a way to fix that ?

beforeAll(async () => {
    global.fetch = vi.fn()
    worker = await unstable_dev('index.js', { disableExperimentalWarning: true });
});

For vitest, there’s miniflare/packages/vitest-environment-miniflare at master · cloudflare/miniflare · GitHub that helps you do it. I use a similar for jest at the moment. You should be able to get MockAgent from udinci and have it configured your way.