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
Copy file name to clipboardExpand all lines: api/browser/locators.md
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,29 @@ outline: [2, 3]
13
13
本页介绍了 API 的使用。为了更好地了解定位器及其用法,请阅读 [Playwright 的“定位器”文档](https://playwright.dev/docs/locators)。
14
14
:::
15
15
16
+
::: tip Difference from `testing-library`
17
+
Vitest's `page.getBy*` methods return a locator object, not a DOM element. This makes locator queries composable and allows Vitest to retry interactions and assertions when needed.
18
+
19
+
Compared to testing-library queries:
20
+
21
+
- Use locator chaining (`.getBy*`, `.filter`, `.nth`) instead of `within(...)`.
22
+
- Keep locators around and interact with them later (`await locator.click()`), instead of resolving elements up front.
23
+
- Single-element escape hatches like `.element()` and `.query()` are strict and throw if multiple elements match.
Copy file name to clipboardExpand all lines: guide/common-errors.md
+44Lines changed: 44 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -123,3 +123,47 @@ vitest --pool=forks
123
123
```
124
124
125
125
:::
126
+
127
+
## Unhandled Promise Rejection
128
+
129
+
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).
130
+
131
+
A common cause is calling an async function without `await`ing it:
132
+
133
+
```ts
134
+
asyncfunction fetchUser(id) {
135
+
const res =awaitfetch(`/api/users/${id}`)
136
+
if (!res.ok) {
137
+
thrownewError(`User ${id} not found`) // [!code highlight]
138
+
}
139
+
returnres.json()
140
+
}
141
+
142
+
test('fetches user', async () => {
143
+
fetchUser(123) // [!code error]
144
+
})
145
+
```
146
+
147
+
Because `fetchUser()` is not `await`ed, its rejection has no handler and Vitest reports:
148
+
149
+
```
150
+
Unhandled Rejection: Error: User 123 not found
151
+
```
152
+
153
+
### Fix
154
+
155
+
`await` the promise so Vitest can catch the error:
156
+
157
+
```ts
158
+
test('fetches user', async () => {
159
+
awaitfetchUser(123) // [!code ++]
160
+
})
161
+
```
162
+
163
+
If you expect the call to throw, use [`expect().rejects`](/api/expect#rejects):
164
+
165
+
```ts
166
+
test('rejects for missing user', async () => {
167
+
awaitexpect(fetchUser(123)).rejects.toThrow('User 123 not found')
0 commit comments