MSW with the same API for Node.js and browsers
npm install msw isomorphic-msw
This example shows a Vitest test that uses isomorphic-msw
and can be run under
Vitest browser mode and/or Node.js-based Vitest test environments without
having to switch which MSW API is used:
import { setupServerOrWorker } from "isomorphic-msw";
import { http, HttpResponse } from "msw";
import { afterAll, beforeAll, beforeEach, expect, it } from "vitest";
const server = setupServerOrWorker();
beforeAll(async () => {
await server.start({
worker: {
quiet: true,
onUnhandledRequest: "bypass",
},
});
});
beforeEach(() => {
server.resetHandlers();
});
afterAll(async () => {
server.stop();
});
it("responds to requests", async () => {
server.use(
http.get("/greet", ({ request }) => {
const url = new URL(request.url);
const name = url.searchParams.get("name") ?? "";
return HttpResponse.json({ greeting: `Hello ${name}` });
}),
);
const response = await fetch("/greet?name=MSW");
expect(response.ok).toBe(true);
expect(await response.json()).toEqual({ greeting: "Hello MSW" });
});
See the reference documentation for a list of all functions and their signatures.