-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsw.setup.ts
98 lines (79 loc) · 1.96 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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.all(`${BASE_URL}/api/echo`, async ({ request }) => {
return HttpResponse.json({
method : request.method,
headers: request.headers,
})
}),
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.all(`${BASE_URL}/api/error/500`, () => {
return HttpResponse.json({}, { status: 500 })
}),
http.get(`${BASE_URL}/api/error/unstable`, async function * () {
let count = 0
while (count < 2) {
yield HttpResponse.json({ data: { count } }, { status: 500 })
count++
}
return HttpResponse.json({ message: 'Pong', data: { count } })
}),
)
beforeAll(() => {
server.listen()
})
beforeEach(() => {
setApi(createApi({ baseURL: BASE_URL }))
})
afterEach(() => {
server.resetHandlers()
})
afterAll(() => {
server.close()
})
process.env.BASE_URL = BASE_URL