diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 0c4a7daf3..5a7f5be4f 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,7 +1,7 @@ ### 在提交PR之前,请确保您执行以下操作: -- [ ] 曾阅读过[翻译须知](https://github.com/vitest-dev/docs-cn/issues/391)。 +- [ ] 曾阅读过 [翻译须知](https://github.com/vitest-dev/docs-cn/issues/391)。 - [ ] 检查是否已经有PR以同样的方式解决问题,以避免创建重复。 - [ ] 在此PR中描述正在解决的问题,或引用它解决的问题(例如:`fixes #123`)。 diff --git a/advanced/pool.md b/advanced/pool.md index e38ec1853..a1bf6691c 100644 --- a/advanced/pool.md +++ b/advanced/pool.md @@ -1,4 +1,4 @@ -# 自定义运行池 +# 自定义运行池 advanced {#custom-pool} ::: warning 这是一个高级且非常底层的 API。如果你只是想 [运行测试](/guide/),你可能不需要这个。它主要由库作者使用。 diff --git a/api/advanced/metadata.md b/api/advanced/metadata.md index dfba92250..1d4ef7654 100644 --- a/api/advanced/metadata.md +++ b/api/advanced/metadata.md @@ -1,4 +1,4 @@ -# 任务元数据 高级 +# 任务元数据 高级 {#task-metadata} 如果你正在开发自定义报告器或使用 Vitest Node.js API,你可能会发现将在各种上下文中执行的测试中的数据传递给报告器或自定义 Vitest 处理程序很有用。 @@ -42,7 +42,7 @@ Vitest 使用不同的方法与 Node.js 进程进行通信。 该属性也会出现在每个测试的 `json` 报告中,因此请确保数据可以序列化为 JSON。 -另外,请确保在设置[错误属性](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#error_types)之前序列化它们。 +另外,请确保在设置 [错误属性](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#error_types) 之前序列化它们。 ::: 当测试运行完成时,你还可以从 Vitest 状态获取此信息: diff --git a/api/advanced/plugin.md b/api/advanced/plugin.md index 1b447e17e..c30b38089 100644 --- a/api/advanced/plugin.md +++ b/api/advanced/plugin.md @@ -59,7 +59,7 @@ Vitest 通过 `Vite` namespace 重新导出所有仅 Vite 类型的导入,我 ### project -该插件所属的当前[测试项目](./test-project)。 +该插件所属的当前 [测试项目](./test-project)。 ::: warning 浏览器模式 请注意,如果我们依赖浏览器功能,则 `project.browser` 字段尚未设置。请改用 [`reporter.onBrowserInit`](./reporters#onbrowserinit) 事件。 diff --git a/api/advanced/runner.md b/api/advanced/runner.md index 157e935eb..16d67417f 100644 --- a/api/advanced/runner.md +++ b/api/advanced/runner.md @@ -1,4 +1,4 @@ -# 运行器 API advanced +# 运行器 API advanced {#runner-api} ::: warning 注意 这是高级 API。如果你只需要 [运行测试](/guide/),你可能不需要这个。它主要被库的作者使用。 diff --git a/api/advanced/test-module.md b/api/advanced/test-module.md index 0426f840b..9041508d5 100644 --- a/api/advanced/test-module.md +++ b/api/advanced/test-module.md @@ -66,7 +66,7 @@ describe('the validation works correctly', (task) => { ``` :::tip -如果元数据是在收集过程中附加的(在 `test` 函数之外),那么它将在自定义报告器中的['onTestModuleCollectd'](./reporters#onTestModuleCollected) 挂钩中可用。 +如果元数据是在收集过程中附加的(在 `test` 函数之外),那么它将在自定义报告器中的 ['onTestModuleCollectd'](./reporters#onTestModuleCollected) 挂钩中可用。 ::: ## diagnostic diff --git a/api/advanced/vitest.md b/api/advanced/vitest.md index 6b9f78284..9a45b2f6a 100644 --- a/api/advanced/vitest.md +++ b/api/advanced/vitest.md @@ -78,7 +78,7 @@ const testCase = vitest.state.getReportedEntity(task) // 新 API ## projects -这是一个数组,里面包含了所有 [测试项目](/api/advanced/test-project) ,这些项目是用户自己定义的。如果用户没有显式指定任何项目,那么这个数组中只会包含一个 [根项目](#getrootproject) 。 +这是一个数组,里面包含了所有 [测试项目](/api/advanced/test-project),这些项目是用户自己定义的。如果用户没有显式指定任何项目,那么这个数组中只会包含一个 [根项目](#getrootproject)。 Vitest 会保证这个数组里至少有一个项目可用。如果用户在命令行里通过 --project 参数指定了不存在的项目名称,Vitest 会在创建这个数组前就报错。 diff --git a/api/browser/assertions.md b/api/browser/assertions.md index 477c3ea89..f26c9974a 100644 --- a/api/browser/assertions.md +++ b/api/browser/assertions.md @@ -14,7 +14,7 @@ Vitest 默认提供了一组丰富的 DOM 断言,这些断言源自 [`@testing ``` ::: -浏览器中的测试由于其异步特性,可能会不一致地失败。因此,即使条件延迟(如超时、网络请求或动画),也必须有办法保证断言成功。为此,Vitest 通过 [`expect.poll`](/api/expect#poll)和 `expect.element` API 提供了可重试的断言: +浏览器中的测试由于其异步特性,可能会不一致地失败。因此,即使条件延迟(如超时、网络请求或动画),也必须有办法保证断言成功。为此,Vitest 通过 [`expect.poll`](/api/expect#poll) 和 `expect.element` API 提供了可重试的断言: ```ts import { expect, test } from 'vitest' @@ -433,7 +433,7 @@ function toHaveAccessibleErrorMessage(message?: string | RegExp): Promise 这允许你断言一个元素具有预期的 [可访问错误消息](https://w3c.github.io/aria/#aria-errormessage)。 -你可以传递预期的可访问错误消息的确切字符串。或者,你可以通过传递正则表达式或使用 [`expect.stringContaining`](/api/expect#expect-stringcontaining) 或 [`expect.stringMatching`](/api/expect#expect-stringmatching)来进行部分匹配。 +你可以传递预期的可访问错误消息的确切字符串。或者,你可以通过传递正则表达式或使用 [`expect.stringContaining`](/api/expect#expect-stringcontaining) 或 [`expect.stringMatching`](/api/expect#expect-stringmatching) 来进行部分匹配。 ```html ``` -这允许你断言一个元素具有预期的[可访问名称](https://w3c.github.io/accname/)。例如,它有助于断言表单元素和按钮是否被正确标记。 +这允许你断言一个元素具有预期的 [可访问名称](https://w3c.github.io/accname/)。例如,它有助于断言表单元素和按钮是否被正确标记。 你可以传递预期的可访问名称的确切字符串,或者通过传递正则表达式进行部分匹配,也可以使用 [`expect.stringContaining`](/api/expect#expect-stringcontaining) 或 [`expect.stringMatching`](/api/expect#expect-stringmatching)。 @@ -512,7 +512,7 @@ await expect.element(getByTestId('input-title')).toHaveAccessibleName() function toHaveAttribute(attribute: string, value?: unknown): Promise ``` -这允许你检查给定的元素是否具有某个属性。你还可以选择性地验证该属性是否具有特定的预期值或使用 [`expect.stringContaining`](/api/expect#expect-stringcontaining) 或 [`expect.stringMatching`](/api/expect#expect-stringmatching)进行部分匹配。 +这允许你检查给定的元素是否具有某个属性。你还可以选择性地验证该属性是否具有特定的预期值或使用 [`expect.stringContaining`](/api/expect#expect-stringcontaining) 或 [`expect.stringMatching`](/api/expect#expect-stringmatching) 进行部分匹配。 ```html @@ -948,7 +948,7 @@ await expect.element(getByTestId('switch')).toHaveRole('alert') // ❌ function toHaveSelection(selection?: string): Promise ``` -这允许断言某个元素具有一个[文本选择](https://developer.mozilla.org/en-US/docs/Web/API/Selection)。 +这允许断言某个元素具有一个 [文本选择](https://developer.mozilla.org/en-US/docs/Web/API/Selection)。 这在检查元素内是否选择了文本或部分文本时非常有用。该元素可以是文本类型的输入框、`textarea`,或者是任何包含文本的其他元素,例如段落、`span`、`div` 等。 diff --git a/api/browser/commands.md b/api/browser/commands.md index 3f43b0794..0475e143c 100644 --- a/api/browser/commands.md +++ b/api/browser/commands.md @@ -3,7 +3,7 @@ title: 命令 | 浏览器模式 outline: deep --- -# 命令 +# 命令 {#commands} 命令是一个函数,它调用服务器上的另一个函数并将结果传递回浏览器。Vitest 公开了几个可以在浏览器测试中使用的内置命令。 @@ -59,7 +59,7 @@ expect(input).toHaveValue('a') ``` ::: warning -CDP session仅适用于 `playwright` provider,并且仅在使用 `chromium` 浏览器时有效。有关详细信息,请参阅 playwright 的 [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession)文档。 +CDP session仅适用于 `playwright` provider,并且仅在使用 `chromium` 浏览器时有效。有关详细信息,请参阅 playwright 的 [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) 文档。 ::: ## 自定义命令 {#custom-commands} @@ -133,7 +133,7 @@ Vitest 在命令上下文中公开了几个`playwright`特定属性。 - `page`引用包含测试 iframe 的完整页面。这是协调器 HTML,为避免出现问题,最好不要碰它。 - `frame` 是一个异步方法,用于解析测试器 [`Frame`](https://playwright.dev/docs/api/class-frame)。它的 API 与 `page` 类似,但不支持某些方法。如果您需要查询元素,应优先使用 `context.iframe` 代替,因为它更稳定、更快速。 - `iframe` 是一个 [`FrameLocator`](https://playwright.dev/docs/api/class-framelocator),用于查询页面上的其他元素。 -- `context` 是指唯一的[BrowserContext](https://playwright.dev/docs/api/class-browsercontext)。 +- `context` 是指唯一的 [BrowserContext](https://playwright.dev/docs/api/class-browsercontext)。 ```ts import { BrowserCommand } from 'vitest/node' diff --git a/api/browser/context.md b/api/browser/context.md index 12d649ff1..16c9ba1f0 100644 --- a/api/browser/context.md +++ b/api/browser/context.md @@ -179,7 +179,7 @@ await frame.click() // ❌ 不可用 ::: danger IMPORTANT 目前,`frameLocator` 方法仅支持 `playwright` 提供者。 -交互方法(如 `click` 或 `fill`)在 iframe 内的元素上始终可用,但使用 `expect.element` 进行断言时要求 iframe 具有[同源策略](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)。 +交互方法(如 `click` 或 `fill`)在 iframe 内的元素上始终可用,但使用 `expect.element` 进行断言时要求 iframe 具有 [同源策略](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)。 ::: ## `cdp` @@ -187,7 +187,7 @@ await frame.click() // ❌ 不可用 `cdp` 导出返回当前的 Chrome DevTools 协议会话。它主要用于库作者在其基础上构建工具。 ::: warning -CDP 会话仅适用于 `playwright` provider,并且仅在使用 `chromium` 浏览器时有效。有关详细信息,请参阅 playwright 的 [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession)文档。 +CDP 会话仅适用于 `playwright` provider,并且仅在使用 `chromium` 浏览器时有效。有关详细信息,请参阅 playwright 的 [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) 文档。 ::: ```ts diff --git a/api/browser/interactivity.md b/api/browser/interactivity.md index cdf212112..7640d79e4 100644 --- a/api/browser/interactivity.md +++ b/api/browser/interactivity.md @@ -262,7 +262,7 @@ test('update input', async () => { 该方法聚焦元素、填充元素并在填充后触发一个 `input` 事件。您可以使用空字符串来清除字段。 ::: tip -该 API 比使用 [`userEvent.type`](#userevent-type) 或 [`userEvent.keyboard`](#userevent-keyboard) 更快,但**不支持** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard) (例如,`{Shift}{selectall}`)。 +该 API 比使用 [`userEvent.type`](#userevent-type) 或 [`userEvent.keyboard`](#userevent-keyboard) 更快,但**不支持** [user-event `keyboard` syntax](https://testing-library.com/docs/user-event/keyboard)(例如,`{Shift}{selectall}`)。 在不需要输入特殊字符或对按键事件进行细粒度控制的情况下,我们建议使用此 API 而不是 [`userEvent.type`](#userevent-type)。 ::: diff --git a/api/expect.md b/api/expect.md index a8d3b02bb..d5c13d3ef 100644 --- a/api/expect.md +++ b/api/expect.md @@ -36,14 +36,14 @@ expect(input).toBe(2) // jest API 此外,`expect` 可以静态地使用来访问匹配器函数,稍后将会介绍。 ::: warning -如果表达式没有类型错误,则 `expect` 对测试类型没有影响。 如果你想使用 Vitest 作为[类型检查器](/guide/testing-types),请使用 [`expectTypeOf`](/api/expect-typeof) 或 [`assertType`](/api/assert-type) 。 +如果表达式没有类型错误,则 `expect` 对测试类型没有影响。 如果你想使用 Vitest 作为 [类型检查器](/guide/testing-types),请使用 [`expectTypeOf`](/api/expect-typeof) 或 [`assertType`](/api/assert-type)。 ::: ## assert - **类型:** `Chai.AssertStatic` -Vitest 将 Chai 的 [`assert` API](https://www.chaijs.com/api/assert/) 以 `expect.assert` 的形式重新导出。你可以在 [Assert API page](/api/assert) 页面查看支持的方法。 +Vitest 将 Chai 的 [`assert` API](https://www.chaijs.com/api/assert/) 以 `expect.assert` 的形式重新导出。你可以在 [Assert API page](/api/assert) 页面查看支持的方法。 如果你需要缩小类型范围时,这将特别有用,因为 `expect.to*` 方法不支持此功能: diff --git a/api/mock.md b/api/mock.md index 916485a5a..07862b44e 100644 --- a/api/mock.md +++ b/api/mock.md @@ -51,7 +51,7 @@ fn.length // == 2 ::: ::: warning 类支持 {#class-support} -像 `mockReturnValue`、`mockReturnValueOnce`、`mockResolvedValue` 这样的简写方法不能用于模拟类。类构造函数在返回值方面具有[反直觉的行为](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor): +像 `mockReturnValue`、`mockReturnValueOnce`、`mockResolvedValue` 这样的简写方法不能用于模拟类。类构造函数在返回值方面具有 [反直觉的行为](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor): ```ts {2,7} const CorrectDogClass = vi.fn(class { diff --git a/api/test.md b/api/test.md index 84ca72506..7e5309f72 100644 --- a/api/test.md +++ b/api/test.md @@ -517,7 +517,7 @@ test.fails('repro #1234', () => { - **别名:** `it.each` ::: tip -`test.each` 是为兼容 Jest 而提供的,Vitest 还提供了 [`test.for`](#test-for),额外支持集成 [`TestContext`](/guide/test-context)。 +`test.each` 是为兼容 Jest 而提供的,Vitest 还提供了 [`test.for`](#test-for),额外支持集成 [`TestContext`](/guide/test-context)。 ::: 当需要以不同变量运行相同测试时,使用 `test.each`。你可以在测试名称中按测试函数参数顺序,使用 [printf 格式化](https://nodejs.org/api/util.html#util_util_format_format_args) 注入参数。 diff --git a/api/vi.md b/api/vi.md index 13f78fbc3..b25311309 100644 --- a/api/vi.md +++ b/api/vi.md @@ -32,7 +32,7 @@ function mock( ): void ``` -用另一个模块替换提供的 `path` 中的所有导入模块。我们可以在路径内使用配置的 Vite 别名。对 `vi.mock` 的调用是悬挂式的,因此在何处调用并不重要。它总是在所有导入之前执行。如果需要在其作用域之外引用某些变量,可以在 [`vi.hoisted`](/api/vi#vi-hoisted)中定义它们,并在 `vi.mock` 中引用它们。 +用另一个模块替换提供的 `path` 中的所有导入模块。我们可以在路径内使用配置的 Vite 别名。对 `vi.mock` 的调用是悬挂式的,因此在何处调用并不重要。它总是在所有导入之前执行。如果需要在其作用域之外引用某些变量,可以在 [`vi.hoisted`](/api/vi#vi-hoisted) 中定义它们,并在 `vi.mock` 中引用它们。 建议仅在测试文件中使用 `vi.mock` 或 `vi.hoisted`。若禁用 Vite 的 [module runner](/config/experimental#experimental-vitemodulerunner),这些模拟声明将不会被提升。此设计作为性能优化手段,可避免预加载不必要的文件。 @@ -41,7 +41,7 @@ function mock( 为了提升 `vi.mock` ,Vitest 会静态分析文件。它会指出不能使用未直接从 `vitest` 软件包导入的 `vi` (例如,从某个实用程序文件导入)。使用 `vi.mock` 与从 `vitest` 导入的 `vi` 一起使用,或者启用 [`globals`](/config/globals) 配置选项。 -Vitest 不会模拟 [setup file](/config/setupfiles) 中导入的模块,因为这些模块在运行测试文件时已被缓存。我们可以在 [`vi.hoisted`](#vi-hoisted) 中调用 [`vi.resetModules()`](#vi-resetmodules) ,在运行测试文件前清除所有模块缓存。 +Vitest 不会模拟 [setup file](/config/setupfiles) 中导入的模块,因为这些模块在运行测试文件时已被缓存。我们可以在 [`vi.hoisted`](#vi-hoisted) 中调用 [`vi.resetModules()`](#vi-resetmodules),在运行测试文件前清除所有模块缓存。 ::: 如果定义了 `factory` 函数,所有导入都将返回其结果。Vitest 只调用一次 factory,并缓存所有后续导入的结果,直到 [`vi.unmock`](#vi-unmock) 或 [`vi.doUnmock`](#vi-dounmock) 被调用。 @@ -86,7 +86,7 @@ vi.mock(import('./path/to/module.js'), async (importOriginal) => { 这也意味着不能在 factory 内部使用任何在 factory 外部定义的变量。 -如果需要在 factory 内部使用变量,请尝试 [`vi.doMock`](#vi-domock) 。它以同样的方式工作,但不会被吊起。请注意,它只能模拟后续的导入。 +如果需要在 factory 内部使用变量,请尝试 [`vi.doMock`](#vi-domock)。它以同样的方式工作,但不会被吊起。请注意,它只能模拟后续的导入。 如果在 `vi.mock` 之前声明了 `vi.hoisted` 方法,也可以引用该方法定义的变量: @@ -164,7 +164,7 @@ axios.get(`/apples/${increment(1)}`) 请注意,如果不调用 `vi.mock` ,模块**不会**被自动模拟。要复制 Jest 的自动锁定行为,可以在 [`setupFiles`](/config/setupfiles) 中为每个所需的模块调用 `vi.mock` 。 ::: -如果没有提供 `__mocks__` 文件夹或未提供工厂函数,Vitest 将导入原始模块并自动模拟其所有导出。有关应用的规则,请参阅[算法](/guide/mocking/modules#automocking-algorithm)。 +如果没有提供 `__mocks__` 文件夹或未提供工厂函数,Vitest 将导入原始模块并自动模拟其所有导出。有关应用的规则,请参阅 [算法](/guide/mocking/modules#automocking-algorithm)。 ### vi.doMock @@ -321,7 +321,7 @@ vi.mock('./example.js', async () => { function importMock(path: string): Promise> ``` -导入模块并模拟其所有属性(包括嵌套属性)。遵循与 [`vi.mock`](#vi-mock) 相同的规则。有关应用的规则,请参阅[算法](/guide/mocking/modules#automocking-algorithm)。 +导入模块并模拟其所有属性(包括嵌套属性)。遵循与 [`vi.mock`](#vi-mock) 相同的规则。有关应用的规则,请参阅 [算法](/guide/mocking/modules#automocking-algorithm)。 ### vi.unmock @@ -402,7 +402,7 @@ test('module has old state', async () => { ``` ::: warning -不会重置 mock 注册表。要清除 mock 注册表,请使用 [`vi.unmock`](#vi-unmock) 或 [`vi.doUnmock`](#vi-dounmock) 。 +不会重置 mock 注册表。要清除 mock 注册表,请使用 [`vi.unmock`](#vi-unmock) 或 [`vi.doUnmock`](#vi-dounmock)。 ::: ### vi.dynamicImportSettled @@ -436,7 +436,7 @@ test('operations are resolved', async () => { 该方法还将在导入解析后等待下一个 `setTimeout` 跟他挂钩,因此所有同步操作都应在解析时完成。 ::: -## 模拟函数和对象 +## 模拟函数和对象 {#mocking-functions-and-objects} 本节介绍如何使用 [method mock](/api/mock) 替换环境变量和全局变量。 @@ -570,7 +570,7 @@ function spyOn( ): Mock ``` -创建与 [`vi.fn()`](#vi-fn) 类似的对象的方法或 getter/setter 的监听(spy) 。它会返回一个 [mock 函数](/api/mock) 。 +创建与 [`vi.fn()`](#vi-fn) 类似的对象的方法或 getter/setter 的监听(spy) 。它会返回一个 [mock 函数](/api/mock)。 ```ts let apples = 0 @@ -618,7 +618,7 @@ const spy = vi 如果传入箭头函数, mock 被调用时将抛出 [` is not a constructor` 错误](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_constructor)。 ::: tip -若运行环境支持 [显式资源管理](https://github.com/tc39/proposal-explicit-resource-management) ,可将 `const` 替换为 `using`。离开当前块级作用域时,系统会自动对被 mock 的函数调用 `mockRestore`,特别适用于已打 spy 的方法。 +若运行环境支持 [显式资源管理](https://github.com/tc39/proposal-explicit-resource-management),可将 `const` 替换为 `using`。离开当前块级作用域时,系统会自动对被 mock 的函数调用 `mockRestore`,特别适用于已打 spy 的方法。 ```ts it('calls console.log', () => { @@ -803,7 +803,7 @@ IntersectionObserver === undefined ## Fake Timers -本节介绍如何使用 [fake timers](/guide/mocking/timers) 。 +本节介绍如何使用 [fake timers](/guide/mocking/timers)。 ### vi.advanceTimersByTime @@ -971,7 +971,7 @@ function runAllTimersAsync(): Promise ``` 该方法将异步调用每个已启动的定时器,直到定时器队列为空。这意味着在 `runAllTimersAsync` 期间调用的每个定时器都会被触发,即使是异步定时器。如果我们有一个无限的时间间隔、 -会在尝试 10000 次后抛出(可使用 [`fakeTimers.loopLimit`](/config/faketimers#faketimers-looplimit) )。 +会在尝试 10000 次后抛出(可使用 [`fakeTimers.loopLimit`](/config/faketimers#faketimers-looplimit))。 ```ts setTimeout(async () => { @@ -1062,7 +1062,7 @@ function useFakeTimers(config?: FakeTimerInstallOpts): Vitest 在 `node:child_process` 中使用 `--pool=forks` 运行 Vitest 时,不支持模拟 `nextTick` 。NodeJS 在 `node:child_process` 中内部使用了 `process.nextTick` ,当模拟它时会挂起。使用 `--pool=threads` 运行 Vitest 时支持模拟 `nextTick`。 -内部实现基于 [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers) 。 +内部实现基于 [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers)。 ::: tip `vi.useFakeTimers()` 不再自动模拟 `process.nextTick` 。 @@ -1340,7 +1340,7 @@ vi.setConfig({ function resetConfig(): void ``` -如果之前调用过 [`vi.setConfig`](#vi-setconfig) ,则会将配置重置为原始状态。 +如果之前调用过 [`vi.setConfig`](#vi-setconfig),则会将配置重置为原始状态。 ### vi.defineHelper 4.1.0 {#vi-defineHelper} diff --git a/blog/vitest-3-2.md b/blog/vitest-3-2.md index cd9019daf..34818ec17 100644 --- a/blog/vitest-3-2.md +++ b/blog/vitest-3-2.md @@ -25,7 +25,7 @@ head: content: summary_large_image --- -# Vitest 3.2 发布了! +# Vitest 3.2 发布了!{#vitest-3-2-is-out} _2025 年 6 月 2 日_ @@ -122,7 +122,7 @@ locators.extend({ }) ``` -返回 Playwright [定位器字符串](https://playwright.dev/docs/other-locators)来构造新的定位器。请注意,从此方法返回的字符串将用作于父定位器范围内(如果有的话)。 +返回 Playwright [定位器字符串](https://playwright.dev/docs/other-locators) 来构造新的定位器。请注意,从此方法返回的字符串将用作于父定位器范围内(如果有的话)。 现在你可以直接在 `page` 对象或任何其他定位器上调用 `getByCommentsCount`: diff --git a/blog/vitest-3.md b/blog/vitest-3.md index 5c829c9d4..bb13c52a3 100644 --- a/blog/vitest-3.md +++ b/blog/vitest-3.md @@ -50,7 +50,7 @@ _2025 年 1 月 17 日_ 如果你要开始参与,我们建议你帮助 [分类问题](https://github.com/vitest-dev/vitest/issues)、[审查 PR](https://github.com/vitest-dev/vitest/pulls)、基于开放的问题发送包含失败测试的 PR,并在 [Discussions](https://github.com/vitest-dev/vitest/discussions) 和 Vitest Land 的帮助论坛中 [帮助其他人](https://discord.com/channels/917386801235247114/1057959614160851024)。如果你想与我们交流,请加入我们的 [Discord 社区](http://chat.vitest.dev/),并在 [#contributing 频道](https://discord.com/channels/917386801235247114/1057959614160851024) 上打个招呼。 -要获取关于 Vitest 生态系统和 Vitest Core 的最新消息,请在 [Bluesky](https://bsky.app/profile/vitest.dev) 或 [Mastodon](https://webtoo.ls/@vitest) 上关注我们。 +要获取关于 Vitest 生态系统和 Vitest Core 的最新消息,请在 [Bluesky](https://bsky.app/profile/vitest.dev) 或 [Mastodon](https://webtoo.ls/@vitest) 上关注我们。 ## 报告器更新 {#reporter-updates} @@ -62,7 +62,7 @@ _2025 年 1 月 17 日_ -伴随此更改,我们还重新设计了公共报告器 API(reporters 字段),使[生命周期](/api/advanced/reporters)更容易理解。 +伴随此更改,我们还重新设计了公共报告器 API(reporters 字段),使 [生命周期](/api/advanced/reporters) 更容易理解。 你可以在 [#7069](https://github.com/vitest-dev/vitest/pull/7069) PR 中关注设计过程。为了逆向工程之前的 onTaskUpdate API 并实现这个新的优雅生命周期,我们经历了一番艰难的努力。 diff --git a/blog/vitest-4-1.md b/blog/vitest-4-1.md index ae2b64529..42fa009b5 100644 --- a/blog/vitest-4-1.md +++ b/blog/vitest-4-1.md @@ -138,7 +138,7 @@ With this flag, **no file transforms are applied** — your test files, source c If you are using Node.js 22.18+ or 23.6+, TypeScript is [stripped natively](https://nodejs.org/en/learn/typescript/run-natively) — no extra configuration needed. -Mocking with `vi.mock` and `vi.hoisted` is supported via the Node.js [Module Loader API](https://nodejs.org/api/module.html#customization-hooks) (requires Node.js 22.15+). Note that `import.meta.env`, Vite plugins, aliases, and the `istanbul` coverage provider are not available in this mode. +Mocking with `vi.mock` and `vi.hoisted` is supported via the Node.js [Module Loader API](https://nodejs.org/api/module.html#customization-hooks)(requires Node.js 22.15+). Note that `import.meta.env`, Vite plugins, aliases, and the `istanbul` coverage provider are not available in this mode. Consider this option if you run server-side or script-like tests that don't need Vite transforms. For `jsdom`/`happy-dom` tests, we still recommend the default module runner or [browser mode](/guide/browser/). diff --git a/config/browser/preview.md b/config/browser/preview.md index 56534fbc6..55d379edb 100644 --- a/config/browser/preview.md +++ b/config/browser/preview.md @@ -1,4 +1,4 @@ -# 配置预览 +# 配置预览 {#configuring-preview} ::: warning `preview` 提供程序的主要功能是在真实浏览器环境中显示测试。不过,它不支持高级浏览器自动化功能,如多个浏览器实例或无头模式。对于更复杂的场景,请考虑使用 [Playwright](/config/browser/playwright) 或 [WebdriverIO](/config/browser/webdriverio)。 @@ -22,7 +22,7 @@ export default defineConfig({ 这将使用默认浏览器打开一个新的浏览器窗口来运行测试。您可以通过设置 `instances` 数组中的 `browser` 属性来配置使用哪个浏览器。Vitest 会尝试自动打开浏览器,但在某些环境下可能无法正常工作。在这种情况下,您可以在所需的浏览器中手动打开所提供的 URL。 -## 与其他 Providers 的差异 +## 与其他 Providers 的差异 {#differences-with-other-providers} 与 [Playwright](/config/browser/playwright) 或 [WebdriverIO](/config/browser/webdriverio) 等其他 Providers 相比,预览服务存在一些限制: diff --git a/config/experimental.md b/config/experimental.md index 754109633..c301a0158 100644 --- a/config/experimental.md +++ b/config/experimental.md @@ -298,7 +298,7 @@ vitest --experimental.importDurations.failOnDanger ::: warning 当前 Vitest 仍需依赖 Vite 实现某些功能,如模块图或监视模式。 -另外请注意,此选项仅适用于`forks`或`threads`[执行池](/config/pool)。 +另外请注意,此选项仅适用于`forks`或`threads` [执行池](/config/pool)。 ::: ### 模块运行器 {#module-runner} diff --git a/config/file.md b/config/file.md deleted file mode 100644 index bc4a239a9..000000000 --- a/config/file.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -outline: deep ---- - -# 管理 Vitest 配置文件 - -如果你正在使用 Vite,并且有一个 `vite.config` 文件,Vitest 会读取它并以插件匹配,并将其设置为你的 Vite 应用程序。如果你想使用不同的配置进行测试,或者你的主应用程序不特别依赖 Vite,你可以: - -- 创建 `vitest.config.ts`,它将具有更高的优先级,并**覆盖** `vite.config.ts` 中的配置(Vitest 支持所有传统的 JS 和 TS 扩展,但不支持 `json`)-这意味着 `vite.config` 中的所有选项都将被**忽略** -- 将 `--config` 选项传递给 CLI,例如 `vitest --config ./path/to/vitest.config.ts` -- 使用`process.env.VITEST` 或者 `defineConfig` 上的 `mode` 属性(如果未被`mode`覆盖,则将设置为`test`/`benchmark`),以有条件地应用 `vite.config.ts` 中的不同配置 - -要配置 `vitest` 本身,请在 Vite 配置中添加 `test` 属性。如果你要从 `vite` 本身导入`defineConfig`,你还需要使用[三斜杠命令](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-)添加对 Vitest 类型的引用。 - -使用 `vite` 中的 `defineConfig`,你应该如下配置: - -```ts -/// -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - // ... Specify options here. - }, -}) -``` - -`` 将在 Vitest 3 中停止工作,但您可以在 Vitest 2.1 中开始迁移到 `vitest/config`: - -```ts -/// -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - // ... Specify options here. - }, -}) -``` - -使用 `vitest/config` 中的 `defineConfig`,你应该如下配置: - -```ts -import { defineConfig } from 'vitest/config' - -export default defineConfig({ - test: { - // ... Specify options here. - }, -}) -``` - -如果需要,你可以检索 Vitest 的默认选项以展开它们: - -```ts -import { configDefaults, defineConfig } from 'vitest/config' - -export default defineConfig({ - test: { - exclude: [...configDefaults.exclude, 'packages/template/*'], - }, -}) -``` - -当使用单独的 `vitest.config.js` 时,如果需要,你还可以从另一个配置文件扩展 Vite 的选项: - -```ts -import { defineConfig, mergeConfig } from 'vitest/config' -import viteConfig from './vite.config' - -export default mergeConfig( - viteConfig, - defineConfig({ - test: { - exclude: ['packages/template/*'], - }, - }) -) -``` - -如果你的 Vite 配置被定义为一个函数,可以这样定义配置: - -```ts -import { defineConfig, mergeConfig } from 'vitest/config' -import viteConfig from './vite.config' - -export default defineConfig(configEnv => - mergeConfig( - viteConfig(configEnv), - defineConfig({ - test: { - exclude: ['packages/template/*'], - }, - }) - ) -) -``` diff --git a/config/sequence.md b/config/sequence.md index 58f0fa246..4aac1f295 100644 --- a/config/sequence.md +++ b/config/sequence.md @@ -145,7 +145,7 @@ Whether to randomize tests. - `stack`:"after" 类钩子按定义顺序逆序执行,"before" 类钩子保持定义顺序执行 - `list`:所有钩子严格按定义顺序执行 -- `parallel`:在单个组内并行运行钩子(父套件的钩子仍会在当前套件的钩子之前运行)。实际并发数受[`maxConcurrency`](/config/maxconcurrency) 限制 +- `parallel`:在单个组内并行运行钩子(父套件的钩子仍会在当前套件的钩子之前运行)。实际并发数受 [`maxConcurrency`](/config/maxconcurrency) 限制 ::: tip 此选项不影响 [`onTestFinished`](/api/hooks#ontestfinished) 钩子,该钩子始终采用逆序调用。 diff --git a/config/server.md b/config/server.md index 3956947c1..97e4ae9fa 100644 --- a/config/server.md +++ b/config/server.md @@ -53,7 +53,7 @@ export default defineConfig({ - **类型:** `(string | RegExp)[] | true` - **默认值:** 所有未被外置的模块 -指定应由 Vite 进行转换和解析的模块。这些模块由 Vite 的 [模块运行器](https://cn.vite.dev/guide/api-environment-runtimes#modulerunner)执行。 +指定应由 Vite 进行转换和解析的模块。这些模块由 Vite 的 [模块运行器](https://cn.vite.dev/guide/api-environment-runtimes#modulerunner) 执行。 通常情况下,源代码文件会被自动内联处理。 diff --git a/eslint.config.js b/eslint.config.js index 518685de2..11789f676 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,5 +1,6 @@ import antfu, { GLOB_SRC } from '@antfu/eslint-config' import { createSimplePlugin } from 'eslint-factory' +import mdStyle from 'eslint-plugin-md-style' export default antfu( { @@ -17,6 +18,8 @@ export default antfu( 'advanced/api/import-example.md', 'api/advanced/import-example.md', 'guide/examples/*.md', + 'README.md', + '.github/*.md', ], }, { @@ -59,6 +62,7 @@ export default antfu( 'import/no-named-as-default': 'off', }, }, + mdStyle.configs.recommended, { files: [`**/*.md`, `**/*.md/${GLOB_SRC}`], rules: { diff --git a/guide/advanced/tests.md b/guide/advanced/tests.md index 1f6e24533..a41dd211c 100644 --- a/guide/advanced/tests.md +++ b/guide/advanced/tests.md @@ -1,7 +1,7 @@ # 运行测试 advanced {#running-tests} ::: warning 注意 -本指南介绍如何使用高级 API 通过 Node.js 脚本运行测试。如果您只想[运行测试](/guide/),则可能不需要这个。它主要被库的作者使用。 +本指南介绍如何使用高级 API 通过 Node.js 脚本运行测试。如果您只想 [运行测试](/guide/),则可能不需要这个。它主要被库的作者使用。 破坏性变更可能不会遵循 SemVer,请在使用实验性 API 时固定 Vitest 的版本。 ::: @@ -67,7 +67,7 @@ finally { } ``` -如果我们打算保留 `Vitest` 实例,请确保至少调用 [`init`](/api/advanced/vitest#init) 。这将初始化报告器和覆盖率提供者,但不会运行任何测试。即使我们不打算使用 Vitest 观察器,但希望保持实例运行,也建议启用 `watch` 模式。Vitest 依赖此标志使其某些功能在连续过程中正常工作。 +如果我们打算保留 `Vitest` 实例,请确保至少调用 [`init`](/api/advanced/vitest#init)。这将初始化报告器和覆盖率提供者,但不会运行任何测试。即使我们不打算使用 Vitest 观察器,但希望保持实例运行,也建议启用 `watch` 模式。Vitest 依赖此标志使其某些功能在连续过程中正常工作。 报告器初始化后,如果需要手动运行测试,可以使用 [`runTestSpecifications`](/api/advanced/vitest#runtestspecifications) 或 [`rerunTestSpecifications`](/api/advanced/vitest#reruntestspecifications) 来运行测试。 diff --git a/guide/browser/index.md b/guide/browser/index.md index b49bbcf07..d180fe21e 100644 --- a/guide/browser/index.md +++ b/guide/browser/index.md @@ -55,7 +55,7 @@ bun add -D vitest @vitest/browser-preview ::: ::: warning -不过,要在 CI 中运行测试,我们需要安装 [`playwright`](https://npmx.dev/package/playwright) 或 [`webdriverio`](https://npmx.dev/package/webdriverio) 。我们还建议在本地测试时切换到这两个选项中的一个,而不是使用默认的 `preview` 提供程序,因为它依赖于模拟事件而不是使用 Chrome DevTools 协议。 +不过,要在 CI 中运行测试,我们需要安装 [`playwright`](https://npmx.dev/package/playwright) 或 [`webdriverio`](https://npmx.dev/package/webdriverio)。我们还建议在本地测试时切换到这两个选项中的一个,而不是使用默认的 `preview` 提供程序,因为它依赖于模拟事件而不是使用 Chrome DevTools 协议。 如果你还没有使用这些工具中的任何一个,我们建议从 Playwright 开始,因为它支持并行执行,可显著提升测试速度。 @@ -295,9 +295,9 @@ Vitest 中的浏览器选项取决于 provider。如果在配置文件中传递 ## 浏览器兼容性 {#browser-compatibility} -Vitest 使用 [Vite dev server](https://cn.vitejs.dev/guide/#browser-support) 来运行我们的测试,因此我们只支持 [`esbuild.target`](https://cn.vitejs.dev/config/shared-options#esbuild)选项(默认为 `esnext`)中指定的功能。 +Vitest 使用 [Vite dev server](https://cn.vitejs.dev/guide/#browser-support) 来运行我们的测试,因此我们只支持 [`esbuild.target`](https://cn.vitejs.dev/config/shared-options#esbuild) 选项(默认为 `esnext`)中指定的功能。 -默认情况下,Vite 的目标浏览器支持原生 [ES Modules](https://caniuse.com/es6-module)、原生 [ESM 动态导入](https://caniuse.com/es6-module-dynamic-import) 和 [`import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta)。此外,我们还利用 [`BroadcastChannel`](https://caniuse.com/?search=BroadcastChannel)在 iframe 之间进行通信: +默认情况下,Vite 的目标浏览器支持原生 [ES Modules](https://caniuse.com/es6-module)、原生 [ESM 动态导入](https://caniuse.com/es6-module-dynamic-import) 和 [`import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta)。此外,我们还利用 [`BroadcastChannel`](https://caniuse.com/?search=BroadcastChannel) 在 iframe 之间进行通信: - Chrome >=87 - Firefox >=78 diff --git a/guide/browser/retry-ability.md b/guide/browser/retry-ability.md index f64b34f6d..1ec9d8c87 100644 --- a/guide/browser/retry-ability.md +++ b/guide/browser/retry-ability.md @@ -4,7 +4,7 @@ title: Retry-ability | 浏览器模式 # Retry-ability -浏览器中的测试由于其异步特性,可能会不一致地失败。因此,即使条件延迟(如超时、网络请求或动画),也必须有办法保证断言成功。为此,Vitest 通过 [`expect.poll`](/api/expect#poll)和 `expect.element` API 提供了可重试的断言: +浏览器中的测试由于其异步特性,可能会不一致地失败。因此,即使条件延迟(如超时、网络请求或动画),也必须有办法保证断言成功。为此,Vitest 通过 [`expect.poll`](/api/expect#poll) 和 `expect.element` API 提供了可重试的断言: ```ts import { screen } from '@testing-library/dom' @@ -28,7 +28,7 @@ test('error banner is rendered', async () => { ::: tip `expect.element` 是 `expect.poll(() => element)`的简写,工作方式完全相同。 -`toHaveTextContent` 和所有其他 [`@testing-library/jest-dom`](https://github.com/testing-library/jest-dom)断言在没有内置重试机制的常规`expect`中仍然可用: +`toHaveTextContent` 和所有其他 [`@testing-library/jest-dom`](https://github.com/testing-library/jest-dom) 断言在没有内置重试机制的常规`expect`中仍然可用: ```ts // will fail immediately if .textContent is not `'Error!'` diff --git a/guide/browser/visual-regression-testing.md b/guide/browser/visual-regression-testing.md index bd89ca4e3..b583051ea 100644 --- a/guide/browser/visual-regression-testing.md +++ b/guide/browser/visual-regression-testing.md @@ -398,8 +398,7 @@ Diff image: == WebdriverIO -[WebdriverIO](https://npmx.dev/package/webdriverio) 要求用户自行准备浏览器环境。不过, -[ @browser-actions ](https://github.com/browser-actions) 团队已经为此提供了方便的解决方案, +[WebdriverIO](https://npmx.dev/package/webdriverio) 要求用户自行准备浏览器环境。不过,[ @browser-actions ](https://github.com/browser-actions) 团队已经为此提供了方便的解决方案, 帮你轻松完成浏览器的安装与配置。 ```yaml [.github/workflows/ci.yml] diff --git a/guide/cli-generated.md b/guide/cli-generated.md index f6b86c13d..493c40f81 100644 --- a/guide/cli-generated.md +++ b/guide/cli-generated.md @@ -238,7 +238,7 @@ Coverage reporters to use. Visit [`coverage.reporter`](/config/coverage#coverage - **命令行终端:** `--coverage.ignoreClassMethods ` - **配置:** [coverage.ignoreClassMethods](/config/coverage#coverage-ignoreclassmethods) -覆盖时要忽略的类方法名称数组。更多信息请访问 [istanbuljs](https://github.com/istanbuljs/nyc#ignoring-methods) 。该选项仅适用于 istanbul providers(默认值:`[]`) +覆盖时要忽略的类方法名称数组。更多信息请访问 [istanbuljs](https://github.com/istanbuljs/nyc#ignoring-methods)。该选项仅适用于 istanbul providers(默认值:`[]`) ### coverage.processingConcurrency diff --git a/guide/cli-table.md b/guide/cli-table.md index 096dfdc1d..f222d4587 100644 --- a/guide/cli-table.md +++ b/guide/cli-table.md @@ -19,7 +19,7 @@ | `--coverage.provider ` | 选择覆盖范围采集工具,可用值为 "V8"、"istanbul"和 "custom"。 | | `--coverage.enabled` | 启用覆盖范围收集。可使用 `--coverage` CLI 选项覆盖(默认值:`false`)。 | | `--coverage.include ` | 作为 glob 模式包含在覆盖范围内的文件。使用多个模式时,可指定多次(默认值:`**`)。 | -| `--coverage.exclude ` | 覆盖范围中要排除的文件。使用多个扩展名时,可指定多次(默认情况下: 访问 [`coverage.exclude`](https://vitest.dev/config/#coverage-exclude) | +| `--coverage.exclude ` | 覆盖范围中要排除的文件。使用多个扩展名时,可指定多次(默认情况下: 访问 [`coverage.exclude`](https://vitest.dev/config/#coverage-exclude) | | `--coverage.extension ` | 包含在覆盖范围内的扩展名。使用多个扩展名时,可指定多次 (默认: `[".js", ".cjs", ".mjs", ".ts", ".mts", ".tsx", ".jsx", ".vue", ".svelte"]`) | | `--coverage.clean` | 运行测试前清除覆盖结果(默认值:true) | | `--coverage.cleanOnRerun` | 重新运行监视时清理覆盖率报告(默认值:true) | @@ -35,9 +35,9 @@ | `--coverage.thresholds.functions ` | 针对函数的覆盖度阈值设定,请访问 [istanbuljs](https://github.com/istanbuljs/nyc#coverage-thresholds) 了解更多信息。 此选项不适用于自定义 providers | | `--coverage.thresholds.branches ` | 针对 branches 的覆盖度阈值设定,请访问 [istanbuljs](https://github.com/istanbuljs/nyc#coverage-thresholds) 了解更多信息。 此选项不适用于自定义 providers | | `--coverage.thresholds.statements ` | 针对 statements 的覆盖度阈值设定,请访问 [istanbuljs](https://github.com/istanbuljs/nyc#coverage-thresholds) 了解更多信息。 此选项不适用于自定义 providers | -| `--coverage.ignoreClassMethods ` | 覆盖时要忽略的类方法名称数组。更多信息请访问 [istanbuljs](https://github.com/istanbuljs/nyc#ignoring-methods) 。该选项仅适用于 istanbul providers(默认值:`[]`)。 | +| `--coverage.ignoreClassMethods ` | 覆盖时要忽略的类方法名称数组。更多信息请访问 [istanbuljs](https://github.com/istanbuljs/nyc#ignoring-methods)。该选项仅适用于 istanbul providers(默认值:`[]`)。 | | `--coverage.processingConcurrency ` | 处理覆盖率结果时使用的并发限制。 (默认最小值介于 20 和 CPU 数量之间) | -| `--coverage.customProviderModule ` | 指定自定义覆盖范围提供程序模块的模块名称或路径。 请访问[自定义 providers 覆盖范围](https://vitest.dev/guide/coverage#custom-coverage-provider) 了解更多信息。 此选项仅适用于自定义 providers | +| `--coverage.customProviderModule ` | 指定自定义覆盖范围提供程序模块的模块名称或路径。 请访问 [自定义 providers 覆盖范围](https://vitest.dev/guide/coverage#custom-coverage-provider) 了解更多信息。 此选项仅适用于自定义 providers | | `--coverage.watermarks.statements ` | High and low watermarks for statements in the format of `,` | | `--coverage.watermarks.lines ` | High and low watermarks for lines in the format of `,` | | `--coverage.watermarks.branches ` | High and low watermarks for branches in the format of `,` | diff --git a/guide/cli.md b/guide/cli.md index 9e6a15f8a..05b67f46f 100644 --- a/guide/cli.md +++ b/guide/cli.md @@ -205,7 +205,7 @@ vitest --api=false 与代码覆盖一起使用时,报告将只包含与更改相关的文件。 - 如果与 [`forceRerunTriggers`](/config/forcereruntriggers)配置选项搭配使用,则在 `forceRerunTriggers` 列表中列出的文件至少有一个发生变化时,将运行整个测试套件。默认情况下,Vitest 配置文件和 `package.json` 的更改将始终重新运行整个套件。 + 如果与 [`forceRerunTriggers`](/config/forcereruntriggers) 配置选项搭配使用,则在 `forceRerunTriggers` 列表中列出的文件至少有一个发生变化时,将运行整个测试套件。默认情况下,Vitest 配置文件和 `package.json` 的更改将始终重新运行整个套件。 ### shard diff --git a/guide/comparisons.md b/guide/comparisons.md index 195abb7c3..10c70ddc6 100644 --- a/guide/comparisons.md +++ b/guide/comparisons.md @@ -8,7 +8,7 @@ title: 与其他测试框架对比 | 指南 [Jest](https://jestjs.io/) 在测试框架领域占据了主导地位,因为它为大多数 JavaScript 项目提供开箱即用的支持,具备舒适的 API(`it` 和 `expect`),且覆盖了大多数测试的需求(例如快照、模拟和覆盖率)。我们感谢 Jest 团队和社区创建了一个令人愉悦的测试 API,并引入了许多已成为 Web 生态系统标准的测试模式。 -在 Vite 项目中使用 Jest 是可能的。[@sodatea](https://bsky.app/profile/haoqun.dev) 开发了 [vite-jest](https://github.com/sodatea/vite-jest#readme) ,旨在为 [Jest](https://jestjs.io/) 提供一流的 Vite 集成。[Jest 中最后的阻碍](https://github.com/sodatea/vite-jest/blob/main/packages/vite-jest/README.md#vite-jest) 已经解决。因此,在 Vite 项目的单元测试中,Jest 仍是一个可用选项。 +在 Vite 项目中使用 Jest 是可能的。[@sodatea](https://bsky.app/profile/haoqun.dev) 开发了 [vite-jest](https://github.com/sodatea/vite-jest#readme),旨在为 [Jest](https://jestjs.io/) 提供一流的 Vite 集成。[Jest 中最后的阻碍](https://github.com/sodatea/vite-jest/blob/main/packages/vite-jest/README.md#vite-jest) 已经解决。因此,在 Vite 项目的单元测试中,Jest 仍是一个可用选项。 然而,在 [Vite](https://vitejs.dev) 已为最常见的 Web 工具(TypeScript、JSX、最流行的 UI 框架)提供了支持的情况下,引入 Jest 会增添不必要的复杂性。如果你的应用由 Vite 驱动,那么配置和维护两个不同的管道是不合理的。如果使用 Vitest,你可以在同一个管道中进行开发、构建和测试环境的配置,它们共享相同的插件和 `vite.config.js` 文件。 diff --git a/guide/coverage.md b/guide/coverage.md index ff2f857eb..d45bf85ec 100644 --- a/guide/coverage.md +++ b/guide/coverage.md @@ -44,7 +44,7 @@ npm i -D @vitest/coverage-istanbul ::: info 以下对 V8 覆盖率的说明仅适用于 Vitest,并不适用于其他测试工具。 -从 `v3.2.0` 版本开始,Vitest 在 V8 覆盖率中采用了 [基于 AST 的重映射技术](/blog/vitest-3-2#coverage-v8-ast-aware-remapping) ,从而生成与 Istanbul 一致的覆盖率报告。 +从 `v3.2.0` 版本开始,Vitest 在 V8 覆盖率中采用了 [基于 AST 的重映射技术](/blog/vitest-3-2#coverage-v8-ast-aware-remapping),从而生成与 Istanbul 一致的覆盖率报告。 这让用户在享受 V8 覆盖率高速执行的同时,也能获得 Istanbul 覆盖率的高准确度。 ::: @@ -347,7 +347,7 @@ export default CustomCoverageProviderModule - [`istanbul`](https://github.com/istanbuljs/nyc#parsing-hints-ignoring-lines) 使用 TypeScript 时,源代码使用 `esbuild` 进行转译,这会从源代码中删除所有注释([esbuild#516](https://github.com/evanw/esbuild/issues/516))。 -被视为[合法注释](https://esbuild.github.io/api/#legal-comments)的注释将被保留。 +被视为 [合法注释](https://esbuild.github.io/api/#legal-comments) 的注释将被保留。 你可以在忽略提示里加入 `@preserve` 关键字。 但要小心,这些忽略提示有可能会被打包进最终的生产环境构建中。 diff --git a/guide/features.md b/guide/features.md index 4ef6378f6..f3b28c78b 100644 --- a/guide/features.md +++ b/guide/features.md @@ -25,7 +25,7 @@ This page is a high-level overview of Vitest's capabilities. If you're new to Vi 与 Vite 的配置、转换器、解析器和插件通用,将会使用应用中的相同配置来运行测试。 -更多详情请参阅 [配置 Vitest](/config/) 。 +更多详情请参阅 [配置 Vitest](/config/)。 ## 监听模式(watch mode) {#watch-mode} @@ -53,7 +53,7 @@ Vitest 还隔离了每个测试文件的运行环境,因此一个文件中的 Vitest 提供了许多缩小测试范围的方法,以便在开发过程中加快速度并集中精力。 -了解更多信息 [测试筛选](/guide/filtering) 。 +了解更多信息 [测试筛选](/guide/filtering)。 ## 同时运行多个测试 {#running-tests-concurrently} @@ -165,7 +165,7 @@ export default defineConfig({ }) ``` -了解更多信息 [模拟对象](/guide/mocking) 。 +了解更多信息 [模拟对象](/guide/mocking)。 ## 测试覆盖率 {#coverage} @@ -195,7 +195,7 @@ export default defineConfig({ }) ``` -了解更多信息 [覆盖率](/guide/coverage) 。 +了解更多信息 [覆盖率](/guide/coverage)。 ## 源码内联测试 {#in-source-testing} @@ -267,7 +267,7 @@ test('my types work properly', () => { ## 分片 {#sharding} -使用 [`--shard`](/guide/cli#shard) 和 [`--reporter=blob`](/guide/reporters#blob-reporter)标志在不同的计算机上运行测试。可以使用 `--merge-reports` 命令在 CI 管道的末尾合并所有测试结果: +使用 [`--shard`](/guide/cli#shard) 和 [`--reporter=blob`](/guide/reporters#blob-reporter) 标志在不同的计算机上运行测试。可以使用 `--merge-reports` 命令在 CI 管道的末尾合并所有测试结果: ```bash vitest --shard=1/2 --reporter=blob --coverage diff --git a/guide/improving-performance.md b/guide/improving-performance.md index a8b5dbd03..c25cb3680 100644 --- a/guide/improving-performance.md +++ b/guide/improving-performance.md @@ -103,7 +103,7 @@ Duration 5.90s (transform 842ms, setup 543ms, import 2.35s, tests 2.94s, enviro ## 运行池 {#pool} -默认情况下,Vitest 在 `pool: 'forks'` 中运行测试。虽然 `'forks'` 池更适合解决兼容性问题([hanging process](/guide/common-errors.html#failed-to-terminate-worker) 和[segfaults](/guide/common-errors.html#segfaults-and-native-code-errors)),但在较大的项目中,它可能比 `pool: 'threads'` 稍慢。 +默认情况下,Vitest 在 `pool: 'forks'` 中运行测试。虽然 `'forks'` 池更适合解决兼容性问题([hanging process](/guide/common-errors.html#failed-to-terminate-worker) 和 [segfaults](/guide/common-errors.html#segfaults-and-native-code-errors)),但在较大的项目中,它可能比 `pool: 'threads'` 稍慢。 你可以尝试通过切换配置中的 `pool` 选项来改善测试运行时间: diff --git a/guide/mocking.md b/guide/mocking.md index a20b19c59..511f8022d 100644 --- a/guide/mocking.md +++ b/guide/mocking.md @@ -15,7 +15,7 @@ New to mocking? Start with the [Mock Functions](/guide/learn/mock-functions) tut 不要忘记在每次测试运行前后清除或恢复模拟对象,以撤消运行测试时模拟对象状态的更改!有关更多信息,请参阅 [`mockReset`](/api/mock.html#mockreset) 文档。 ::: -如果你不熟悉 `vi.fn`、`vi.mock` 或 `vi.spyOn` 方法,请先查看[API部分](/api/vi)。 +如果你不熟悉 `vi.fn`、`vi.mock` 或 `vi.spyOn` 方法,请先查看 [API部分](/api/vi)。 Vitest 提供了有关模拟的全部指南: diff --git a/guide/mocking/dates.md b/guide/mocking/dates.md index 32d9707d6..b3a6a808f 100644 --- a/guide/mocking/dates.md +++ b/guide/mocking/dates.md @@ -1,6 +1,6 @@ # 模拟日期 {#mocking-dates} -有时你需要控制日期以确保测试时的一致性。Vitest 使用 [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers) 包来操作计时器和系统日期。你可以在[这里](/api/vi#vi-setsystemtime)找到有关特定 API 的详细信息。 +有时你需要控制日期以确保测试时的一致性。Vitest 使用 [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers) 包来操作计时器和系统日期。你可以在 [这里](/api/vi#vi-setsystemtime) 找到有关特定 API 的详细信息。 ## 示例 {#example} diff --git a/guide/mocking/requests.md b/guide/mocking/requests.md index 6830535ac..5e66c68b5 100644 --- a/guide/mocking/requests.md +++ b/guide/mocking/requests.md @@ -2,7 +2,7 @@ 由于 Vitest 运行在 Node 环境中,模拟网络请求很棘手;Web API 不可用,所以我们需要一些能够模拟网络行为的工具。我们推荐使用 [Mock Service Worker](https://mswjs.io/) 来实现这一点。它允许你模拟 `http`、`WebSocket` 和 `GraphQL` 网络请求,并且与框架无关。 -Mock Service Worker (MSW) 通过拦截你的测试发出的请求来工作,允许你在不更改任何应用程序代码的情况下使用它。在浏览器中,这使用 [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API)。在 Node.js 和 Vitest 中,它使用 [`@mswjs/interceptors`](https://github.com/mswjs/interceptors) 库。要了解更多关于 MSW 的信息,请阅读他们的[介绍](https://mswjs.io/docs/)。 +Mock Service Worker (MSW) 通过拦截你的测试发出的请求来工作,允许你在不更改任何应用程序代码的情况下使用它。在浏览器中,这使用 [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API)。在 Node.js 和 Vitest 中,它使用 [`@mswjs/interceptors`](https://github.com/mswjs/interceptors) 库。要了解更多关于 MSW 的信息,请阅读他们的 [介绍](https://mswjs.io/docs/)。 ## 配置 {#configuration} @@ -111,4 +111,4 @@ afterEach(() => server.resetHandlers()) > 配置服务器时设置 `onUnhandledRequest: 'error'` 可以确保当存在没有对应请求处理器的请求时抛出错误。 ## 更多信息 {#more} -MSW 的功能远不止这些。你可以访问 cookie 和查询参数,定义模拟错误响应,以及更多其他功能!要了解 MSW 的所有功能,请阅读[它们的文档](https://mswjs.io/docs)。 +MSW 的功能远不止这些。你可以访问 cookie 和查询参数,定义模拟错误响应,以及更多其他功能!要了解 MSW 的所有功能,请阅读 [它们的文档](https://mswjs.io/docs)。 diff --git a/guide/profiling-test-performance.md b/guide/profiling-test-performance.md index 1637ea617..05ebcae7c 100644 --- a/guide/profiling-test-performance.md +++ b/guide/profiling-test-performance.md @@ -53,7 +53,7 @@ export default defineConfig({ 测试运行后,应该会生成 `test-runner-profile/*.cpuprofile` 和 `test-runner-profile/*.heapprofile` 文件。想要知道如何分析这些文件,可以仔细查看 [性能分析记录](#inspecting-profiling-records)。 -也可以看看 [性能分析 | 示例](https://github.com/vitest-dev/vitest/tree/main/examples/profiling) 。 +也可以看看 [性能分析 | 示例](https://github.com/vitest-dev/vitest/tree/main/examples/profiling)。 ## 主线程 {#main-thread} @@ -74,7 +74,7 @@ $ node --cpu-prof --cpu-prof-dir=main-profile ./node_modules/vitest/vitest.mjs - # NodeJS arguments Vitest arguments ``` -测试运行后会生成一个 `main-profile/*.cpuprofile` 文件。有关如何分析这些文件的说明,可以查看[检查分析记录](#inspecting-profiling-records)。 +测试运行后会生成一个 `main-profile/*.cpuprofile` 文件。有关如何分析这些文件的说明,可以查看 [检查分析记录](#inspecting-profiling-records)。 ## 文件转换 {#file-transform} diff --git a/guide/reporters.md b/guide/reporters.md index 99a38bf74..9b17499c3 100644 --- a/guide/reporters.md +++ b/guide/reporters.md @@ -166,7 +166,7 @@ export default defineConfig({ 除此之外,`verbose` 报告器会立即打印测试错误消息。完整的测试错误会在测试运行结束时报告。 -这是唯一一个在测试未失败时报告[注解](/guide/test-annotations)的终端报告器。 +这是唯一一个在测试未失败时报告 [注解](/guide/test-annotations) 的终端报告器。 :::code-group @@ -460,12 +460,12 @@ export default defineConfig({ ::: ::: tip -该报告器需要安装 [`@vitest/ui`](/guide/ui) 。 +该报告器需要安装 [`@vitest/ui`](/guide/ui)。 ::: ### TAP 报告器 {#tap-reporter} -按照 [Test Anything Protocol](https://testanything.org/) (TAP)输出报告。 +按照 [Test Anything Protocol](https://testanything.org/)(TAP)输出报告。 :::code-group @@ -693,7 +693,7 @@ export default defineConfig({ ``` ::: -### Blob 报告器 +### Blob 报告器 {#blob-reporter} 将测试结果存储在计算机上,以便以后可以使用 [`--merge-reports`](/guide/cli#merge-reports) 命令进行合并。 默认情况下,将所有结果存储在 `.vitest-reports` 文件夹中,但可以用 `--outputFile` 或 `--outputFile.blob` 标志覆盖。 diff --git a/guide/test-context.md b/guide/test-context.md index 1a954a3de..0892672e3 100644 --- a/guide/test-context.md +++ b/guide/test-context.md @@ -104,7 +104,7 @@ test('annotations API', async ({ annotate }) => { ### `signal` 3.2.0 {#signal} -一个由 Vitest 控制的 [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) ,在以下场景下会被触发中止: +一个由 Vitest 控制的 [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal),在以下场景下会被触发中止: - 测试用例超时 - 用户使用 Ctrl+C 手动终止了测试 @@ -529,7 +529,7 @@ test('second test', ({ counter }) => { }) ``` -Test-scoped fixtures have access to the [built-in test context](#built-in-test-context) (`task`, `expect`, `skip`, etc.): +Test-scoped fixtures have access to the [built-in test context](#built-in-test-context)(`task`, `expect`, `skip`, etc.): ```ts const test = baseTest @@ -604,7 +604,7 @@ const test = baseTest ``` ::: tip -Only test-scoped fixtures have access to the [built-in test context](#built-in-test-context) (`task`, `expect`, `skip`, etc.). Worker and file fixtures run outside of any specific test, so test-specific properties are not available to them. +Only test-scoped fixtures have access to the [built-in test context](#built-in-test-context)(`task`, `expect`, `skip`, etc.). Worker and file fixtures run outside of any specific test, so test-specific properties are not available to them. If you need the file path in a file-scoped fixture, use `expect.getState().testPath` instead. ::: diff --git a/guide/using-plugins.md b/guide/using-plugins.md index bcc15ba8a..e75f1e06b 100644 --- a/guide/using-plugins.md +++ b/guide/using-plugins.md @@ -2,7 +2,7 @@ title: 使用插件 | 指南 --- -# 使用插件 +# 使用插件 {#using-plugins} Vitest 可以使用插件进行扩展,类似于 Vite 插件的工作方式。这允许你使用相同的 API 和 Vite 插件概念来增强和自定义 Vitest 的功能。 diff --git a/package.json b/package.json index 5132bc6a2..037368969 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@voidzero-dev/vitepress-theme": "^4.8.3", "eslint": "^10.0.3", "eslint-factory": "^0.1.2", + "eslint-plugin-md-style": "0.1.0-beta.2", "https-localhost": "^4.7.1", "lint-staged": "^16.2.7", "pathe": "^2.0.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c15e1f8d0..469a29769 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -60,6 +60,9 @@ importers: eslint-factory: specifier: ^0.1.2 version: 0.1.2(eslint@10.0.3(jiti@2.6.1))(typescript@5.9.3) + eslint-plugin-md-style: + specifier: 0.1.0-beta.2 + version: 0.1.0-beta.2(@eslint/markdown@7.5.1)(eslint@10.0.3(jiti@2.6.1)) https-localhost: specifier: ^4.7.1 version: 4.7.1 @@ -2537,6 +2540,13 @@ packages: peerDependencies: eslint: '>=9.38.0' + eslint-plugin-md-style@0.1.0-beta.2: + resolution: {integrity: sha512-jyBZbhqtFGlSbPjVbMc2VOJPMgHelq6UDKJf+XT336YG/USfGA/QYZ+KT9ch9xzzB4XaI7XIXoawpRAdiPJg3g==} + engines: {node: '>=20.19.0'} + peerDependencies: + '@eslint/markdown': ^7.5.1 + eslint: ^9.0.0 || ^10.0.0 + eslint-plugin-n@17.24.0: resolution: {integrity: sha512-/gC7/KAYmfNnPNOb3eu8vw+TdVnV0zhdQwexsw6FLXbhzroVj20vRn2qL8lDWDGnAQ2J8DhdfvXxX9EoxvERvw==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} @@ -7199,6 +7209,11 @@ snapshots: transitivePeerDependencies: - '@eslint/json' + eslint-plugin-md-style@0.1.0-beta.2(@eslint/markdown@7.5.1)(eslint@10.0.3(jiti@2.6.1)): + dependencies: + '@eslint/markdown': 7.5.1 + eslint: 10.0.3(jiti@2.6.1) + eslint-plugin-n@17.24.0(eslint@10.0.3(jiti@2.6.1))(typescript@5.9.3): dependencies: '@eslint-community/eslint-utils': 4.9.1(eslint@10.0.3(jiti@2.6.1))