Skip to content

Commit de6ab45

Browse files
committed
docs(en): merging all conflicts
2 parents b60891e + 533b7d4 commit de6ab45

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

api/browser/locators.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@ outline: [2, 3]
1313
本页介绍了 API 的使用。为了更好地了解定位器及其用法,请阅读 [Playwright 的“定位器”文档](https://playwright.dev/docs/locators)
1414
:::
1515

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.
24+
25+
```ts
26+
import { expect } from 'vitest'
27+
import { page } from 'vitest/browser'
28+
29+
const deleteButton = page
30+
.getByRole('row')
31+
.filter({ hasText: 'Vitest' })
32+
.getByRole('button', { name: /delete/i })
33+
34+
await deleteButton.click()
35+
await expect.element(deleteButton).toBeEnabled()
36+
```
37+
:::
38+
1639
## getByRole
1740

1841
```ts

guide/common-errors.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,47 @@ vitest --pool=forks
123123
```
124124

125125
:::
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+
async function fetchUser(id) {
135+
const res = await fetch(`/api/users/${id}`)
136+
if (!res.ok) {
137+
throw new Error(`User ${id} not found`) // [!code highlight]
138+
}
139+
return res.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+
await fetchUser(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+
await expect(fetchUser(123)).rejects.toThrow('User 123 not found')
168+
})
169+
```

guide/recipes.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineConfig({
1515
test: {
1616
projects: [
1717
{
18+
<<<<<<< HEAD
1819
// 禁用隔离的单元测试
1920
name: 'Unit tests',
2021
isolate: false,
@@ -24,6 +25,21 @@ export default defineConfig({
2425
// 集成隔离的测试
2526
name: 'Integration tests',
2627
include: ['**.integration.test.ts'],
28+
=======
29+
test: {
30+
// Non-isolated unit tests
31+
name: 'Unit tests',
32+
isolate: false,
33+
exclude: ['**.integration.test.ts'],
34+
},
35+
},
36+
{
37+
test: {
38+
// Isolated integration tests
39+
name: 'Integration tests',
40+
include: ['**.integration.test.ts'],
41+
},
42+
>>>>>>> 533b7d49f571f3024e559dee4f55f67d7212a838
2743
},
2844
],
2945
},
@@ -41,13 +57,17 @@ export default defineConfig({
4157
test: {
4258
projects: [
4359
{
44-
name: 'Parallel',
45-
exclude: ['**.sequential.test.ts'],
60+
test: {
61+
name: 'Parallel',
62+
exclude: ['**.sequential.test.ts'],
63+
},
4664
},
4765
{
48-
name: 'Sequential',
49-
include: ['**.sequential.test.ts'],
50-
fileParallelism: false,
66+
test: {
67+
name: 'Sequential',
68+
include: ['**.sequential.test.ts'],
69+
fileParallelism: false,
70+
},
5171
},
5272
],
5373
},

0 commit comments

Comments
 (0)