mirror of
https://github.com/hanzoai/msw.git
synced 2026-09-24 17:00:34 +00:00
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { HttpResponse, http } from 'msw'
|
|
import { setupServer } from 'msw/node'
|
|
|
|
const server = setupServer(
|
|
http.get('https://test.mswjs.io/book/:bookId', () => {
|
|
return HttpResponse.json({ title: 'Original title' })
|
|
}),
|
|
)
|
|
|
|
beforeAll(() => server.listen())
|
|
afterAll(() => server.close())
|
|
|
|
test('returns a mocked response from the used one-time request handler when restored', async () => {
|
|
server.use(
|
|
http.get(
|
|
'https://test.mswjs.io/book/:bookId',
|
|
() => {
|
|
return HttpResponse.json({ title: 'Overridden title' })
|
|
},
|
|
{ once: true },
|
|
),
|
|
)
|
|
|
|
const firstResponse = await fetch('https://test.mswjs.io/book/abc-123')
|
|
const firstBody = await firstResponse.json()
|
|
expect(firstResponse.status).toBe(200)
|
|
expect(firstBody).toEqual({ title: 'Overridden title' })
|
|
|
|
const secondResponse = await fetch('https://test.mswjs.io/book/abc-123')
|
|
const secondBody = await secondResponse.json()
|
|
expect(secondResponse.status).toBe(200)
|
|
expect(secondBody).toEqual({ title: 'Original title' })
|
|
|
|
server.restoreHandlers()
|
|
|
|
const thirdResponse = await fetch('https://test.mswjs.io/book/abc-123')
|
|
const thirdBody = await thirdResponse.json()
|
|
expect(firstResponse.status).toBe(200)
|
|
expect(thirdBody).toEqual({ title: 'Overridden title' })
|
|
})
|