Skip to content

Commit 287d45f

Browse files
authored
Merge pull request #995 from vitest-dev/sync-8430ac3c-1
docs(en): merge docs-cn/sync-docs into docs-cn/dev @ 8430ac3
2 parents 156801c + 1567992 commit 287d45f

8 files changed

Lines changed: 72 additions & 6 deletions

File tree

.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ export default ({ mode }: { mode: string }) => {
515515
text: 'retry',
516516
link: '/config/retry',
517517
},
518+
{
519+
text: 'repeats',
520+
link: '/config/repeats',
521+
},
518522
{
519523
text: 'onConsoleLog',
520524
link: '/config/onconsolelog',

api/assert.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
# assert
22

33
Vitest 从 [`chai`](https://www.chaijs.com/api/assert/) 重新导出了 `assert` 方法,用于验证不变量。
4+
<!-- TODO: translation -->
5+
::: warning In-Source Testing {#in-source-testing}
6+
When using [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) such as `assert` from `import.meta.vitest` in [in-source tests](/guide/in-source), TypeScript reports error `TS2775` because they must be called via an explicitly annotated name. Annotate the variable with `Chai.Assert` or call it directly:
7+
8+
::: code-group
9+
```ts [Annotated variable]
10+
if (import.meta.vitest) {
11+
const { test, assert } = import.meta.vitest // [!code --]
12+
const { test } = import.meta.vitest // [!code ++]
13+
const assert: Chai.Assert = import.meta.vitest.assert // [!code ++]
14+
15+
test('assert', () => {
16+
assert('foo' !== 'bar', 'foo should not be equal to bar')
17+
})
18+
}
19+
```
20+
```ts [Direct call]
21+
if (import.meta.vitest) {
22+
const { test, assert } = import.meta.vitest // [!code --]
23+
const { test } = import.meta.vitest // [!code ++]
24+
25+
test('assert', () => {
26+
assert('foo' !== 'bar', 'foo should not be equal to bar') // [!code --]
27+
import.meta.vitest!.assert('foo' !== 'bar', 'foo should not be equal to bar') // [!code ++]
28+
})
29+
}
30+
```
31+
:::
432

533
## assert
634

config/repeats.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: repeats | Config
3+
outline: deep
4+
---
5+
6+
# repeats
7+
<!-- TODO: translation -->
8+
- **Type:** `number`
9+
- **Default:** `0`
10+
- **CLI:** `--repeats=<number>`
11+
12+
Repeat every test a specific number of times regardless of the result. A test that uses the [`repeats`](/api/test#repeats) test option takes precedence over this value.
13+
14+
This is useful for verifying that tests are stable across multiple runs. If a test fails on any repetition, the whole test is reported as failed.

guide/browser/index.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ export default defineConfig({
119119

120120
::: info
121121
Vitest 默认分配端口号 `63315` 以避免与开发服务器冲突,允许我们同时并行运行两者。我们可以通过 [`browser.api`](/config/browser/api) 选项来更改这个端口号。
122-
123-
CLI 不会自动打印 Vite 服务器 URL。在观察模式下运行时,你可以按 "b" 键来打印 URL。
124122
:::
125123

126124
如果之前未使用过 Vite,请确保已安装框架插件并在配置中指定。有些框架可能需要额外配置才能运行,请查看其 Vite 相关文档以确定。

guide/cli-generated.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,13 @@ UI 模式和 HTML 报告器中提供的 HTML 覆盖率输出目录。
636636
- **配置:** [retry.condition](/config/retry#retry-condition)
637637

638638
触发重试操作的错误信息匹配正则表达式。仅当错误信息符合该模式时才会执行重试(默认值:所有错误都会触发重试)
639+
<!-- TODO: translation -->
640+
### repeats
641+
642+
- **CLI:** `--repeats <number>`
643+
- **Config:** [repeats](/config/repeats)
644+
645+
Repeat every test a specific number of times regardless of the result (default: `0`)
639646

640647
### diff.aAnnotation
641648

guide/in-source.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ module.exports = {
154154
```
155155

156156
完整的示例请参考 [`examples/in-source-test`](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test)
157+
<!-- TODO: translation -->
158+
::: warning
159+
There is a limitation when using [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) such as `assert` in in-source tests. See [`assert`](/api/assert#in-source-testing) for details and workarounds.
160+
:::
157161

158162
## 说明 {#notes}
159163

guide/learn/setup-teardown.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ test('items starts with 3 fruits', () => {
3333
expect(items).toHaveLength(3)
3434
})
3535

36+
test('can remove an item', () => {
37+
items.pop()
38+
expect(items).toHaveLength(2)
39+
})
40+
3641
test('can add an item', () => {
3742
items.push('date')
3843
expect(items).toHaveLength(4)
39-
// afterEach 会为下一个测试重置项目,
40-
// 因此此处的修改不会影响到其他测试
44+
// beforeEach reset the array to 3 items before this test ran,
45+
// proving that mutations from the previous test do not leak.
4146
})
4247
```
43-
44-
如果没有这些钩子,第二个测试的 `push` 操作会影响其后的所有测试,这是导致测试不稳定的典型原因。这些钩子确保了每个测试都拥有干净的状态。
48+
<!-- TODO: translation -->
49+
Without these hooks, mutations like `pop` or `push` from earlier tests would affect subsequent ones, which is a classic source of flaky tests, while the hooks guarantee clean state for every test.
4550

4651
## 一次性初始化 {#one-time-setup}
4752

guide/migration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ $ cd subdir && vitest --config ../vitest.config.ts # [!code ++]
138138

139139
Assignments to properties on `globalThis` or `window` in `jsdom` and `happy-dom` environments are now propagated to the underlying DOM implementation. Mutable properties such as `innerWidth` can affect APIs implemented by the DOM environment, for example `happy-dom`'s `matchMedia`.
140140

141+
### Browser Orchestrator URL Requires a Session
142+
143+
Vitest no longer serves the browser orchestrator UI from a bare `/__vitest_test__/` URL. Browser runner URLs are now session-bound and must include the `sessionId` generated by Vitest, for example `/__vitest_test__/?sessionId=...`.
144+
145+
If you manually opened the browser preview by copying the Vite server URL or visiting `/__vitest_test__/` directly, use the URL opened or printed by Vitest instead.
146+
141147
## 迁移至 Vitest 4.0 {#vitest-4}
142148

143149
::: warning 前提条件

0 commit comments

Comments
 (0)