Skip to main content

Mock outbound requests

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

Create a network mock

Create a shared network mock for your tests:

JavaScriptTypeScript

test/network.js
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:

JavaScriptTypeScript

test/setup.js
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:

JavaScriptTypeScript

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

JavaScriptTypeScript

test/worker.test.js
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.