Skip to content

Commit fa316c2

Browse files
committed
docs(cn): update guide/migration.md
1 parent 0a66848 commit fa316c2

1 file changed

Lines changed: 49 additions & 34 deletions

File tree

guide/migration.md

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ outline: deep
66
# 迁移指南 {#migration-guide}
77

88
[迁移至 Vitest 3.0](https://v3.vitest.dev/guide/migration) | [迁移至 Vitest 2.0](https://v2.vitest.dev/guide/migration)
9-
<!-- TODO: translation -->
10-
## Migrating to Vitest 5.0 {#vitest-5}
119

12-
::: warning Work in progress
13-
Vitest 5.0 is currently in beta. This section tracks breaking changes as they are merged and may change before the stable release.
10+
## 迁移至 Vitest 5.0 {#vitest-5}
11+
12+
::: warning 进行中
13+
Vitest 5.0 目前处于 beta 阶段。本章节跟踪已合并的重大变更,在稳定版发布前可能还会发生变化。
1414
:::
1515

16-
### Removed `test.sequential`, `describe.sequential`, and `sequential` Options
16+
### 移除 `test.sequential`, `describe.sequential`, `sequential` 选项 {##removed-test-sequential-describe-sequential-and-sequential-options}
1717

18-
Vitest 5.0 removes the deprecated `test.sequential`, `describe.sequential`, and `sequential` test options. Use `concurrent: false` when you need a test or suite to opt out of inherited or globally configured concurrency.
18+
Vitest 5.0 移除了已弃用的 `test.sequential``describe.sequential` `sequential` 选项。当你需要让某个测试或测试套件不再沿用继承来的并发设置,或退出全局配置的并发时,请使用 `concurrent: false`
1919

2020
```ts
2121
test.sequential('example', async () => { /* ... */ }) // [!code --]
@@ -34,14 +34,14 @@ test('example', { sequential: true }, async () => { /* ... */ }) // [!code --]
3434
test('example', { concurrent: false }, async () => { /* ... */ }) // [!code ++]
3535
```
3636

37-
### Locators in Commands are Serialized as Objects
37+
### 命令中的定位器被序列化为对象 {#locators-in-commands-are-serialized-as-objects}
3838

39-
Locators forwarded to [browser commands](/api/browser/commands) are now serialized as a `SerializedLocator` object instead of a bare selector string. The object exposes two fields:
39+
转发到 [浏览器命令](/api/browser/commands) 的定位器现在被序列化为 `SerializedLocator` 对象,而不是裸的选择器字符串。该对象公开两个字段:
4040

41-
- `selector`: the provider-specific selector string (the same value commands previously received).
42-
- `locator`: a human-readable representation of the locator (e.g. `getByRole('button')`), used for error messages and tracing.
41+
- `selector`:提供程序特定的选择器字符串(与 commands 先前接收的值相同)。
42+
- `locator`:定位器的人类可读的(例如 `getByRole('button')`),用于错误消息和跟踪。
4343

44-
Update any custom commands that accept a locator to destructure `selector` from the new object:
44+
更新所有接收定位器的自定义命令,改为从新的对象中解构出 selector
4545

4646
```ts
4747
import type { SerializedLocator } from '@vitest/browser'
@@ -56,18 +56,18 @@ export async function customClick(
5656
}
5757
```
5858

59-
### Removed Deprecated Entrypoints
59+
### 移除了已弃用的入口 {#removed-deprecated-entrypoints}
6060

61-
Several entry points were marked as deprecated in Vitest 4.1. This release removes them entirely.
61+
多个入口在 Vitest 4.1 中被标记为已弃用。此版本完全移除了它们。
6262

63-
- `vitest/coverage`: use `vitest/node` instead
64-
- `vitest/reporters`: use `vitest/node` instead
65-
- `vitest/environments`: use `vitest/runtime` instead
66-
- `vitest/snapshot`: use `vitest/runtime` instead
67-
- `vitest/runners`: use `TestRunner` from `vitest` instead
68-
- `vitest/suite`: use static methods on `TestRunner` from vitest instead (for example, `TestRunner.getCurrentTest()`)
69-
- `vitest/mocker` is removed completely, use `@vitest/mocker` package directly (this was published by accident at one point and never removed)
70-
- `vitest/internal/module-runner` is removed
63+
- `vitest/coverage`:改用 `vitest/node`
64+
- `vitest/reporters`:改用 `vitest/node`
65+
- `vitest/environments`:改用 `vitest/runtime`
66+
- `vitest/snapshot`:改用 `vitest/runtime`
67+
- `vitest/runners`:改用 `vitest` 中的 `TestRunner`
68+
- `vitest/suite`:改用 `vitest``TestRunner` 的静态方法(例如,`TestRunner.getCurrentTest()`
69+
- `vitest/mocker` 已移除,请直接使用 `@vitest/mocker` 包(这个包曾意外发布过一次且从未被移除)
70+
- `vitest/internal/module-runner` 已移除
7171

7272
## 迁移至 Vitest 4.0 {#vitest-4}
7373

@@ -93,7 +93,7 @@ Vitest 的 V8 覆盖率提供器现在使用了更精准的结果映射逻辑,
9393

9494
在之前的版本中,Vitest 会默认把所有未覆盖的文件包含到报告中。这是因为 `coverage.all` 默认为 `true``coverage.include` 默认为 `**`。这样设计是因为测试工具无法准确判断用户源码所在位置。
9595

96-
然而,这导致 Vitest 覆盖率工具会处理很多意料之外的文件(例如压缩 JS 文件),造成报告生成速度很慢甚至卡死。在 Vitest v4 中,我们彻底移除了 `coverage.all`,并将默认行为改为**只在报告中包含被测试覆盖的文件**
96+
然而,这导致 Vitest 覆盖率工具会处理很多意料之外的文件(例如压缩 JS 文件),造成报告生成速度很慢甚至卡死。在 Vitest v4 中,我们彻底移除了 `coverage.all`,并将默认行为改为 **只在报告中包含被测试覆盖的文件**
9797

9898
升级至 v4 版本时,建议先在配置文件中定义 `coverage.include`,再根据需要逐步添加简单的 `coverage.exclude` 匹配规则。
9999

@@ -132,6 +132,7 @@ export default defineConfig({
132132
```
133133

134134
更多示例请参考:
135+
135136
- [覆盖率报告中的文件包含与排除](/guide/coverage.html#including-and-excluding-files-from-coverage-report)
136137
- [性能分析 | 代码覆盖率](/guide/profiling-test-performance.html#code-coverage) 了解调试覆盖率生成的方法
137138

@@ -173,7 +174,7 @@ export default defineConfig({
173174
})
174175
```
175176

176-
### `spyOn``fn` 支持构造函数 {#spyon-and-fn-support-constructors}
177+
### `spyOn``fn` 支持构造函数 {#spyon-and-fn-support-constructors}
177178

178179
在之前的版本中,如果尝试使用 `vi.spyOn` 监视构造函数,可能会收到类似 `Constructor <name> requires 'new'` 的错误。自 Vitest 4 起,所有通过 `new` 关键字调用的模拟会构造实例,而不是调用 `mock.apply`。这意味着在这些情况下,模拟实现必须使用 `function``class` 关键字:
179180

@@ -234,6 +235,7 @@ expect(instance2.method()).toBe(100)
234235

235236
expect(AutoMockedClass.prototype.method).toHaveBeenCalledTimes(4)
236237
```
238+
237239
- 自动 mock 方法一经生成即不可还原,手动 `.mockRestore` 无效;`spy: true` 的自动 mock 模块行为保持不变。
238240
- 自动 mock 的 getter 不再执行原始逻辑,默认返回 `undefined`;如需继续监听并改写,请使用 `vi.spyOn(object, name, 'get')`
239241
- 执行 `vi.fn(implementation).mockReset()` 后,`.getMockImplementation()` 现可正确返回原 mock 实现。
@@ -252,20 +254,23 @@ $ vitest --standalone math.test.ts
252254
这允许用户为独立模式创建可复用的 `package.json`
253255

254256
::: code-group
257+
255258
```json [package.json]
256259
{
257260
"scripts": {
258261
"test:dev": "vitest --standalone"
259262
}
260263
}
261264
```
265+
262266
```bash [CLI]
263267
# 以独立模式启动 Vitest,启动时不运行任何文件
264268
$ pnpm run test:dev
265269

266270
# 立即运行 math.test.ts
267271
$ pnpm run test:dev math.test.ts
268272
```
273+
269274
:::
270275

271276
### `vite-node` 替换为 [Module Runner](https://vite.dev/guide/api-environment-runtimes.html#modulerunner) {#replacing-vite-node-with-module-runner}
@@ -288,6 +293,7 @@ Vite 已提供外部化机制,但为降低破坏性,仍保留旧方案;[`s
288293
在 Vitest 3.2 中,`workspace` 配置选项更名为 [`projects`](/guide/projects)。除了不能指定其他文件作为工作区的源文件(以前可以指定导出项目数组的文件)外,它们在功能上是相同的。迁移到 `projects` 非常简单,只需将代码从 `vitest.workspace.js` 移动到 `vitest.config.ts`
289294

290295
::: code-group
296+
291297
```ts [vitest.config.js]
292298
import { defineConfig } from 'vitest/config'
293299

@@ -305,6 +311,7 @@ export default defineConfig({
305311
}
306312
})
307313
```
314+
308315
```ts [vitest.workspace.js]
309316
import { defineWorkspace } from 'vitest/config' // [!code --]
310317
@@ -317,11 +324,12 @@ export default defineWorkspace([ // [!code --]
317324
} // [!code --]
318325
]) // [!code --]
319326
```
327+
320328
:::
321329

322330
### 修改浏览器模式提供者 {#browser-provider-rework}
323331

324-
在 Vitest 4.0 中,浏览器提供者现在接受对象而非字符 (`'playwright'`, `'webdriverio'`)。 `preview` 不再是默认设置。这使得使用自定义选项变得更简单,而且不再需要添加 `/// <reference` 注释。
332+
在 Vitest 4.0 中,浏览器提供者现在接受对象而非字符 (`'playwright'`, `'webdriverio'`)。`preview` 不再是默认设置。这使得使用自定义选项变得更简单,而且不再需要添加 `/// <reference` 注释。
325333

326334
```ts
327335
import { playwright } from '@vitest/browser-playwright' // [!code ++]
@@ -412,6 +420,7 @@ export default defineConfig({
412420
此前在使用 [测试项目](/guide/projects) 时,无法为单个项目单独指定某些 pool 相关配置项。新架构已解除了这一限制。
413421

414422
::: code-group
423+
415424
```ts [按项目隔离]
416425
import { defineConfig } from 'vitest/config'
417426

@@ -433,6 +442,7 @@ export default defineConfig({
433442
},
434443
})
435444
```
445+
436446
```ts [并行与串行项目]
437447
import { defineConfig } from 'vitest/config'
438448

@@ -452,6 +462,7 @@ export default defineConfig({
452462
},
453463
})
454464
```
465+
455466
```ts [按项目分配 Node CLI 选项]
456467
import { defineConfig } from 'vitest/config'
457468

@@ -470,6 +481,7 @@ export default defineConfig({
470481
},
471482
})
472483
```
484+
473485
:::
474486

475487
更多示例请参阅 [测试技巧](/guide/recipes)
@@ -503,7 +515,7 @@ export default defineConfig({
503515

504516
### 使用自定义元素打印 Shadow Root 快照 {#snapshots-using-custom-elements-print-the-shadow-root}
505517

506-
在 Vitest 4.0 中,包含自定义元素的快照将打印阴影根内容。要恢复以前的行为,请将 [`printShadowRoot` option](/config/snapshotformat) 设为`false`
518+
在 Vitest 4.0 中,包含自定义元素的快照将打印阴影根内容。要恢复以前的行为,请将 [`printShadowRoot` option](/config/snapshotformat) 设为 `false`
507519

508520
```js{15-22}
509521
// 自 Vitest 4.0 前
@@ -702,7 +714,9 @@ export default defineConfig({
702714
```
703715

704716
否则快照中会出现大量转义的 `"` 字符。
717+
705718
<!-- TODO: translation -->
719+
706720
### Custom Snapshot Matchers <Badge type="warning">experimental</Badge> <Version>4.1.3</Version>
707721

708722
Jest imports snapshot composables from `jest-snapshot`. In Vitest, use `Snapshots` from `vitest` instead:
@@ -818,16 +832,16 @@ expect(spy).to.have.been.calledWith('arg1', 'arg2')
818832

819833
Vitest supports all common sinon-chai assertions:
820834

821-
| Sinon-Chai | Vitest | 详情 |
822-
|------------|--------|-------------|
823-
| `spy.called` | `called` | Spy 至少被调用过一次 |
824-
| `spy.calledOnce` | `calledOnce` | Spy 恰好被调用过一次 |
825-
| `spy.calledTwice` | `calledTwice` | Spy 恰好被调用过两次 |
826-
| `spy.calledThrice` | `calledThrice` | Spy 恰好被调用过三次 |
827-
| `spy.callCount(n)` | `callCount(n)` | Spy 被调用过 n 次 |
828-
| `spy.calledWith(...)` | `calledWith(...)` | Spy 以特定参数被调用 |
835+
| Sinon-Chai | Vitest | 详情 |
836+
| ------------------------- | --------------------- | ---------------------------- |
837+
| `spy.called` | `called` | Spy 至少被调用过一次 |
838+
| `spy.calledOnce` | `calledOnce` | Spy 恰好被调用过一次 |
839+
| `spy.calledTwice` | `calledTwice` | Spy 恰好被调用过两次 |
840+
| `spy.calledThrice` | `calledThrice` | Spy 恰好被调用过三次 |
841+
| `spy.callCount(n)` | `callCount(n)` | Spy 被调用过 n 次 |
842+
| `spy.calledWith(...)` | `calledWith(...)` | Spy 以特定参数被调用 |
829843
| `spy.calledOnceWith(...)` | `calledOnceWith(...)` | Spy 以特定参数恰好被调用一次 |
830-
| `spy.returned(value)` | `returned` | Spy 返回了特定值 |
844+
| `spy.returned(value)` | `returned` | Spy 返回了特定值 |
831845

832846
更多内容请参阅 [Chai 风格 Spy 断言](/api/expect#chai-style-spy-assertions) 文档中的完整列表。
833847

@@ -909,6 +923,7 @@ vi.useRealTimers()
909923
3. **并发执行**:Vitest 默认并发运行测试,Mocha 则顺序执行。
910924

911925
更多内容请参阅:
926+
912927
- [Chai 风格 Spy 断言](/api/expect#chai-style-spy-assertions)
913928
- [Mocking 指南](/guide/mocking)
914929
- [Vi API](/api/vi)

0 commit comments

Comments
 (0)