MSW
Mock Service Worker
import { setupWorker, rest } from 'msw';
const worker = setupWorker(
rest.post("/login", async (req, res, ctx) => {
const { username } = await req.json();
return res(
ctx.json({
username,
firstName: "John",
})
)
})
);
worker.start();
// setupServer
// server.ts
import { setupServer } from "msw/node";
import handlers from "./handlers";
const server = setupServer(...handlers);
export default server;
// handlers.ts
import { rest } from "msw";
import * as fixtures from "@/fixtures";
const handlers = [
rest.get("/categories", (req, res, ctx) =>
res(ctx.json({ categories: fixtures.categories }))
),
rest.get("/products", (req, res, ctx) => {
const categoryId = req.url.searchParams.get("categoryId");
if (categoryId) {
return res(
ctx.json({
products: fixtures.products.filter(
(product) => product.category.id === (categoryId as string)
),
})
);
}
return res(ctx.json({ products: fixtures.products }));
})]Jest용 세팅
Last updated