Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f8d1d1c
docs(cn): translate comments and missing content
WuMingDao Jan 1, 2026
313eb80
fix test-context
WuMingDao Jan 4, 2026
074e92a
Merge branch 'vitest-dev:dev' into dev
WuMingDao Jan 4, 2026
a2f7bb5
fix: test-context next
WuMingDao Jan 4, 2026
daa5014
docs(cn): update api/advanced/plugin
NoiseFan Jan 5, 2026
0e915c6
docs(cn): update api/advanced/plugin
NoiseFan Jan 5, 2026
7c5d1d5
docs(cn): update /api/expect-typeof
NoiseFan Jan 5, 2026
eda0496
update(cn): update api/browser/interactivity.md
NoiseFan Jan 6, 2026
1d3664c
docs(cn): update api/advanced/vitest
NoiseFan Jan 6, 2026
d5dc8f5
docs(cn): update /api/index
NoiseFan Jan 7, 2026
0624418
Merge branch 'dev' into pr-test-context-fix
NoiseFan Jan 7, 2026
d966f0a
docs(cn): update guide/browser/component-testing
NoiseFan Jan 7, 2026
af18bc9
docs(cn): update guide/profiling-test-performance
NoiseFan Jan 7, 2026
7c5062b
docs(cn): update api/browser/locators
NoiseFan Jan 8, 2026
6349264
Merge branch 'dev' into pr-test-context-fix
NoiseFan Jan 9, 2026
1995a51
docs(cn): update api/expect
NoiseFan Jan 8, 2026
46bc338
docs(cn): update guide/parallelism
NoiseFan Jan 11, 2026
80b43fe
docs(cn): update config/resolvesnapshotpath
NoiseFan Jan 11, 2026
672de47
docs(cn): update guide/browser/index.md
NoiseFan Jan 11, 2026
5dfb152
docs(cn): update guide/comparisons.md
NoiseFan Jan 11, 2026
1a45248
docs(cn): update guide/comparisons.md
NoiseFan Jan 11, 2026
42590c7
docs(cn): update guide/mocking/modules.md
NoiseFan Jan 11, 2026
69d6545
docs(cn): update guide/browser/component-testing.md
NoiseFan Jan 11, 2026
29f59d3
docs(cn): update guide/features.md
NoiseFan Jan 11, 2026
f4dddd8
docs(cn): update guide/migration.md
NoiseFan Jan 11, 2026
64e55aa
docs(cn): update config/browser.md
NoiseFan Jan 13, 2026
7bd31ff
Merge branch 'dev' into pr-test-context-fix
NoiseFan Jan 14, 2026
eb17e3f
Merge branch 'dev' into pr-test-context-fix
NoiseFan Jan 14, 2026
e1406ed
Merge branch 'dev' into pr-test-context-fix
NoiseFan Jan 14, 2026
57b2932
docs(cn): update config/experimental.md
NoiseFan Jan 14, 2026
60d3f13
docs(cn): update api/advanced/test-case.md
NoiseFan Jan 18, 2026
03c27dc
docs(cn): update config/onstacktrace.md
NoiseFan Jan 18, 2026
4e12a2f
docs(cn): update guide/browser/visual-regression-testing.md
NoiseFan Jan 18, 2026
9622f19
docs(cn): update guide/test-context.md
NoiseFan Jan 18, 2026
4c42b60
docs(cn): update guide/open-telemetry.md
NoiseFan Jan 18, 2026
fca17ef
Merge remote-tracking branch 'upstream/dev' into pr-test-context-fix
NoiseFan Jan 18, 2026
415f7f2
Merge branch 'dev' into pr-test-context-fix
NoiseFan Jan 18, 2026
a17070b
docs(cn): update
NoiseFan Jan 18, 2026
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
4 changes: 2 additions & 2 deletions .vitepress/components/Advanced.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<Badge type="danger" title="This is an advanced API intended for library authors and framework integrations. Most users should not need this." class="advanced-badge">
advanced
<Badge type="danger" title="这是一个面向库作者和框架集成的进阶 API,大多数用户无需使用此功能。" class="advanced-badge">
高级 API
</Badge>
</template>

