-
Notifications
You must be signed in to change notification settings - Fork 1
/
msw.setup.ts
79 lines (65 loc) · 1.53 KB
/
msw.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
delay,
http,
HttpResponse,
} from 'msw'
import { setupServer } from 'msw/node'
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
} from 'vitest'
import { setApi, createApi } from './src/core'
const BASE_URL = 'http://localhost:3000'
const server = setupServer(
http.all('*', async () => {
await delay(2)
}),
http.get(`${BASE_URL}/api/ping`, () => {
return HttpResponse.json({ message: 'Pong' })
}),
http.get(`${BASE_URL}/v1/api/ping`, () => {
return HttpResponse.json({ message: 'Pong', data: { version: 'v1' } })
}),
http.get(`${BASE_URL}/v2/api/ping`, () => {
return HttpResponse.json({ message: 'Pong', data: { version: 'v2' } })
}),
http.get(`${BASE_URL}/api/user`, () => {
return HttpResponse.json({ data: 'data-user' })
}),
http.get(`${BASE_URL}/api/error/404`, () => {
return HttpResponse.json({
code : 404,
details: [],
}, { status: 404 })
}),
http.get(`${BASE_URL}/api/error/422`, () => {
return HttpResponse.json({
code : 422,
message: 'Validation Error',
details: [
{
type_url: 'type_url',
value : 'base64string',
},
],
}, { status: 422 })
}),
http.get(`${BASE_URL}/api/error/500`, () => {
return new HttpResponse(undefined, { status: 500 })
}),
)
beforeAll(() => {
server.listen()
})
beforeEach(() => {
setApi(createApi({ baseURL: BASE_URL }))
})
afterEach(() => {
server.resetHandlers()
})
afterAll(() => {
server.close()
})
process.env.BASE_URL = BASE_URL