Mock outbound requests

Last updated Oct 8, 2026 Use @msw/cloudflare ↗︎ to mock outbound HTTP and WebSocket requests with @cloudflare/vitest-plugin. The integration supports unit tests that call your Worker's exported handler and integration tests that call exports.default.fetch().

Install dependencies

Install Mock Service Worker (MSW) version 3 or later and the Cloudflare integration:

npmyarnpnpmbun

npm i -D msw@^3.0.0 @msw/cloudflare
yarn add -D msw@^3.0.0 @msw/cloudflare
pnpm add -D msw@^3.0.0 @msw/cloudflare
bun add -d msw@^3.0.0 @msw/cloudflare

Create a network mock

Create a shared network mock for your tests:

test/network.jsjs

import { setupNetwork } from "@msw/cloudflare";

export const network = setupNetwork();

test/network.tsts

import { setupNetwork } from "@msw/cloudflare";

export const network = setupNetwork();

In a Vitest setup file, start the mock before tests, reset handlers after each test, and stop it after tests finish:

test/setup.jsjs

import { afterAll, afterEach, beforeAll } from "vitest";
import { network } from "./network";

beforeAll(() => network.enable());
afterEach(() => network.resetHandlers());
afterAll(() => network.disable());

test/setup.tsts

import { afterAll, afterEach, beforeAll } from "vitest";
import { network } from "./network";

beforeAll(() => network.enable());
afterEach(() => network.resetHandlers());
afterAll(() => network.disable());

Add the setup file to the setupFiles array in your Vitest configuration.

Mock an HTTP request

Use network.use() and MSW request handlers to return a response for an outbound request. This example tests a Worker that requests a greeting from an external API:

src/index.jsjs

export default {
    async fetch() {
        return fetch("https://api.example.com/greeting");
    },
};

src/index.tsts

export default {
    async fetch(): Promise<Response> {
        return fetch("https://api.example.com/greeting");
    },
} satisfies ExportedHandler;

test/worker.test.jsjs

import {
    createExecutionContext,
    waitOnExecutionContext,
} from "cloudflare:test";
import { env } from "cloudflare:workers";
import { http, HttpResponse } from "msw";
import { expect, it } from "vitest";
import worker from "../src";
import { network } from "./network";

it("mocks an outbound request", async () => {
    network.use(
        http.get("https://api.example.com/greeting", () => {
            return HttpResponse.json({ message: "Hello" });
        }),
    );

    const ctx = createExecutionContext();
    const response = await worker.fetch(
        new Request("https://example.com"),
        env,
        ctx,
    );
    await waitOnExecutionContext(ctx);
    expect(await response.json()).toEqual({ message: "Hello" });
});

test/worker.test.tsts

import {
    createExecutionContext,
    waitOnExecutionContext,
} from "cloudflare:test";
import { env } from "cloudflare:workers";
import { http, HttpResponse } from "msw";
import { expect, it } from "vitest";
import worker from "../src";
import { network } from "./network";

it("mocks an outbound request", async () => {
    network.use(
        http.get("https://api.example.com/greeting", () => {
            return HttpResponse.json({ message: "Hello" });
        }),
    );

    const ctx = createExecutionContext();
    const response = await worker.fetch(
        new Request("https://example.com"),
        env,
        ctx,
    );
    await waitOnExecutionContext(ctx);
    expect(await response.json()).toEqual({ message: "Hello" });
});

Mock an outbound WebSocket

Use MSW's ws.link() API to mock a WebSocket connection created by your Worker. The request-mocking fixture ↗︎ includes HTTP, exports.default.fetch(), and WebSocket examples.

Was this helpful?

YesNo

On this page

Mock outbound requests - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/workers/testing/vitest-integration/mock-outbound-requests/#page","headline":"Mock outbound requests","description":"Mock outbound HTTP and WebSocket requests when testing Workers with the Vitest plugin.","url":"https://developers.cloudflare.com/workers/testing/vitest-integration/mock-outbound-requests/","inLanguage":"en","image":"https://developers.cloudflare.com/workers/testing/vitest-integration/mock-outbound-requests/og.png?v=953487ab876c21e0","dateModified":"2026-10-08","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Examples that demonstrate how to write unit and integration tests with the Workers Vitest integration. title: Recipes and examples image: https://developers.cloudflare.com/workers/testing/vitest-integration/recipes/og.png?v=6f206be55a8b1f98


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/workers/llms.txt
Use this file to discover all available pages before exploring further.