Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vitepress/components/Deprecated.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<Badge type="danger">
deprecated
弃用
</Badge>
</template>
4 changes: 2 additions & 2 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ export default ({ mode }: { mode: string }) => {
link: '/guide/environment',
},
{
text: 'Test Run Lifecycle',
text: '测试运行生命周期',
link: '/guide/lifecycle',
},
{
Expand Down Expand Up @@ -885,7 +885,7 @@ export default ({ mode }: { mode: string }) => {
link: '/guide/migration#jest',
},
{
text: 'Migrating from Mocha + Chai + Sinon',
text: 'Mocha + Chai + Sinon 迁移',
link: '/guide/migration#mocha-chai-sinon',
},
],
Expand Down
12 changes: 5 additions & 7 deletions api/advanced/runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,17 @@ Vitest 提供了 `createTaskCollector` 工具来创建您自己的 `test` 方法

任务是套件的一部分对象。它会通过 `suite.task` 方法自动添加到当前套件中:

<!-- TODO: translation -->

```js [custom.js]
export { afterAll, beforeAll, describe, TestRunner } from 'vitest'

// this function will be called during collection phase:
// don't call function handler here, add it to suite tasks
// with "getCurrentSuite().task()" method
// note: createTaskCollector provides support for "todo"/"each"/...
// 此函数将在收集阶段被调用:
// 不要在此处调用函数处理器,而应通过
// "getCurrentSuite().task()" 方法将其添加到套件任务中
// 注意:createTaskCollector 提供了对 "todo"/"each"/... 的支持
export const myCustomTask = TestRunner.createTaskCollector(
function (name, fn, timeout) {
TestRunner.getCurrentSuite().task(name, {
...this, // so "todo"/"skip"/... is tracked correctly
...this, // 以便正确追踪 "todo"/"skip"/...
meta: {
customPropertyToDifferentiateTask: true
},
Expand Down
6 changes: 2 additions & 4 deletions api/advanced/test-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,13 @@ test('the validation works correctly', ({ task }) => {
task.meta.decorated = false
})
```
<!-- TODO: translation reference history -->

If the test did not finish running yet, the meta will be an empty object, unless it has static meta:
如果测试尚未运行完毕,元数据将是一个空对象,除非它定义了静态元数据:

```ts
test('the validation works correctly', { meta: { decorated: true } })
```

Since Vitest 4.1, Vitest inherits [`meta`](/api/advanced/test-suite#meta) property defined on the [suite](/api/advanced/test-suite).
Vitest 4.1 起,Vitest 会继承定义在 [套件](/api/advanced/test-suite) 上的 [`meta`](/api/advanced/test-suite#meta) 属性。

## result

Expand Down
14 changes: 4 additions & 10 deletions api/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
type Awaitable<T> = T | PromiseLike<T>
```

<!-- TODO: translation reference history -->

`expect` is used to create assertions. In this context `assertions` are functions that can be called to assert a statement. Vitest provides `chai` assertions by default and also `Jest` compatible assertions built on top of `chai`. Since Vitest 4.1, for spy/mock testing, Vitest also provides [Chai-style assertions](#chai-style-spy-assertions) (e.g., `expect(spy).to.have.been.called()`) alongside Jest-style assertions (e.g., `expect(spy).toHaveBeenCalled()`). Unlike `Jest`, Vitest supports a message as the second argument - if the assertion fails, the error message will be equal to it.
`expect` 用于创建断言。在此上下文中,`断言` 是可以被调用来验证一个语句的函数。Vitest 默认提供 `chai` 断言,同时也提供基于 chai 构建的兼容 `Jest` 的断言。自 Vitest 4.1 起,在进行 spy/mock 测试时,Vitest 还额外提供了 [Chai 风格断言](#chai-style-spy-assertions)(如 `expect(spy).to.have.been.called()`),与 Jest 风格断言(如 `expect(spy).toHaveBeenCalled()`)并存。与 `Jest` 不同,Vitest 支持将一条消息作为第二个参数传入,如果断言失败,错误信息将等于该消息。

```ts
export interface ExpectStatic extends Chai.ExpectStatic, AsymmetricMatchersContaining {
Expand All @@ -33,7 +31,7 @@ expect(input).to.equal(2) // chai API
expect(input).toBe(2) // jest API
```

从技术上讲,这个示例没有使用 [`test`](/api/#test) 函数,因此在控制台中你将看到 Nodejs 错误而不是 Vitest 输出。 要了解更多关于 `test` 的信息,请参阅 [Test API](/api/test)。
从技术上讲,这个示例没有使用 [`test`](/api/test) 函数,因此在控制台中你将看到 Nodejs 错误而不是 Vitest 输出。 要了解更多关于 `test` 的信息,请参阅 [Test API](/api/test)。
Comment thread
NoiseFan marked this conversation as resolved.

此外,`expect` 可以静态地使用来访问匹配器函数,稍后将会介绍。

Expand Down Expand Up @@ -792,9 +790,7 @@ test('the number of elements must match exactly', () => {

- `RegExp`: 错误消息匹配该模式
- `string`: 错误消息包含该子字符串
- any other value: compare with thrown value using deep equality (similar to `toEqual`)

<!-- TODO: translation -->
- 任何其他值:使用深度相等与抛出的值进行比较(等价于 `toEqual`)

:::tip
必须将代码包装在一个函数中,否则错误将无法被捕获,测试将失败。
Expand Down Expand Up @@ -855,10 +851,8 @@ test('throws on pineapples', async () => {

:::

<!-- TODO: translation -->

:::tip
You can also test non-Error values that are thrown:
你也可以测试抛出的非 Error 值:

```ts
test('throws non-Error values', () => {
Expand Down
Loading
Loading