Skip to content
Closed
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
123 changes: 123 additions & 0 deletions api/advanced/vitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ function start(filters?: string[]): Promise<TestRunResult>
function standalone(): Promise<void>
```

<<<<<<< HEAD
- **别名:**: `init` <Deprecated />
=======
- **Alias:** `init` <Deprecated />
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

初始化报告器和覆盖率提供者。此方法不运行任何测试。如果提供了 `--watch` 标志,Vitest 仍将运行更改的测试,即使未调用此方法。

Expand Down Expand Up @@ -696,3 +700,122 @@ export interface SourceModuleDiagnostic {
::: warning
[浏览器模式](/guide/browser/) 暂不支持。
:::

## createReport <Version>5.0.0</Version> {#createreport}

```ts
function createReport(scope: string): Report
```

Creates a report that is limited to the given scope. `Report` follows Vitest's rules around [Storing artifacts on file system](/guide/advanced/reporters.html#storing-artifacts-on-file-system).

`Report` provides collection of utilities for writing test results, temporary files and other artifacts on the file system. It's especially intended for third party integrations like custom reporters.

All operations of `Report` are limited to given `scope`. A single report cannot interfere with other reports. Internally Vitest creates `.vitest` directory where each `scope` creates their own directory. This convention of `.vitest` directory reduces the amount of entries end-users need to specify in their `.gitignore`.

```ts
import type { Report } from 'vitest/node'

const scope = 'example-yaml-reporter'

// Automatically creates `<project-root>/.vitest/example-yaml-reporter/`
// directory if it does not exist already
const report: Report = vitest.createReport(scope)
```

### Report.root

```ts
const root: string
```

The root directory for this scope.

```ts
const report = vitest.createReport('my-json-reporter')

// Is <project-root>/.vitest/my-json-reporter
const root = report.root
```


### Report.clean

```ts
function clean(): Promise<void>
```

Clean up the report directory for this scope.

```ts
const report = vitest.createReport('my-json-reporter')

// Removes everything inside <project-root>/.vitest/my-json-reporter/
await report.clean()
```

### Report.writeFile

```ts
function writeFile(
filename: string,
content: string | Uint8Array,
encoding?: BufferEncoding
): Promise<void>
```

Write a file to the report directory for this scope. By default the file will be written with UTF-8 encoding. The filename is relative to the scope directory.

```ts
const report = vitest.createReport('my-json-reporter')

// Writes file to .vitest/my-json-reporter/test-report.json
await report.writeFile('test-report.json', JSON.stringify(results))
```

### Report.readFile

```ts
function readFile(filename: string, encoding?: BufferEncoding): Promise<string>
```

Read a file from the report directory for this scope.

```ts
const report = vitest.createReport('my-json-reporter')

// Reads file from .vitest/my-json-reporter/test-report.json
const content: string = await report.readFile('test-report.json')
```

### Report.readdir

```ts
function readdir(): Promise<string[]>
```

Read contents of the report directory for this scope.

```ts
const report = vitest.createReport('my-json-reporter')

// Reads contents from .vitest/my-json-reporter
const filenames: string[] = await report.readdir()
```

### Report.delete

<!-- eslint-skip -->
```ts
function delete(filename: string): Promise<void>
```

Delete a file from the report directory for this scope.

```ts
const report = vitest.createReport('my-json-reporter')

