handler 示例
handler 示例
import { http, HttpResponse } from 'msw';
export const handlers = [
http.get('/api/user', () => HttpResponse.json({ name: 'alice' })),
];未匹配的请求默认放行到真实网络;把 onUnhandledRequest 设为报错可及时发现漏配接口。
MSW 在网络层拦截请求:浏览器端通过 Service Worker、Node 端通过拦截器,前端开发与测试共用同一套 handler 定义,不需要为测试单独维护一套 mock 层。
import { http, HttpResponse } from 'msw';
export const handlers = [
http.get('/api/user', () => HttpResponse.json({ name: 'alice' })),
];