Skip to content

Commit a50f092

Browse files
committed
docs(en): merging all conflicts
2 parents a120a52 + 8c11432 commit a50f092

21 files changed

+461
-17
lines changed

.vitepress/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export default ({ mode }: { mode: string }) => {
103103
},
104104
}),
105105
],
106+
languages: ['js', 'jsx', 'ts', 'tsx'],
106107
},
107108
themeConfig: {
108109
logo: '/logo.svg',
@@ -342,7 +343,15 @@ export default ({ mode }: { mode: string }) => {
342343
],
343344
},
344345
{
346+
<<<<<<< HEAD
345347
text: '运行器「Runner」 API',
348+
=======
349+
text: 'Plugin API',
350+
link: '/advanced/api/plugin',
351+
},
352+
{
353+
text: 'Runner API',
354+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
346355
link: '/advanced/runner',
347356
},
348357
{

advanced/api/plugin.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: Plugin API
3+
outline: deep
4+
---
5+
6+
# Plugin API <Version>3.1.0</Version> {#plugin-api}
7+
8+
::: warning
9+
This is an advanced API. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors.
10+
11+
This guide assumes you know how to work with [Vite plugins](https://vite.dev/guide/api-plugin.html).
12+
:::
13+
14+
Vitest supports an experimental `configureVitest` [plugin](https://vite.dev/guide/api-plugin.html) hook hook since version 3.1. Any feedback regarding this API is welcome in [GitHub](https://github.com/vitest-dev/vitest/discussions/7104).
15+
16+
::: code-group
17+
```ts [only vitest]
18+
import type { Vite, VitestPluginContext } from 'vitest/node'
19+
20+
export function plugin(): Vite.Plugin {
21+
return {
22+
name: 'vitest:my-plugin',
23+
configureVitest(context: VitestPluginContext) {
24+
// ...
25+
}
26+
}
27+
}
28+
```
29+
```ts [vite and vitest]
30+
/// <reference types="vitest/config" />
31+
32+
import type { Plugin } from 'vite'
33+
34+
export function plugin(): Plugin {
35+
return {
36+
name: 'vitest:my-plugin',
37+
transform() {
38+
// ...
39+
},
40+
configureVitest(context) {
41+
// ...
42+
}
43+
}
44+
}
45+
```
46+
:::
47+
48+
::: tip TypeScript
49+
Vitest re-exports all Vite type-only imports via a `Vite` namespace, which you can use to keep your versions in sync. However, if you are writing a plugin for both Vite and Vitest, you can continue using the `Plugin` type from the `vite` entrypoint. Just make sure you have `vitest/config` referenced somewhere so that `configureVitest` is augmented correctly:
50+
51+
```ts
52+
/// <reference types="vitest/config" />
53+
```
54+
:::
55+
56+
Unlike [`reporter.onInit`](/advanced/api/reporters#oninit), this hooks runs early in Vitest lifecycle allowing you to make changes to configuration like `coverage` and `reporters`. A more notable change is that you can manipulate the global config from a [workspace project](/guide/workspace) if your plugin is defined in the project and not in the global config.
57+
58+
## Context
59+
60+
### project
61+
62+
The current [test project](./test-project) that the plugin belongs to.
63+
64+
::: warning Browser Mode
65+
Note that if you are relying on a browser feature, the `project.browser` field is not set yet. Use [`reporter.onBrowserInit`](./reporters#onbrowserinit) event instead.
66+
:::
67+
68+
### vitest
69+
70+
The global [Vitest](./vitest) instance. You can change the global configuration by directly mutating the `vitest.config` property:
71+
72+
```ts
73+
vitest.config.coverage.enabled = false
74+
vitest.config.reporters.push([['my-reporter', {}]])
75+
```
76+
77+
::: warning Config is Resolved
78+
Note that Vitest already resolved the config, so some types might be different from the usual user configuration. This also means that some properties will not be resolved again, like `setupFile`. If you are adding new files, make sure to resolve it first.
79+
80+
At this point reporters are not created yet, so modifying `vitest.reporters` will have no effect because it will be overwritten. If you need to inject your own reporter, modify the config instead.
81+
:::
82+
83+
### injectTestProjects
84+
85+
```ts
86+
function injectTestProjects(
87+
config: TestProjectConfiguration | TestProjectConfiguration[]
88+
): Promise<TestProject[]>
89+
```
90+
91+
This methods accepts a config glob pattern, a filepath to the config or an inline configuration. It returns an array of resolved [test projects](./test-project).
92+
93+
```ts
94+
// inject a single project with a custom alias
95+
const newProjects = await injectTestProjects({
96+
// you can inherit the current project config by referencing `configFile`
97+
// note that you cannot have a project with the name that already exists,
98+
// so it's a good practice to define a custom name
99+
configFile: project.vite.config.configFile,
100+
test: {
101+
name: 'my-custom-alias',
102+
alias: {
103+
customAlias: resolve('./custom-path.js'),
104+
},
105+
},
106+
})
107+
```
108+
109+
::: warning Projects are Filtered
110+
Vitest filters projects during the config resolution, so if the user defined a filter, injected project might not be resolved unless it [matches the filter](./vitest#matchesprojectfilter). You can update the filter via the `vitest.config.project` option to always include your workspace project:
111+
112+
```ts
113+
vitest.config.project.push('my-project-name')
114+
```
115+
116+
Note that this will only affect projects injected with [`injectTestProjects`](#injecttestprojects) method.
117+
:::
118+
119+
::: tip Referencing the Current Config
120+
If you want to keep the user configuration, you can specify the `configFile` property. All other properties will be merged with the user defined config.
121+
122+
The project's `configFile` can be accessed in Vite's config: `project.vite.config.configFile`.
123+
124+
Note that this will also inherit the `name` - Vitest doesn't allow multiple projects with the same name, so this will throw an error. Make sure you specified a different name. You can access the current name via the `project.name` property and all used names are available in the `vitest.projects` array.
125+
:::

advanced/api/reporters.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ function onInit(vitest: Vitest): Awaitable<void>
5252
当 [Vitest](/advanced/api/vitest) 初始化或启动时,但在测试被过滤之前,会调用此方法。
5353

5454
::: info
55+
<<<<<<< HEAD
5556
在内部,此方法在 [`vitest.start`](/advanced/api/vitest#start)、[`vitest.init`](/advanced/api/vitest#init) 或 [`vitest.mergeReports`](/advanced/api/vitest#mergereports) 中调用。如果我们使用的是编程式 API,请确保根据需要在调用 [`vitest.runTestSpecifications`](/advanced/api/vitest#runtestspecifications) 之前调用其中之一。内置的 CLI 将始终按正确顺序运行方法。
57+
=======
58+
Internally this method is called inside [`vitest.start`](/advanced/api/vitest#start), [`vitest.init`](/advanced/api/vitest#init) or [`vitest.mergeReports`](/advanced/api/vitest#mergereports). If you are using programmatic API, make sure to call either one depending on your needs before calling [`vitest.runTestSpecifications`](/advanced/api/vitest#runtestspecifications), for example. Built-in CLI will always run methods in correct order.
59+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
5660
:::
5761

5862
请注意,我们还可以通过 [`project`](/advanced/api/test-project) 属性从测试用例、套件和测试模块中访问 `vitest` 实例,但在此方法中存储对 `vitest` 的引用也可能有用。
@@ -137,9 +141,15 @@ function onTestRunEnd(
137141

138142
第三个参数指示测试运行结束的原因:
139143

144+
<<<<<<< HEAD
140145
- `passed`:测试运行正常结束,没有错误
141146
- `failed`:测试运行至少有一个错误(由于收集期间的语法错误或测试执行期间的实际错误)
142147
- `interrupted`:测试被 [`vitest.cancelCurrentRun`](/advanced/api/vitest#cancelcurrentrun) 调用中断,或者在终端中按下了 `Ctrl+C`(请注意,在这种情况下仍可能有失败的测试)
148+
=======
149+
- `passed`: test run was finished normally and there are no errors
150+
- `failed`: test run has at least one error (due to a syntax error during collection or an actual error during test execution)
151+
- `interrupted`: test was interrupted by [`vitest.cancelCurrentRun`](/advanced/api/vitest#cancelcurrentrun) call or `Ctrl+C` was pressed in the terminal (note that it's still possible to have failed tests in this case)
152+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
143153

144154
如果 Vitest 没有找到任何要运行的测试文件,此事件将以空的模块和错误数组调用,状态将取决于 [`config.passWithNoTests`](/config/#passwithnotests) 的值。
145155

advanced/api/test-project.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ const value = inject('key')
124124
```
125125
:::
126126

127+
<<<<<<< HEAD
127128
这些值可以动态提供。测试中提供的值将在下次运行时更新。
129+
=======
130+
The values can be provided dynamically. Provided value in tests will be updated on their next run.
131+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
128132

129133
::: tip
130134
此方法也可用于 [全局设置文件](/config/#globalsetup),以便在无法使用公共 API 的情况下使用:

advanced/api/test-specification.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Vite 模块图中的模块 ID。通常,它是一个使用 POSIX 分隔符的
3333

3434
## testModule
3535

36-
Instance of [`TestModule`](/advanced/api/test-module) assosiated with the specification. If test wasn't queued yet, this will be `undefined`.
36+
Instance of [`TestModule`](/advanced/api/test-module) associated with the specification. If test wasn't queued yet, this will be `undefined`.
3737

3838
## pool <Badge type="warning">experimental</Badge> {#pool}
3939

advanced/api/vitest.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ const testCase = vitest.state.getReportedEntity(task) // 新 API
9393

9494
全局快照管理器。Vitest 使用 `snapshot.add` 方法跟踪所有快照。
9595

96+
<<<<<<< HEAD
9697
我们可以通过 `vitest.snapshot.summay` 属性获取快照的最新摘要。
98+
=======
99+
You can get the latest summary of snapshots via the `vitest.snapshot.summary` property.
100+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
97101
98102
## cache
99103

@@ -130,7 +134,11 @@ function provide<T extends keyof ProvidedContext & string>(
130134

131135
Vitest 公开了 `provide` 方法,它是 `vitest.getRootProject().provide` 的简写。通过此方法,我们可以从主线程传递值到测试中。所有值在存储之前都通过 `structuredClone` 进行检查,但值本身不会被克隆。
132136

137+
<<<<<<< HEAD
133138
要在测试中接收这些值,我们需要从 `vitest` 入口点导入 `inject` 方法:
139+
=======
140+
To receive the values in the test, you need to import `inject` method from `vitest` entrypoint:
141+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
134142

135143
```ts
136144
import { inject } from 'vitest'
@@ -155,7 +163,11 @@ declare module 'vitest' {
155163
```
156164

157165
::: warning
166+
<<<<<<< HEAD
158167
从技术上讲,`provide` 是 [`TestProject`](/advanced/api/test-project) 的方法,因此它仅限于特定项目。然而,所有项目都继承自核心项目的值,这使得 `vitest.provide` 成为传递值到测试的通用方式。
168+
=======
169+
Technically, `provide` is a method of [`TestProject`](/advanced/api/test-project), so it is limited to the specific project. However, all projects inherit the values from the root project which makes `vitest.provide` universal way of passing down values to tests.
170+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
159171
:::
160172

161173
## getProvidedContext
@@ -172,7 +184,11 @@ function getProvidedContext(): ProvidedContext
172184
function getProjectByName(name: string): TestProject
173185
```
174186

187+
<<<<<<< HEAD
175188
此方法通过名称返回项目。类似于调用 `vitest.projects.find`
189+
=======
190+
This method returns the project by its name. Similar to calling `vitest.projects.find`.
191+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
176192

177193
::: warning
178194
如果项目不存在,此方法将返回根项目 - 请确保再次检查返回的项目是否是我们要找的项目。
@@ -297,7 +313,11 @@ function getModuleSpecifications(moduleId: string): TestSpecification[]
297313
function clearSpecificationsCache(moduleId?: string): void
298314
```
299315

316+
<<<<<<< HEAD
300317
当调用 [`globTestSpecifications`](#globtestspecifications) 或 [`runTestSpecifications`](#runtestspecifications) 时,Vitest 会自动缓存每个文件的测试规范。此方法清除给定文件的缓存或整个缓存,具体取决于第一个参数。
318+
=======
319+
Vitest automatically caches test specifications for each file when [`globTestSpecifications`](#globtestspecifications) or [`runTestSpecifications`](#runtestspecifications) is called. This method clears the cache for the given file or the whole cache altogether depending on the first argument.
320+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
301321

302322
## runTestSpecifications
303323

@@ -452,7 +472,11 @@ function exit(force = false): Promise<void>
452472

453473
关闭所有项目并退出进程。如果 `force` 设置为 `true`,则进程将在关闭项目后立即退出。
454474

475+
<<<<<<< HEAD
455476
如果进程在 [`config.teardownTimeout`](/config/#teardowntimeout) 毫秒后仍然处于活动状态,此方法还将强制调用 `process.exit()`
477+
=======
478+
This method will also forcefully call `process.exit()` if the process is still active after [`config.teardownTimeout`](/config/#teardowntimeout) milliseconds.
479+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
456480

457481
## shouldKeepServer
458482

@@ -517,4 +541,18 @@ vitest.onFilterWatchedSpecification(specification =>
517541
)
518542
```
519543

544+
<<<<<<< HEAD
520545
Vitest 可以根据 `pool``locations` 选项为同一文件创建不同的规范,因此不要依赖引用。Vitest 还可以从 [`vitest.getModuleSpecifications`](#getmodulespecifications) 返回缓存的规范 - 缓存基于 `moduleId``pool`。请注意,[`project.createSpecification`](/advanced/api/test-project#createspecification) 总是返回一个新实例。
546+
=======
547+
Vitest can create different specifications for the same file depending on the `pool` or `locations` options, so do not rely on the reference. Vitest can also return cached specification from [`vitest.getModuleSpecifications`](#getmodulespecifications) - the cache is based on the `moduleId` and `pool`. Note that [`project.createSpecification`](/advanced/api/test-project#createspecification) always returns a new instance.
548+
549+
## matchesProjectFilter <Version>3.1.0</Version> {#matchesprojectfilter}
550+
551+
```ts
552+
function matchesProjectFilter(name: string): boolean
553+
```
554+
555+
Check if the name matches the current [project filter](/guide/cli#project). If there is no project filter, this will always return `true`.
556+
557+
It is not possible to programmatically change the `--project` CLI option.
558+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69

advanced/guide/tests.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ finally {
7171
}
7272
```
7373

74+
<<<<<<< HEAD
7475
如果你打算保留 `Vitest` 实例,请确保至少调用 [`init`](/advanced/api/vitest#init)。这将初始化报告器和覆盖率提供者,但不会运行任何测试。即使你不打算使用 Vitest 监视器,但希望保持实例运行,也建议启用 `watch` 模式。Vitest 依赖于这个标志,以确保某些功能在持续过程中正常工作。
76+
=======
77+
If you intend to keep the `Vitest` instance, make sure to at least call [`init`](/advanced/api/vitest#init). This will initialise reporters and the coverage provider, but won't run any tests. It is also recommended to enable the `watch` mode even if you don't intend to use the Vitest watcher, but want to keep the instance running. Vitest relies on this flag for some of its features to work correctly in a continuous process.
78+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
7579
7680
报告器初始化后,如果需要手动运行测试,可以使用 [`runTestSpecifications`](/advanced/api/vitest#runtestspecifications)[`rerunTestSpecifications`](/advanced/api/vitest#reruntestspecifications) 来运行测试。
7781

@@ -88,7 +92,11 @@ watcher.on('change', async (file) => {
8892
```
8993

9094
::: warning
95+
<<<<<<< HEAD
9196
上述示例展示了如果你禁用默认监视器行为的一个潜在用例。默认情况下,Vitest 在文件发生变化时已经会重新运行测试。
97+
=======
98+
The example above shows a potential use-case if you disable the default watcher behaviour. By default, Vitest already reruns tests if files change.
99+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
92100
93101
另外请注意,`getModuleSpecifications` 不会解析测试文件,除非这些文件已经通过 `globTestSpecifications` 处理过。如果文件刚刚创建,应使用 `project.matchesGlobPattern`
94102

api/index.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ test('skipped test', (context) => {
134134
})
135135
```
136136

137+
Since Vitest 3.1, if the condition is unknonwn, you can provide it to the `skip` method as the first arguments:
138+
139+
```ts
140+
import { assert, test } from 'vitest'
141+
142+
test('skipped test', (context) => {
143+
context.skip(Math.random() < 0.5, 'optional message')
144+
// Test skipped, no error
145+
assert.equal(Math.sqrt(4), 3)
146+
})
147+
```
148+
137149
### test.skipIf
138150

139151
- **类型:** `(condition: any) => Test`
@@ -344,7 +356,8 @@ test.fails('fail test', async () => {
344356
- `%f`: floating point value
345357
- `%j`: json
346358
- `%o`: object
347-
- `%#`: index of the test case
359+
- `%#`: 0-based index of the test case
360+
- `%$`: 1-based index of the test case
348361
- `%%`: single percent sign ('%')
349362

350363
```ts
@@ -364,7 +377,11 @@ test.each([
364377
// ✓ add(2, 1) -> 3
365378
```
366379

380+
<<<<<<< HEAD
367381
如果使用对象作为参数,也可以使用前缀 `$` 访问对象属性:
382+
=======
383+
You can also access object properties and array elements with `$` prefix:
384+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
368385
369386
```ts
370387
test.each([
@@ -374,6 +391,22 @@ test.each([
374391
])('add($a, $b) -> $expected', ({ a, b, expected }) => {
375392
expect(a + b).toBe(expected)
376393
})
394+
<<<<<<< HEAD
395+
=======
396+
397+
// this will return
398+
// ✓ add(1, 1) -> 2
399+
// ✓ add(1, 2) -> 3
400+
// ✓ add(2, 1) -> 3
401+
402+
test.each([
403+
[1, 1, 2],
404+
[1, 2, 3],
405+
[2, 1, 3],
406+
])('add($0, $1) -> $2', (a, b, expected) => {
407+
expect(a + b).toBe(expected)
408+
})
409+
>>>>>>> 8c114323d7389495d09c1f8e137101ca70841c69
377410

378411
// 这将返回
379412
// ✓ add(1, 1) -> 2

0 commit comments

Comments
 (0)