Mock Service Worker - Overview
Mock Service Worker (MSW) is a library that helps you mock HTTP requests in your tests. We can use it to define request handlers to respond different mock data mainly for testing. Let’s see how to use MSW in browser environment. Installation Install msw package bun install msw Setup Create a index.js which will be used to setup MSW and ship to browser. import { HttpResponse, http } from "msw"; import { setupWorker } from "msw/browser"; // handlers const userHandler = () => { return HttpResponse.json({ id: "abc-123", firstName: "John", lastName: "Maverick", }); }; // handler list used by worker const handlers = [http.get("/user", userHandler)]; // setup worker const worker = setupWorker(...handlers); worker.start(); We can build this bundle and use it in browser environment. ...