// Deletes file from .vitest/my-json-reporter/test-report.json
await report.delete('test-report.json')
```

10 changes: 10 additions & 0 deletions api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ test('assert.fail', () => {

## isOk

<<<<<<< HEAD
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Resolve merge conflict markers before publishing docs

This commit leaves raw Git conflict markers in rendered documentation (for example <<<<<<< HEAD at this location), which means users will see unresolved merge artifacts and conflicting content variants instead of a single authoritative doc. The same pattern appears across many files in this change set, so the docs output is materially corrupted until these conflicts are resolved.

Useful? React with 👍 / 👎.

- **类型:** `<T>(value: T, message?: string) => asserts value`
- **Alias** `ok`
=======
- **Type:** `<T>(value: T, message?: string) => asserts value`
- **Alias:** `ok`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

断言给定的 `value` 是 true 。

Expand All @@ -51,8 +56,13 @@ test('assert.isOk', () => {

## isNotOk

<<<<<<< HEAD
- **类型:** `<T>(value: T, message?: string) => void`
- **Alias** `notOk`
=======
- **Type:** `<T>(value: T, message?: string) => void`
- **Alias:** `notOk`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

断言给定的 `value` 是 false 。

Expand Down
68 changes: 68 additions & 0 deletions api/expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,11 @@ test('spy function', () => {

## toHaveBeenCalledTimes

<<<<<<< HEAD
- **类型**: `(amount: number) => Awaitable<void>`
=======
- **Type:** `(amount: number) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数是否被调用了特定次数。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1102,7 +1106,11 @@ test('spy function called two times', () => {

## toHaveBeenCalledWith

<<<<<<< HEAD
- **类型**: `(...args: any[]) => Awaitable<void>`
=======
- **Type:** `(...args: any[]) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数是否至少一次被调用,并带有特定的参数。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1128,7 +1136,11 @@ test('spy function', () => {

## toHaveBeenCalledBefore

<<<<<<< HEAD
- **类型**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
=======
- **Type:** `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查一个 `Mock` 是否在另一个 `Mock` 之前被调用。

Expand All @@ -1147,7 +1159,11 @@ test('calls mock1 before mock2', () => {

## toHaveBeenCalledAfter

<<<<<<< HEAD
- **类型**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
=======
- **Type:** `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查一个 `Mock` 是否在另一个 `Mock` 之后被调用。

Expand All @@ -1166,7 +1182,11 @@ test('calls mock1 after mock2', () => {

## toHaveBeenCalledExactlyOnceWith

<<<<<<< HEAD
- **类型**: `(...args: any[]) => Awaitable<void>`
=======
- **Type:** `(...args: any[]) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数是否恰好被调用了一次,并且带有特定的参数。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1190,7 +1210,11 @@ test('spy function', () => {

## toHaveBeenLastCalledWith

<<<<<<< HEAD
- **类型**: `(...args: any[]) => Awaitable<void>`
=======
- **Type:** `(...args: any[]) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数在其最后一次调用时是否被传入了特定的参数。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1216,7 +1240,11 @@ test('spy function', () => {

## toHaveBeenNthCalledWith

<<<<<<< HEAD
- **类型**: `(time: number, ...args: any[]) => Awaitable<void>`
=======
- **Type:** `(time: number, ...args: any[]) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数是否在特定的次数被调用时带有特定的参数。计数从 1 开始。因此,要检查第二次调用,我们需要写成 `.toHaveBeenNthCalledWith(2, ...)`。

Expand All @@ -1243,7 +1271,11 @@ test('first call of spy function called with right params', () => {

## toHaveReturned

<<<<<<< HEAD
- **类型**: `() => Awaitable<void>`
=======
- **Type:** `() => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数是否至少成功返回了一次值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1267,7 +1299,11 @@ test('spy function returned a value', () => {

## toHaveReturnedTimes

<<<<<<< HEAD
- **类型**: `(amount: number) => Awaitable<void>`
=======
- **Type:** `(amount: number) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数是否在确切的次数内成功返回了值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1286,7 +1322,11 @@ test('spy function returns a value two times', () => {

## toHaveReturnedWith

<<<<<<< HEAD
- **类型**: `(returnValue: any) => Awaitable<void>`
=======
- **Type:** `(returnValue: any) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

我们可以调用这个断言来检查函数是否至少一次成功返回了带有特定参数的值。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1304,7 +1344,11 @@ test('spy function returns a product', () => {

## toHaveLastReturnedWith

<<<<<<< HEAD
- **类型**: `(returnValue: any) => Awaitable<void>`
=======
- **Type:** `(returnValue: any) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

我们可以使用这个断言来检查函数在最后一次被调用时是否成功返回了特定的值。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1323,7 +1367,11 @@ test('spy function returns bananas on a last call', () => {

## toHaveNthReturnedWith

<<<<<<< HEAD
- **类型**: `(time: number, returnValue: any) => Awaitable<void>`
=======
- **Type:** `(time: number, returnValue: any) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

我们可以调用这个断言来检查函数是否在特定的调用中成功返回了带有特定参数的值。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1344,7 +1392,11 @@ test('spy function returns bananas on second call', () => {

## toHaveResolved

<<<<<<< HEAD
- **类型**: `() => Awaitable<void>`
=======
- **Type:** `() => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

这个断言检查函数是否至少一次成功地解析了一个值( i.e. ,没有被拒绝)。需要将一个 spy 函数传递给 `expect`。

Expand All @@ -1370,7 +1422,11 @@ test('spy function resolved a value', async () => {

## toHaveResolvedTimes

<<<<<<< HEAD
- **类型**: `(amount: number) => Awaitable<void>`
=======
- **Type:** `(amount: number) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

此断言检查函数是否已成功解析值精确次数(即未 reject)。需要将 spy 函数传递给`expect`。

Expand All @@ -1391,7 +1447,11 @@ test('spy function resolved a value two times', async () => {

## toHaveResolvedWith

<<<<<<< HEAD
- **类型:** `(returnValue: any) => Awaitable<void>`
=======
- **Type:** `(returnValue: any) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

您可以调用此断言来检查函数是否至少成功解析过一次某个值。需要将 spy 函数传递给`expect`。

Expand All @@ -1411,7 +1471,11 @@ test('spy function resolved a product', async () => {

## toHaveLastResolvedWith

<<<<<<< HEAD
- **类型:** `(returnValue: any) => Awaitable<void>`
=======
- **Type:** `(returnValue: any) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

您可以调用此断言来检查函数在上次调用时是否已成功解析某个值。需要将 spy 函数传递给`expect`。

Expand All @@ -1432,7 +1496,11 @@ test('spy function resolves bananas on a last call', async () => {

## toHaveNthResolvedWith

<<<<<<< HEAD
- **类型:** `(time: number, returnValue: any) => Awaitable<void>`
=======
- **Type:** `(time: number, returnValue: any) => Awaitable<void>`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

你可以调用此断言来检查函数在特定调用中是否成功解析了某个值。需要将一个 spy 函数传递给 `expect`。

Expand Down
4 changes: 4 additions & 0 deletions api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,12 @@
})
```

::: tip

Check failure on line 618 in api/test.md

View workflow job for this annotation

GitHub Actions / autofix

Unexpected additional H1 heading found
<<<<<<< HEAD
Vitest 使用 Chai 的 `format` 方法处理 `$values`。如果值被截断,可在配置文件中调大 [chaiConfig.truncateThreshold](/config/chaiconfig#chaiconfig-truncatethreshold)。
=======
Vitest formats interpolated title values with its display formatter. If the value is too truncated, you can increase [taskTitleValueFormatTruncate](/config/tasktitlevalueformattruncate) in your config file.
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf
:::

## test.for
Expand Down
6 changes: 6 additions & 0 deletions config/allowonly.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ outline: deep

# allowOnly

<<<<<<< HEAD
- **类型:**: `boolean`
- **默认值:**: `!process.env.CI`
- **命令行终端:** `--allowOnly`, `--allowOnly=false`
=======
- **Type:** `boolean`
- **Default:** `!process.env.CI`
- **CLI:** `--allowOnly`, `--allowOnly=false`
>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf

默认情况下,Vitest 不允许在持续集成(CI)环境中运行带有 [`only`](/api/test#test-only) 标记的测试。相反,在本地开发环境中,Vitest 允许运行这些测试。

Expand Down
Loading
Loading