-
-
Notifications
You must be signed in to change notification settings - Fork 476
/
error.node.test.ts
112 lines (96 loc) · 3.14 KB
/
error.node.test.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @vitest-environment node
*/
import { HttpServer } from '@open-draft/test-server/http'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
const httpServer = new HttpServer((app) => {
app.get('/user', (req, res) => {
res.status(200).json({ original: true })
})
app.post('/explicit-return', (req, res) => {
res.status(500).end()
})
app.post('/implicit-return', (req, res) => {
res.status(500).end()
})
})
const server = setupServer()
beforeAll(async () => {
await httpServer.listen()
server.use(
http.get(httpServer.http.url('/user'), () => {
return HttpResponse.json({ mocked: true })
}),
http.post(httpServer.http.url('/explicit-return'), () => {
// Short-circuiting in a handler makes it perform the request as-is,
// but still treats this request as handled.
return
}),
http.post(httpServer.http.url('/implicit-return'), () => {
// The handler that has no return value so it falls through any
// other matching handlers (whicbh are none). In the end,
// the request is performed as-is and is still considered handled.
}),
)
server.listen({ onUnhandledRequest: 'error' })
})
beforeEach(() => {
vi.spyOn(global.console, 'error').mockImplementation(() => void 0)
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0)
})
afterEach(() => {
vi.resetAllMocks()
})
afterAll(async () => {
vi.restoreAllMocks()
server.close()
await httpServer.close()
})
test('errors on unhandled request when using the "error" value', async () => {
const endpointUrl = httpServer.http.url('/')
const makeRequest = () => {
return fetch(endpointUrl)
.then(() => {
throw new Error('Must not resolve')
})
.catch<Error>((error) => {
return error
})
}
const requestError = await makeRequest()
expect(requestError.message).toBe('Failed to fetch')
/**
* @note Undici wraps fetch rejections in a generic "Failed to fetch" error,
* forwarding the actual rejection in the "error.cause" property.
*/
expect(requestError.cause).toEqual(
new Error(
'[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
),
)
expect(console.error)
.toHaveBeenCalledWith(`[MSW] Error: intercepted a request without a matching request handler:
• GET ${endpointUrl}
If you still wish to intercept this unhandled request, please create a request handler for it.
Read more: https://mswjs.io/docs/getting-started/mocks`)
expect(console.warn).not.toHaveBeenCalled()
})
test('does not error on request which handler explicitly returns no mocked response', async () => {
const makeRequest = () => {
return fetch(httpServer.http.url('/explicit-return'), {
method: 'POST',
})
}
await makeRequest()
expect(console.error).not.toHaveBeenCalled()
})
test('does not error on request which handler implicitly returns no mocked response', async () => {
const makeRequest = () => {
return fetch(httpServer.http.url('/implicit-return'), {
method: 'POST',
})
}
await makeRequest()
expect(console.error).not.toHaveBeenCalled()
})