Expand Down
2 changes: 1 addition & 1 deletion .vitepress/components/Experimental.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<Badge type="warning" title="This feature is experimental and does not follow SemVer." class="experimental-badge">
experimental
实验性
</Badge>
</template>

Expand Down
13 changes: 6 additions & 7 deletions api/advanced/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ vitest.config.project.push('my-project-name')
请注意,这也将继承 `name` - Vitest 不允许多个项目使用相同的名称,因此这将引发错误。请确保我们指定了不同的名称。我们可以通过 `project.name` 属性访问当前名称,并且所有使用的名称都可以在 `vitest.projects` 数组中找到。
:::

<!-- TODO: translation -->
### experimental_defineCacheKeyGenerator <Version type="experimental">4.0.11</Version> <Experimental /> {#definecachekeygenerator}

```ts
Expand All @@ -139,11 +138,11 @@ function experimental_defineCacheKeyGenerator(
): void
```

Define a generator that will be applied before hashing the cache key.
定义一个缓存键生成器,它将在缓存键哈希之前运行。

Use this to make sure Vitest generates correct hash. It is a good idea to define this function if your plugin can be registered with different options.
如果你的插件支持通过不同的参数选项注册,建议通过这种方式,确保 Vitest 生成正确的哈希值。

This is called only if [`experimental.fsModuleCache`](/config/experimental#experimental-fsmodulecache) is defined.
仅当定义了 [`experimental.fsModuleCache`](/config/experimental#experimental-fsmodulecache) 时才会调用此方法。

```ts
interface PluginOptions {
Expand All @@ -162,13 +161,13 @@ export function plugin(options: PluginOptions) {
},
configureVitest({ experimental_defineCacheKeyGenerator }) {
experimental_defineCacheKeyGenerator(() => {
// since these options affect the transform result,
// return them together as a unique string
// 由于这些选项会影响转换结果,
// 将它们组合成一个唯一字符串并返回
return options.replacePropertyKey + options.replacePropertyValue
})
}
}
}
```

If `false` is returned, the module will not be cached on the file system.
如果返回 `false`,模块将不会缓存在文件系统上。
7 changes: 3 additions & 4 deletions api/advanced/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ onTestAnnotate 是与 [`context.annotate`](/guide/test-context#annotate) 方法

如果在注解中指定了文件路径, Vitest 会将附件保存到一个独立的目录(该目录通过 [`attachmentsDir`](/config/#attachmentsdir) 配置),并自动更新 path 属性,使其指向存储后的文件位置。

<!-- TODO: translation -->
## onTestCaseArtifactRecord <Version type="experimental">4.0.11</Version> {#ontestcaseartifactrecord}

```ts
Expand All @@ -332,8 +331,8 @@ function onTestCaseArtifactRecord(
): Awaitable<void>
```

The `onTestCaseArtifactRecord` hook is associated with the [`recordArtifact`](/api/advanced/artifacts#recordartifact) utility. When `recordArtifact` is invoked, Vitest serialises it and sends the same attachment to the main thread where reporter can interact with it.
`onTestCaseArtifactRecord` 钩子与 [`recordArtifact`](/api/advanced/artifacts#recordartifact) 工具相关联。当调用 `recordArtifact` 时,Vitest 会将其序列化并将相同的附件发送到主线程,报告器可以在那里与其交互。

If the path is specified, Vitest stores it in a separate directory (configured by [`attachmentsDir`](/config/#attachmentsdir)) and modifies the `path` property to reference it.
如果指定了路径,Vitest 会将其存储在单独的目录中(由 [`attachmentsDir`](/config/#attachmentsdir) 配置),并修改 `path` 属性以引用它。

Note: annotations, [even though they're built on top of this feature](/api/advanced/artifacts#relationship-with-annotations), won't hit this hook and won't appear in the `task.artifacts` array for backwards compatibility reasons until the next major version.
注意:注解,[即使它们是基于此功能构建的](/api/advanced/artifacts#relationship-with-annotations),出于向后兼容性原因,在下一个主要版本之前不会触发此钩子,也不会出现在 `task.artifacts` 数组中。
14 changes: 3 additions & 11 deletions api/advanced/test-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,27 +265,19 @@ interface TestDiagnostic {
::: info
如果测试尚未被安排运行,`diagnostic()` 将返回 `undefined`。
:::
<!-- TODO: translation -->

## annotations

```ts
function annotations(): ReadonlyArray<TestAnnotation>
```

[Test annotations](/guide/test-annotations) added via the [`task.annotate`](/guide/test-context#annotate) API during the test execution.
通过 [`task.annotate`](/guide/test-context#annotate) API,在测试执行过程中添加的 [测试注释](/guide/test-annotations)。

## artifacts <Version type="experimental">4.0.11</Version> <Experimental /> {#artifacts}

```ts
function artifacts(): ReadonlyArray<TestArtifact>
```

[Test artifacts](/api/advanced/artifacts) recorded via the `recordArtifact` API during the test execution.

## toTestSpecification <Version>4.1.0</Version> {#totestspecification}

```ts
function toTestSpecification(): TestSpecification
```

Returns a new [test specification](/api/advanced/test-specification) that can be used to filter or run this specific test case.
通过 `recordArtifact` API,在测试执行过程中记录的 [测试产物](/api/advanced/artifacts)。
16 changes: 8 additions & 8 deletions api/advanced/test-suite.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ ID 的格式如下:

::: tip
你可以使用 `vitest/node` 中的 `generateFileHash` 函数生成文件哈希,该函数自 Vitest 3 起可用:
<!-- TODO: translation -->

```ts
import { generateFileHash } from 'vitest/node'

const hash = generateFileHash(
'/file/path.js', // relative path
undefined // the project name or `undefined` is not set
'/file/path.js', // 相对路径
undefined // 项目名称,如果未设置则为 `undefined`
)
```

Expand Down Expand Up @@ -199,18 +199,18 @@ describe('collection failed', () => {
function meta(): TaskMeta
```
在执行或收集过程中附加到套件的自定义[元数据](/api/advanced/metadata)。在测试运行期间,可以通过向 `task.meta` 对象分配属性来附加 meta:
<!-- TODO: translation -->
```ts {7,12}

```ts {6,11}
import { describe, test, TestRunner } from 'vitest'

describe('the validation works correctly', () => {
// assign "decorated" during collection
// 在收集期间分配 "decorated"
const { suite } = TestRunner.getCurrentSuite()
suite!.meta.decorated = true

test('some test', ({ task }) => {
// 在试运行期间指定 “decorated,它将可用
// 仅在 onTestCaseReady hook
// 在测试运行期间分配 "decorated",它将可用
// 仅在 onTestCaseReady hook
task.suite.meta.decorated = false
})
})
Expand Down
16 changes: 8 additions & 8 deletions api/advanced/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ function onCancel(fn: (reason: CancelReason) => Awaitable<void>): () => void

注册一个处理程序,当测试运行被 [`vitest.cancelCurrentRun`](#cancelcurrentrun) 取消时调用。

::: warning EXPERIMENTAL
Since 4.0.10, `onCancel` returns a teardown function that will remove the listener.
::: warning 实验性
4.0.10 起,`onCancel` 会返回一个用于移除监听器的清理函数。
:::

## onClose
Expand Down Expand Up @@ -628,16 +628,16 @@ function experimental_parseSpecifications(
}
): Promise<TestModule[]>
```
<!-- TODO: translation -->
This method will [collect tests](#parsespecification) from an array of specifications. By default, Vitest will run only `os.availableParallelism()` number of specifications at a time to reduce the potential performance degradation. You can specify a different number in a second argument.

此方法将从规范数组中 [收集测试用例](#parsespecification)。默认情况下,Vitest 每次仅会并行运行 `os.availableParallelism()` 数量的规范,以降低潜在的性能损耗。你可以通过第二个参数指定不同的并发数量。

## experimental_clearCache <Version type="experimental">4.0.11</Version> <Experimental /> {#clearcache}

```ts
function experimental_clearCache(): Promise<void>
```

Deletes all Vitest caches, including [`experimental.fsModuleCache`](/config/experimental#experimental-fsmodulecache).
删除所有 Vitest 缓存,包括 [`experimental.fsModuleCache`](/config/experimental#experimental-fsmodulecache)

## experimental_getSourceModuleDiagnostic <Version type="experimental">4.0.15</Version> <Experimental /> {#getsourcemodulediagnostic}

Expand All @@ -648,7 +648,7 @@ export function experimental_getSourceModuleDiagnostic(
): Promise<SourceModuleDiagnostic>
```

::: details Types
::: details 类型
```ts
export interface ModuleDefinitionLocation {
line: number
Expand Down Expand Up @@ -690,8 +690,8 @@ export interface SourceModuleDiagnostic {
```
:::

Returns module's diagnostic. If [`testModule`](/api/advanced/test-module) is not provided, `selfTime` and `totalTime` will be aggregated across all tests that were running the last time. If the module was not transformed or executed, the diagnostic will be empty.
返回模块的诊断信息。如果未提供 [`testModule`](/api/advanced/test-module),则 `selfTime` `totalTime` 将聚合上次运行的所有测试。如果模块未被转换或执行,诊断信息将为空。

::: warning
At the moment, the [browser](/guide/browser/) modules are not supported.
[浏览器模式](/guide/browser/) 暂不支持。
:::
23 changes: 11 additions & 12 deletions api/browser/interactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: 交互性 API | 浏览器模式
---

# 交互性 API
# 交互性 API {#interactivity-api}

Vitest 使用 [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/) 或 [webdriver](https://www.w3.org/TR/webdriver/) 实现了 [`@testing-library/user-event`](https://testing-library.com/docs/user-event/intro) 库的子集 API,而不是伪造事件,这使得浏览器行为更加可靠和一致,符合用户与页面交互的方式。

Expand Down Expand Up @@ -61,28 +61,27 @@ test('clicks on an element', async () => {
// 或者你可以直接从定位器上访问
await logo.click()

// With WebdriverIO, this uses either ElementClick (with no arguments) or
// actions (with arguments). Use an empty object to force the use of actions.
// WebdriverIO 中,该方法根据参数情况使用 ElementClick(无参数时)或行为链(有参数时)
// 传入空对象可以强制使用行为链
await logo.click({})
})
```
<!-- TODO: translation -->
### Clicking with a modifier

With either WebdriverIO or Playwright:
### 使用修饰键点击 {#clicking-with-a-modifier}

使用 WebdriverIO 或 Playwright:

```ts
await userEvent.keyboard('{Shift>}')
// By using an empty object as the option, this opts in to using a chain of actions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a chain of actionsselenium 里面的名词,应翻译为 行为链

// instead of an ElementClick in webdriver.
// Firefox has a bug that makes this necessary.
// Follow https://bugzilla.mozilla.org/show_bug.cgi?id=1456642 to know when this
// will be fixed.
// 通过传入空对象作为选项参数,该方法强制会选择使用行为链
// 而非 webdriver 的原生 ElementClick
// 由于 Firefox 存在一个 bug ,所以必须使用这种方式
// 关注 https://bugzilla.mozilla.org/show_bug.cgi?id=1456642 以获取该问题的修复进展
await userEvent.click(element, {})
await userEvent.keyboard('{/Shift}')
```

With Playwright:
使用 Playwright
```ts
await userEvent.click(element, { modifiers: ['Shift'] })
```
Expand Down
25 changes: 12 additions & 13 deletions api/browser/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,26 @@ function getByLabelText(
下方示例中,`page.getByLabelText('Username')` 会一次性选中所有相关输入框。

```html
// for/htmlFor relationship between label and form element id
// for/htmlFor 标签与表单元素 ID 的关系
<label for="username-input">Username</label>
<input id="username-input" />

// The aria-labelledby attribute with form elements
// 使用 aria-labelledby 属性与表单元素
<label id="username-label">Username</label>
<input aria-labelledby="username-label" />

// Wrapper labels
// 包裹式标签
<label>Username <input /></label>

// Wrapper labels where the label text is in another child element
// 标签文本位于其他子元素中的包裹式标签
<label>
<span>Username</span>
<input />
</label>

// aria-label attributes
// Take care because this is not a label that users can see on the page,
// so the purpose of your input must be obvious to visual users.
// aria-label 属性
// 注意:这不是用户在页面上可见的标签
// 因此输入的目的必须对视觉障碍用户显而易见。
<input aria-label="Username" />
```

Expand Down Expand Up @@ -801,7 +801,7 @@ function screenshot(options?: LocatorScreenshotOptions & { base64?: false }): Pr
你可以使用 `path` 选项指定屏幕截图的保存位置,该选项相对于当前测试文件。如果未设置 `path` 选项,Vitest 将默认使用 [`browser.screenshotDirectory`](/config/browser/screenshotdirectory)(默认为 `__screenshot__`),并结合文件名和测试名来确定屏幕截图的文件路径。

如果你还需要屏幕截图的内容,可以指定 `base64: true` 以返回屏幕截图的 base64 编码内容以及保存路径。
<!-- TODO: translation -->

```ts
import { page } from 'vitest/browser'

Expand All @@ -811,10 +811,10 @@ const path = await button.screenshot()

const { path, base64 } = await button.screenshot({
path: './button-click-me.png',
base64: true, // also return base64 string
base64: true, // 同时返回 base64 字符串
})
// path - fullpath to the screenshot
// bas64 - base64 encoded string of the screenshot
// path - 屏幕截图的完整路径
// base64 - 屏幕截图的 base64 编码字符串
```

::: warning WARNING <Version>3.2.0</Version>
Expand Down Expand Up @@ -976,8 +976,7 @@ test('works correctly', async () => {

### length

This getter returns a number of elements that this locator is matching. It is equivalent to calling `locator.elements().length`.
此属性返回当前定位器匹配的元素数量,等效于调用 g `locator.elements().length`。
此属性返回当前定位器匹配的元素数量,等效于调用 `locator.elements().length`。

参考以下 DOM 结构:

Expand Down
11 changes: 5 additions & 6 deletions api/expect-typeof.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,23 +553,22 @@ expectTypeOf(obj).toHaveProperty('b').toBeString()
expectTypeOf(obj).toHaveProperty('a').not.toBeString()
```

<!-- TODO: translation -->
## branded

- **Type:** `ExpectTypeOf<BrandedType>`
- **类型:** `ExpectTypeOf<BrandedType>`

You can use `.branded` to allow type assertions to succeed for types that are semantically equivalent but differ in representation.
你可以使用 `.branded` 来允许那些语义上等价,但表示形式不同的类型断言成功。

```ts
import { expectTypeOf } from 'vitest'

// Without .branded, this fails even though the types are effectively the same
// 没有使用 .branded,即使实际上类型相同,但也会断言失败
expectTypeOf<{ a: { b: 1 } & { c: 1 } }>().toEqualTypeOf<{ a: { b: 1; c: 1 } }>()

// With .branded, the assertion succeeds
// 使用 .branded,断言成功
expectTypeOf<{ a: { b: 1 } & { c: 1 } }>().branded.toEqualTypeOf<{ a: { b: 1; c: 1 } }>()
```

::: warning
This helper comes at a performance cost and can cause the TypeScript compiler to 'give up' if used with excessively deep types. Use it sparingly and only when necessary.
此工具函数会带来性能开销,并且在处理过深类型时可能导致 TypeScript 编译器 “崩溃”。仅在必要时谨慎使用。
:::
Loading