Skip to content

Commit c8a5300

Browse files
committed
docs(en): merging all conflicts
2 parents 2e79749 + ec2e441 commit c8a5300

File tree

7 files changed

+144
-12
lines changed

7 files changed

+144
-12
lines changed

.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export default ({ mode }: { mode: string }) => {
7373
'CLI': 'vscode-icons:file-type-shell',
7474
'vitest.shims': 'vscode-icons:file-type-vitest',
7575
'vitest.config': 'vscode-icons:file-type-vitest',
76+
'vitest.workspace': 'vscode-icons:file-type-vitest',
7677
'.spec.ts': 'vscode-icons:file-type-testts',
7778
'.test.ts': 'vscode-icons:file-type-testts',
7879
'.spec.js': 'vscode-icons:file-type-testjs',

advanced/api/vitest.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,24 @@ function createCoverageProvider(): Promise<CoverageProvider | null>
553553
若未将 [`coverage.clean`](/config/#coverage-clean) 显式设为 false ,此方法还会清空之前的所有报告。
554554
:::
555555

556+
## enableCoverage <Version>4.0.0</Version> {#enablecoverage}
557+
558+
```ts
559+
function enableCoverage(): Promise<void>
560+
```
561+
562+
This method enables coverage for tests that run after this call. `enableCoverage` doesn't run any tests; it only sets up Vitest to collect coverage.
563+
564+
It creates a new coverage provider if one doesn't already exist.
565+
566+
## disableCoverage <Version>4.0.0</Version> {#disablecoverage}
567+
568+
```ts
569+
function disableCoverage(): void
570+
```
571+
572+
This method disables coverage collection for tests that run afterwards.
573+
556574
## experimental_parseSpecification <Version>4.0.0</Version> <Badge type="warning">experimental</Badge> {#parsespecification}
557575

558576
```ts

api/expect-typeof.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ expectTypeOf({ a: 1, b: 1 }).not.toEqualTypeOf<{ a: number }>()
3131

3232
- **类型:** `<T>(expected: T) => void`
3333

34+
<<<<<<< HEAD
3435
此匹配器检查期望类型是否扩展了提供的类型。它不同于 `toEqual`,更类似于 [expect's](/api/expect) `toMatchObject()`。使用此匹配器,你可以检查对象是否“匹配”类型。
36+
=======
37+
::: warning DEPRECATED
38+
This matcher has been deprecated since expect-type v1.2.0. Use [`toExtend`](#toextend) instead.
39+
:::
40+
This matcher checks if expect type extends provided type. It is different from `toEqual` and is more similar to [expect's](/api/expect) `toMatchObject()`. With this matcher, you can check if an object “matches” a type.
41+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
3542
3643
```ts
3744
import { expectTypeOf } from 'vitest'
@@ -41,6 +48,44 @@ expectTypeOf<number>().toMatchTypeOf<string | number>()
4148
expectTypeOf<string | number>().not.toMatchTypeOf<number>()
4249
```
4350

51+
## toExtend
52+
53+
- **Type:** `<T>(expected: T) => void`
54+
55+
This matcher checks if expect type extends provided type. It is different from `toEqual` and is more similar to [expect's](/api/expect) `toMatchObject()`. With this matcher, you can check if an object "matches" a type.
56+
57+
```ts
58+
import { expectTypeOf } from 'vitest'
59+
60+
expectTypeOf({ a: 1, b: 1 }).toExtend({ a: 1 })
61+
expectTypeOf<number>().toExtend<string | number>()
62+
expectTypeOf<string | number>().not.toExtend<number>()
63+
```
64+
65+
## toMatchObjectType
66+
67+
- **Type:** `() => void`
68+
69+
This matcher performs a strict check on object types, ensuring that the expected type matches the provided object type. It's stricter than [`toExtend`](#toextend) and is the recommended choice when working with object types as it's more likely to catch issues like readonly properties.
70+
71+
```ts
72+
import { expectTypeOf } from 'vitest'
73+
74+
expectTypeOf({ a: 1, b: 2 }).toMatchObjectType<{ a: number }>() // preferred
75+
expectTypeOf({ a: 1, b: 2 }).toExtend<{ a: number }>() // works but less strict
76+
77+
// Supports nested object checking
78+
const user = {
79+
name: 'John',
80+
address: { city: 'New York', zip: '10001' }
81+
}
82+
expectTypeOf(user).toMatchObjectType<{ name: string; address: { city: string } }>()
83+
```
84+
85+
::: warning
86+
This matcher only works with plain object types. It will fail for union types and other complex types. For those cases, use [`toExtend`](#toextend) instead.
87+
:::
88+
4489
## extract
4590

4691
- **类型:** `ExpectTypeOf<ExtractedUnion>`

guide/browser/config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export interface BrowserScript {
295295
}
296296
```
297297

298+
<<<<<<< HEAD
298299
## browser.testerScripts
299300

300301
- **类型:** `BrowserScript[]`
@@ -308,6 +309,8 @@ export interface BrowserScript {
308309

309310
脚本的 `src``content` 将由 Vite 插件处理。
310311

312+
=======
313+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
311314
## browser.commands
312315

313316
- **类型:** `Record<string, BrowserCommand>`

guide/features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ import { mount } from './mount.js'
246246

247247
test('my types work properly', () => {
248248
expectTypeOf(mount).toBeFunction()
249-
expectTypeOf(mount).parameter(0).toMatchTypeOf<{ name: string }>()
249+
expectTypeOf(mount).parameter(0).toExtend<{ name: string }>()
250250

251251
// @ts-expect-error name is a string
252252
assertType(mount({ name: 42 }))

guide/migration.md

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ export default defineConfig({
7878
- [覆盖率报告中的文件包含与排除](/guide/coverage.html#including-and-excluding-files-from-coverage-report)
7979
- [性能分析 | 代码覆盖率](/guide/profiling-test-performance.html#code-coverage) 了解调试覆盖率生成的方法
8080

81+
<<<<<<< HEAD
8182
### `spyOn` 支持构造函数
83+
=======
84+
### `spyOn` and `fn` Support Constructors
85+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
8286
8387
在之前版本中,如果你对构造函数使用 `vi.spyOn`,会收到类似 `Constructor <name> requires 'new'` 的错误。从 Vitest 4 开始,所有用 `new` 调用的 mock 都会正确创建实例,而不是调用 `mock.apply`。这意味着 mock 实现必须使用 `function``class` 关键字,例如:
8488

@@ -187,15 +191,63 @@ Vite 已提供外部化机制,但为降低破坏性,仍保留旧方案;[`s
187191

188192
未使用上述高级功能者,升级无感知。
189193

194+
<<<<<<< HEAD
190195
### 移除废弃的 API
196+
=======
197+
### `workspace` is Replaced with `projects`
198+
199+
The `workspace` configuration option was renamed to [`projects`](/guide/projects) in Vitest 3.2. They are functionally the same, except you cannot specify another file as the source of your workspace (previously you could specify a file that would export an array of projects). Migrating to `projects` is easy, just move the code from `vitest.workspace.js` to `vitest.config.ts`:
200+
201+
::: code-group
202+
```ts [vitest.config.js]
203+
import { defineConfig } from 'vitest/config'
204+
205+
export default defineConfig({
206+
test: {
207+
workspace: './vitest.workspace.js', // [!code --]
208+
projects: [ // [!code ++]
209+
'./packages/*', // [!code ++]
210+
{ // [!code ++]
211+
test: { // [!code ++]
212+
name: 'unit', // [!code ++]
213+
}, // [!code ++]
214+
}, // [!code ++]
215+
] // [!code ++]
216+
}
217+
})
218+
```
219+
```ts [vitest.workspace.js]
220+
import { defineWorkspace } from 'vitest/config' // [!code --]
221+
222+
export default defineWorkspace([ // [!code --]
223+
'./packages/*', // [!code --]
224+
{ // [!code --]
225+
test: { // [!code --]
226+
name: 'unit', // [!code --]
227+
}, // [!code --]
228+
} // [!code --]
229+
]) // [!code --]
230+
```
231+
:::
232+
233+
### Deprecated APIs are Removed
234+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
191235
192236
Vitest 4.0 移除了以下废弃的配置项:
193237

238+
<<<<<<< HEAD
194239
- `poolMatchGlobs` 配置项,请使用 [`projects`](/guide/projects) 代替。
195240
- `environmentMatchGlobs` 配置项,请使用 [`projects`](/guide/projects) 代替。
196241
- `workspace` 配置项,请使用 [`projects`](/guide/projects) 代替。
197242
- Reporter 的 API 例如 `onCollected`, `onSpecsCollected`, `onPathsCollected`, `onTaskUpdate``onFinished` 。查看 [`Reporters API`](/advanced/api/reporters) 了解替代方案。这些 API 在 Vitest v3.0.0 中引入。
198243
- 配置项 `deps.external`, `deps.inline`, `deps.fallbackCJS`。请改用 `server.deps.external`, `server.deps.inline``server.deps.fallbackCJS`
244+
=======
245+
- `poolMatchGlobs` config option. Use [`projects`](/guide/projects) instead.
246+
- `environmentMatchGlobs` config option. Use [`projects`](/guide/projects) instead.
247+
- Reporter APIs `onCollected`, `onSpecsCollected`, `onPathsCollected`, `onTaskUpdate` and `onFinished`. See [`Reporters API`](/advanced/api/reporters) for new alternatives. These APIs were introduced in Vitest `v3.0.0`.
248+
- `deps.external`, `deps.inline`, `deps.fallbackCJS` config options. Use `server.deps.external`, `server.deps.inline`, or `server.deps.fallbackCJS` instead.
249+
- `browser.testerScripts` config option. Use [`browser.testerHtmlPath`](/guide/browser/config#browser-testerhtmlpath) instead.
250+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
199251
200252
同时,所有弃用类型被一次性清理,彻底解决误引 `@types/node` 的问题([#5481](https://github.com/vitest-dev/vitest/issues/5481)[#6141](https://github.com/vitest-dev/vitest/issues/6141))。
201253

@@ -280,7 +332,11 @@ Vitest 的测试名使用 `>` 符号连接,方便区分测试与套件,而 J
280332

281333
### Done 回调
282334

335+
<<<<<<< HEAD
283336
从 Vitest v0.10.0 开始,回调式测试声明被弃用。你可以改写为使用 `async`/`await`,或用 Promise 模拟回调风格。
337+
=======
338+
Vitest does not support the callback style of declaring tests. You can rewrite them to use `async`/`await` functions, or use Promise to mimic the callback style.
339+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
284340
285341
<!--@include: ./examples/promise-done.md-->
286342

@@ -293,7 +349,11 @@ beforeEach(() => setActivePinia(createTestingPinia())) // [!code --]
293349
beforeEach(() => { setActivePinia(createTestingPinia()) }) // [!code ++]
294350
```
295351

352+
<<<<<<< HEAD
296353
Jest 中钩子顺序执行(逐个执行),Vitest 默认并行执行。若想使用 Jest 行为,可配置 [`sequence.hooks`](/config/#sequence-hooks)
354+
=======
355+
In Jest hooks are called sequentially (one after another). By default, Vitest runs hooks in a stack. To use Jest's behavior, update [`sequence.hooks`](/config/#sequence-hooks) option:
356+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
297357
298358
```ts
299359
export default defineConfig({
@@ -330,23 +390,20 @@ vi.setConfig({ testTimeout: 5_000 }) // [!code ++]
330390

331391
### Vue 快照
332392

393+
<<<<<<< HEAD
333394
这不是 Jest 特有功能,但如果之前使用 Jest 的 vue-cli preset,需要安装 [`jest-serializer-vue`](https://github.com/eddyerburgh/jest-serializer-vue) 包,并在 [setupFiles](/config/#setupfiles) 中使用:
395+
=======
396+
This is not a Jest-specific feature, but if you previously were using Jest with vue-cli preset, you will need to install [`jest-serializer-vue`](https://github.com/eddyerburgh/jest-serializer-vue) package, and specify it in [`snapshotSerializers`](/config/#snapshotserializers):
397+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
334398
335-
:::code-group
336-
```js [vite.config.js]
337-
import { defineConfig } from 'vite'
399+
```js [vitest.config.js]
400+
import { defineConfig } from 'vitest/config'
338401

339402
export default defineConfig({
340403
test: {
341-
setupFiles: ['./tests/unit/setup.js']
404+
snapshotSerializers: ['jest-serializer-vue']
342405
}
343406
})
344407
```
345-
```js [tests/unit/setup.js]
346-
import vueSnapshotSerializer from 'jest-serializer-vue'
347-
348-
expect.addSnapshotSerializer(vueSnapshotSerializer)
349-
```
350-
:::
351408

352409
否则快照中会出现大量转义的 `"` 字符。

guide/testing-types.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { mount } from './mount.js'
3030

3131
test('my types work properly', () => {
3232
expectTypeOf(mount).toBeFunction()
33-
expectTypeOf(mount).parameter(0).toMatchTypeOf<{ name: string }>()
33+
expectTypeOf(mount).parameter(0).toExtend<{ name: string }>()
3434

3535
// @ts-expect-error name is a string
3636
assertType(mount({ name: 42 }))
@@ -45,7 +45,11 @@ test('my types work properly', () => {
4545

4646
如果使用的是 `expectTypeOf` API,请参阅 [expect-type 关于其错误信息的文档](https://github.com/mmkal/expect-type#error-messages)
4747

48+
<<<<<<< HEAD
4849
当类型不匹配时,`.toEqualTypeOf``.toMatchTypeOf`会使用一种特殊的辅助类型来生成尽可能可操作的错误信息。但要理解它们还有一些细微差别。由于断言是 "流畅地 "编写的,所以失败应该发生在 "预期 "类型上,而不是 "实际 "类型上(`expect<Actual>().toEqualTypeOf<Expected>()`)。这意味着类型错误可能有点令人困惑,因此该库生成了一个 `MismatchInfo` 类型,试图明确说明期望是什么。例如
50+
=======
51+
When types don't match, `.toEqualTypeOf` and `.toExtend` use a special helper type to produce error messages that are as actionable as possible. But there's a bit of an nuance to understanding them. Since the assertions are written "fluently", the failure should be on the "expected" type, not the "actual" type (`expect<Actual>().toEqualTypeOf<Expected>()`). This means that type errors can be a little confusing - so this library produces a `MismatchInfo` type to try to make explicit what the expectation is. For example:
52+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
4953
5054
```ts
5155
expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: string }>()
@@ -91,7 +95,11 @@ expectTypeOf({ a: 1 }).toEqualTypeOf({ a: '' })
9195
expectTypeOf({ a: 1 }).toEqualTypeOf<{ a: string }>()
9296
```
9397

98+
<<<<<<< HEAD
9499
这是因为 TypeScript 编译器需要推断 `.toEqualTypeOf({a: ''})` 样式的类型参数,并且该库只能通过将其与通用的 `Mismatch` 类型进行比较来标记它为失败。因此,在可能的情况下,使用类型参数而不是具体类型来使用 `.toEqualTypeOf``toMatchTypeOf`。如果使用两个具体类型进行比较更加方便,可以使用 `typeof`
100+
=======
101+
This is because the TypeScript compiler needs to infer the typearg for the `.toEqualTypeOf({a: ''})` style, and this library can only mark it as a failure by comparing it against a generic `Mismatch` type. So, where possible, use a typearg rather than a concrete type for `.toEqualTypeOf` and `.toExtend`. If it's much more convenient to compare two concrete types, you can use `typeof`:
102+
>>>>>>> ec2e441b2e5176ed50172b0eed5ac79812b5c578
95103
96104
```ts
97105
const one = valueFromFunctionOne({ some: { complex: inputs } })

0 commit comments

Comments
 (0)