Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,7 @@ test('assert.notTypeOf', () => {
```ts
import { assert, test } from 'vitest'

function Person(name) {
this.name = name
}
function Person(name) { this.name = name }
const foo = new Person('foo')

class Tea {
Expand All @@ -667,9 +665,7 @@ test('assert.instanceOf', () => {
```ts
import { assert, test } from 'vitest'

function Person(name) {
this.name = name
}
function Person(name) { this.name = name }
const foo = new Person('foo')

class Tea {
Expand Down Expand Up @@ -700,11 +696,7 @@ import { assert, test } from 'vitest'
test('assert.include', () => {
assert.include([1, 2, 3], 2, 'array contains value')
assert.include('foobar', 'foo', 'string contains substring')
assert.include(
{ foo: 'bar', hello: 'universe' },
{ foo: 'bar' },
'object contains property'
)
assert.include({ foo: 'bar', hello: 'universe' }, { foo: 'bar' }, 'object contains property')
})
```

Expand Down Expand Up @@ -811,10 +803,7 @@ import { assert, test } from 'vitest'

test('assert.deepNestedInclude', () => {
assert.deepNestedInclude({ a: { b: [{ x: 1 }] } }, { 'a.b[0]': { x: 1 } })
assert.deepNestedInclude(
{ '.a': { '[b]': { x: 1 } } },
{ '\\.a.\\[b\\]': { x: 1 } }
)
assert.deepNestedInclude({ '.a': { '[b]': { x: 1 } } }, { '\\.a.\\[b\\]': { x: 1 } })
})
```

Expand All @@ -829,10 +818,7 @@ import { assert, test } from 'vitest'

test('assert.notDeepNestedInclude', () => {
assert.notDeepNestedInclude({ a: { b: [{ x: 1 }] } }, { 'a.b[0]': { y: 1 } })
assert.notDeepNestedInclude(
{ '.a': { '[b]': { x: 1 } } },
{ '\\.a.\\[b\\]': { y: 2 } }
)
assert.notDeepNestedInclude({ '.a': { '[b]': { x: 1 } } }, { '\\.a.\\[b\\]': { y: 2 } })
})
```

Expand Down
18 changes: 9 additions & 9 deletions api/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ test('element exists', async () => {
```

::: warning
`expect.poll` 使每个断言变为异步,因此我们需要等待它。自 Vitest 3 起,如果我们忘记等待,测试将以警告失败,提示我们需要这样做
`expect.poll` 使每个断言变为异步,因此我们需要等待它。自 Vitest 3 起,如果我们忘记添加 await,测试将以警告失败,测试将失败并显示警告提示

`expect.poll` 不适用于多个匹配器:

Expand Down Expand Up @@ -483,10 +483,10 @@ test('stocks are not the same', () => {
对于 `Error` 对象,非可枚举属性如 `name`、`message`、`cause` 和 `AggregateError.errors` 也会进行比较。对于 `Error.cause`,比较是不对称的:

```ts
// success
// 成功
expect(new Error('hi', { cause: 'x' })).toEqual(new Error('hi'))

// fail
// 失败
expect(new Error('hi')).toEqual(new Error('hi', { cause: 'x' }))
```

Expand Down Expand Up @@ -570,7 +570,7 @@ test('toHaveLength', () => {
expect('abc').toHaveLength(3)
expect([1, 2, 3]).toHaveLength(3)

expect('').not.toHaveLength(3) // doesn't have .length of 3
expect('').not.toHaveLength(3) // length 不等于3
expect({ length: 3 }).toHaveLength(3)
})
```
Expand Down Expand Up @@ -642,7 +642,7 @@ import { expect, test } from 'vitest'

test('top fruits', () => {
expect('top fruits include apple, orange and grape').toMatch(/apple/)
expect('applefruits').toMatch('fruit') // toMatch also accepts a string
expect('applefruits').toMatch('fruit') // toMatch 同样支持字符串匹配
})
```

Expand Down Expand Up @@ -690,7 +690,7 @@ test('invoice has john personal details', () => {
})

test('the number of elements must match exactly', () => {
// Assert that an array of object matches
// 断言对象数组的匹配情况
expect([{ foo: 'bar' }, { baz: 1 }]).toMatchObject([
{ foo: 'bar' },
{ baz: 1 },
Expand Down Expand Up @@ -1071,7 +1071,7 @@ test('first call of spy function called with right params', () => {

- **类型**: `() => Awaitable<void>`

这个断言检查函数是否至少成功返回了一次值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`。
这个断言检查函数是否至少成功返回了一次值(即没有抛出错误)。需要将一个 spy 函数传递给 `expect`。

```ts
import { expect, test, vi } from 'vitest'
Expand All @@ -1095,7 +1095,7 @@ test('spy function returned a value', () => {

- **类型**: `(amount: number) => Awaitable<void>`

这个断言检查函数是否在确切的次数内成功返回了值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`。
这个断言检查函数是否在确切的次数内成功返回了值(即没有抛出错误)。需要将一个 spy 函数传递给 `expect`。

```ts
import { expect, test, vi } from 'vitest'
Expand Down Expand Up @@ -1170,7 +1170,7 @@ test('spy function returns bananas on second call', () => {

- **类型**: `() => Awaitable<void>`

这个断言检查函数是否至少一次成功地解析了一个值( i.e. ,没有被拒绝)。需要将一个 spy 函数传递给 `expect`。
这个断言检查函数是否至少一次成功地解析了一个值(即没有被拒绝)。需要将一个 spy 函数传递给 `expect`。

如果函数返回了一个 promise ,但它还没有被解决,这个断言将会失败。

Expand Down
19 changes: 10 additions & 9 deletions config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { defineConfig } from 'vite'

export default defineConfig({
test: {
// ... 在此指定选项。
// 在此配置选项...
},
})
```
Expand All @@ -34,7 +34,7 @@ import { defineConfig } from 'vite'

export default defineConfig({
test: {
// ... 在此指定选项。
// 在此配置选项...
},
})
```
Expand All @@ -46,7 +46,7 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
// ... 在此指定选项。
// 在此配置选项...
},
})
```
Expand Down Expand Up @@ -448,7 +448,7 @@ Vitest 使用 Vite SSR 基元来运行测试,这有 [一定的缺陷](https://

默认情况下,`vitest` 不显式提供全局 API。如果你更倾向于使用类似 jest 中的全局 API,可以将 `--globals` 选项传递给 CLI 或在配置中添加 `globals: true`。

```ts
```ts [vitest.config.ts]
import { defineConfig } from 'vitest/config'

export default defineConfig({
Expand Down Expand Up @@ -675,10 +675,11 @@ export default defineConfig({
test: {
poolMatchGlobs: [
// all tests in "worker-specific" directory will run inside a worker as if you enabled `--pool=threads` for them,
// "worker-specific" 目录下的所有测试将在 worker 中运行,等效于对这些测试启用 `--pool=threads`
['**/tests/worker-specific/**', 'threads'],
// run all tests in "browser" directory in an actual browser
// "browser" 目录下的所有测试将在真实浏览器中运行
['**/tests/browser/**', 'browser'],
// all other tests will run based on "browser.enabled" and "threads" options, if you didn't specify other globs
// 其余测试将根据 "browser.enabled" "threads" 配置项决定运行环境(未指定其他 glob 规则时)
// ...
],
},
Expand Down Expand Up @@ -756,7 +757,7 @@ export default defineConfig({
- **默认值:** `'default'`
- **命令行终端:** `--reporter=<name>`, `--reporter=<name1> --reporter=<name2>`

自定义 [报告器](/guide/reporters) 输出。报告器可以是 [Reporter 实例](https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/types/reporter.ts)、用于选择内置报告器的字符串,或自定义实现的路径(例如 `./path/to/reporter.ts`、`@scope/reporter`)。
自定义 [报告器](/guide/reporters) 输出。报告器可以是 [一个 Reporter 实例](https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/types/reporter.ts)、用于选择内置报告器的字符串,或自定义实现的路径(例如 `./path/to/reporter.ts`、`@scope/reporter`)。

### outputFile<NonProjectOption />

Expand Down Expand Up @@ -986,7 +987,7 @@ export default defineConfig({
##### poolOptions.vmThreads.memoryLimit<NonProjectOption />

- **类型:** `string | number`
- **命令行终端:** `1 / CPU Cores`
- **命令行终端:** `1 / CPU 核心`

指定工作线程被回收之前的内存限制。该值在很大程度上取决于你的运行环境,因此最好手动指定它,而不是依赖默认值。

Expand Down Expand Up @@ -1068,7 +1069,7 @@ export default defineConfig({
##### poolOptions.vmForks.memoryLimit<NonProjectOption />

- **类型:** `string | number`
- **默认值:** `1 / CPU Cores`
- **默认值:** `1 / CPU 核心`

指定 Worker 被回收前的内存限制。该值在很大程度上取决于环境,因此最好手动指定,而不是依赖默认值。该值的计算方法查看 [`poolOptions.vmThreads.memoryLimit`](#pooloptions-vmthreads-memorylimit)

Expand Down
5 changes: 3 additions & 2 deletions guide/browser/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ export const commands: BrowserCommands
页面导出提供了与当前页面交互的实用程序。

::: warning
虽然它从 Playwright 的 `page` 中获取了一些实用程序,但它与 Playwright 的 `page` 并不是同一个对象。由于浏览器上下文是在浏览器中评估的,您的测试无法访问 Playwright 的 `page`,因为它是在服务器上运行的。
虽然该工具暴露了部分 Playwright 的 `page` 对象实用方法,但两者并非同一对象。由于浏览器上下文在浏览器环境中执行,而 Playwright 的 `page` 对象运行在服务端,因此测试代码无法直接访问该对象。

:::

使用 [Commands API](/guide/browser/commands) 如果您需要访问 Playwright 的 `page` 对象
如需操作 Playwright 的 `page` 对象,请使用 [Commands API](/guide/browser/commands)。
```ts
export const page: {
/**
Expand Down
6 changes: 3 additions & 3 deletions guide/browser/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ npx vitest --browser.headless
在这种情况下,Vitest 将使用 Chrome 浏览器以无头模式运行。

::: warning
默认情况下Headless模式不可用。我们需要使用 [`playwright`](https://npmjs.com/package/playwright) 或 [`webdriverio`](https://www.npmjs.com/package/webdriverio) 提供程序来启用此功能。
默认情况下无头模式不可用。我们需要使用 [`playwright`](https://npmjs.com/package/playwright) 或 [`webdriverio`](https://www.npmjs.com/package/webdriverio) 提供程序来启用此功能。
:::

## 示例 {#examples}
Expand Down Expand Up @@ -426,7 +426,7 @@ test('properly handles form inputs', async () => {
```ts
import { expect } from 'vitest'
import { page } from 'vitest/browser'
// element is rendered correctly
// 元素渲染正确
await expect.element(page.getByText('Hello World')).toBeInTheDocument()
```

Expand All @@ -435,7 +435,7 @@ Vitest 暴露了一个 [上下文 API](/guide/browser/context),其中包含一
```ts
import { page, userEvent } from 'vitest/browser'
await userEvent.fill(page.getByLabelText(/username/i), 'Alice')
// or just locator.fill
// 或使用 locator.fill
await page.getByLabelText(/username/i).fill('Alice')
```

Expand Down
20 changes: 10 additions & 10 deletions guide/browser/interactivity-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function type(

`type` 方法在 [`keyboard`](https://testing-library.com/docs/user-event/keyboard) API 的基础上实现了 `@testing-library/user-event` 的 [`type`](https://testing-library.com/docs/user-event/utility/#type) 工具。

你可以使用此函数向 `input` 、`textarea` 或 `contenteditable` 元素中模拟键盘输入。[它兼容 user-event 提供的 keyboard 语法](https://testing-library.com/docs/user-event/keyboard)。
你可以使用此函数向 `input` 、`textarea` 或 `contenteditable` 元素中模拟键盘输入。它兼容 [user-event 提供的 `keyboard` 语法](https://testing-library.com/docs/user-event/keyboard)。

如果只需按下字符而无需输入,请使用 [`userEvent.keyboard`](#userevent-keyboard) API。

Expand Down Expand Up @@ -337,7 +337,7 @@ function selectOptions(
): Promise<void>
```

The `userEvent.selectOptions` allows selecting a value in a `<select>` element.
`userEvent.selectOptions` 方法支持在 `<select>` 元素中选择指定值。

::: warning
如果 select 元素没有 [`multiple`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#attr-multiple) 属性,Vitest 将只选择数组中的第一个元素。
Expand Down Expand Up @@ -470,7 +470,7 @@ test('can upload a file', async () => {
// 或者你可以直接从定位器上访问
await input.upload(file)

// you can also use file paths relative to the root of the project
// 还可以使用相对于测试文件的文件路径
await userEvent.upload(input, './fixtures/file.png')
})
```
Expand All @@ -495,7 +495,7 @@ function dragAndDrop(
): Promise<void>
```

将源元素拖到目标元素的顶部。不要忘记,源元素的`draggable`属性必须设置为 `true`。
将源元素拖到目标元素的顶部。不要忘记,源元素的 `draggable` 属性必须设置为 `true`。

```ts
import { page, userEvent } from 'vitest/browser'
Expand Down Expand Up @@ -533,15 +533,15 @@ function copy(): Promise<void>
import { page, userEvent } from 'vitest/browser'

test('copy and paste', async () => {
// write to 'source'
// 'source' 输入 'hello'
await userEvent.click(page.getByPlaceholder('source'))
await userEvent.keyboard('hello')

// select and copy 'source'
// 选择并复制 'source'
await userEvent.dblClick(page.getByPlaceholder('source'))
await userEvent.copy()

// paste to 'target'
// 粘贴到 'target'
await userEvent.click(page.getByPlaceholder('target'))
await userEvent.paste()

Expand All @@ -566,15 +566,15 @@ function cut(): Promise<void>
import { page, userEvent } from 'vitest/browser'

test('copy and paste', async () => {
// write to 'source'
// 'source' 输入 'hello'
await userEvent.click(page.getByPlaceholder('source'))
await userEvent.keyboard('hello')

// select and cut 'source'
// 选择并剪切 'source'
await userEvent.dblClick(page.getByPlaceholder('source'))
await userEvent.cut()

// paste to 'target'
// 粘贴到 'target'
await userEvent.click(page.getByPlaceholder('target'))
await userEvent.paste()

Expand Down