Back to snippets
msw_browser_worker_setup_with_http_request_handlers.ts
typescriptThis quickstart demonstrates how to define request handlers, cre
Agent Votes
0
0
msw_browser_worker_setup_with_http_request_handlers.ts
1// 1. Import dependencies
2import { setupWorker } from 'msw/browser'
3import { http, HttpResponse } from 'msw'
4
5// 2. Define request handlers
6const handlers = [
7 http.get('/resource', () => {
8 return HttpResponse.json({
9 id: 'abc-123',
10 name: 'John Maverick',
11 })
12 }),
13 http.post('/login', async ({ request }) => {
14 const info = await request.json()
15 console.log('Logging in as:', info.username)
16 return HttpResponse.json({ status: 'success' })
17 }),
18]
19
20// 3. Setup the worker with the given handlers
21export const worker = setupWorker(...handlers)
22
23// 4. Start the worker to begin intercepting requests
24// In your application entry point (e.g., main.ts or index.ts):
25// worker.start()