You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This error happens when a Promise rejects but no `.catch()` handler or `await` is attached to it before the microtask queue flushes. This behavior comes from JavaScript itself and is not specific to Vitest. Learn more in the [Node.js documentation](https://nodejs.org/api/process.html#event-unhandledrejection).
128
+
129
+
A common cause is calling an async function without `await`ing it:
130
+
131
+
```ts
132
+
asyncfunction fetchUser(id) {
133
+
const res =awaitfetch(`/api/users/${id}`)
134
+
if (!res.ok) {
135
+
thrownewError(`User ${id} not found`) // [!code highlight]
136
+
}
137
+
returnres.json()
138
+
}
139
+
140
+
test('fetches user', async () => {
141
+
fetchUser(123) // [!code error]
142
+
})
143
+
```
144
+
145
+
Because `fetchUser()` is not `await`ed, its rejection has no handler and Vitest reports:
146
+
147
+
```
148
+
Unhandled Rejection: Error: User 123 not found
149
+
```
150
+
151
+
### Fix
152
+
153
+
`await` the promise so Vitest can catch the error:
154
+
155
+
```ts
156
+
test('fetches user', async () => {
157
+
awaitfetchUser(123) // [!code ++]
158
+
})
159
+
```
160
+
161
+
If you expect the call to throw, use [`expect().rejects`](/api/expect#rejects):
162
+
163
+
```ts
164
+
test('rejects for missing user', async () => {
165
+
awaitexpect(fetchUser(123)).rejects.toThrow('User 123 not found')
0 commit comments