diff --git a/api/advanced/vitest.md b/api/advanced/vitest.md index 1da470b6..a6476a7c 100644 --- a/api/advanced/vitest.md +++ b/api/advanced/vitest.md @@ -244,7 +244,11 @@ function start(filters?: string[]): Promise function standalone(): Promise ``` +<<<<<<< HEAD - **别名:**: `init` +======= +- **Alias:** `init` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 初始化报告器和覆盖率提供者。此方法不运行任何测试。如果提供了 `--watch` 标志,Vitest 仍将运行更改的测试,即使未调用此方法。 @@ -696,3 +700,122 @@ export interface SourceModuleDiagnostic { ::: warning [浏览器模式](/guide/browser/) 暂不支持。 ::: + +## createReport 5.0.0 {#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 `/.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 /.vitest/my-json-reporter +const root = report.root +``` + + +### Report.clean + +```ts +function clean(): Promise +``` + +Clean up the report directory for this scope. + +```ts +const report = vitest.createReport('my-json-reporter') + +// Removes everything inside /.vitest/my-json-reporter/ +await report.clean() +``` + +### Report.writeFile + +```ts +function writeFile( + filename: string, + content: string | Uint8Array, + encoding?: BufferEncoding +): Promise +``` + +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 +``` + +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 +``` + +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 + + +```ts +function delete(filename: string): Promise +``` + +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') +``` + diff --git a/api/assert.md b/api/assert.md index 8f484404..91db44ba 100644 --- a/api/assert.md +++ b/api/assert.md @@ -35,8 +35,13 @@ test('assert.fail', () => { ## isOk +<<<<<<< HEAD - **类型:** `(value: T, message?: string) => asserts value` - **Alias** `ok` +======= +- **Type:** `(value: T, message?: string) => asserts value` +- **Alias:** `ok` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 断言给定的 `value` 是 true 。 @@ -51,8 +56,13 @@ test('assert.isOk', () => { ## isNotOk +<<<<<<< HEAD - **类型:** `(value: T, message?: string) => void` - **Alias** `notOk` +======= +- **Type:** `(value: T, message?: string) => void` +- **Alias:** `notOk` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 断言给定的 `value` 是 false 。 diff --git a/api/expect.md b/api/expect.md index 4a62783e..b9686b3d 100644 --- a/api/expect.md +++ b/api/expect.md @@ -1077,7 +1077,11 @@ test('spy function', () => { ## toHaveBeenCalledTimes +<<<<<<< HEAD - **类型**: `(amount: number) => Awaitable` +======= +- **Type:** `(amount: number) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数是否被调用了特定次数。需要将一个 spy 函数传递给 `expect`。 @@ -1102,7 +1106,11 @@ test('spy function called two times', () => { ## toHaveBeenCalledWith +<<<<<<< HEAD - **类型**: `(...args: any[]) => Awaitable` +======= +- **Type:** `(...args: any[]) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数是否至少一次被调用,并带有特定的参数。需要将一个 spy 函数传递给 `expect`。 @@ -1128,7 +1136,11 @@ test('spy function', () => { ## toHaveBeenCalledBefore +<<<<<<< HEAD - **类型**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable` +======= +- **Type:** `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查一个 `Mock` 是否在另一个 `Mock` 之前被调用。 @@ -1147,7 +1159,11 @@ test('calls mock1 before mock2', () => { ## toHaveBeenCalledAfter +<<<<<<< HEAD - **类型**: `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable` +======= +- **Type:** `(mock: MockInstance, failIfNoFirstInvocation?: boolean) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查一个 `Mock` 是否在另一个 `Mock` 之后被调用。 @@ -1166,7 +1182,11 @@ test('calls mock1 after mock2', () => { ## toHaveBeenCalledExactlyOnceWith +<<<<<<< HEAD - **类型**: `(...args: any[]) => Awaitable` +======= +- **Type:** `(...args: any[]) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数是否恰好被调用了一次,并且带有特定的参数。需要将一个 spy 函数传递给 `expect`。 @@ -1190,7 +1210,11 @@ test('spy function', () => { ## toHaveBeenLastCalledWith +<<<<<<< HEAD - **类型**: `(...args: any[]) => Awaitable` +======= +- **Type:** `(...args: any[]) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数在其最后一次调用时是否被传入了特定的参数。需要将一个 spy 函数传递给 `expect`。 @@ -1216,7 +1240,11 @@ test('spy function', () => { ## toHaveBeenNthCalledWith +<<<<<<< HEAD - **类型**: `(time: number, ...args: any[]) => Awaitable` +======= +- **Type:** `(time: number, ...args: any[]) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数是否在特定的次数被调用时带有特定的参数。计数从 1 开始。因此,要检查第二次调用,我们需要写成 `.toHaveBeenNthCalledWith(2, ...)`。 @@ -1243,7 +1271,11 @@ test('first call of spy function called with right params', () => { ## toHaveReturned +<<<<<<< HEAD - **类型**: `() => Awaitable` +======= +- **Type:** `() => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数是否至少成功返回了一次值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`。 @@ -1267,7 +1299,11 @@ test('spy function returned a value', () => { ## toHaveReturnedTimes +<<<<<<< HEAD - **类型**: `(amount: number) => Awaitable` +======= +- **Type:** `(amount: number) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数是否在确切的次数内成功返回了值( i.e. ,没有抛出错误)。需要将一个 spy 函数传递给 `expect`。 @@ -1286,7 +1322,11 @@ test('spy function returns a value two times', () => { ## toHaveReturnedWith +<<<<<<< HEAD - **类型**: `(returnValue: any) => Awaitable` +======= +- **Type:** `(returnValue: any) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 我们可以调用这个断言来检查函数是否至少一次成功返回了带有特定参数的值。需要将一个 spy 函数传递给 `expect`。 @@ -1304,7 +1344,11 @@ test('spy function returns a product', () => { ## toHaveLastReturnedWith +<<<<<<< HEAD - **类型**: `(returnValue: any) => Awaitable` +======= +- **Type:** `(returnValue: any) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 我们可以使用这个断言来检查函数在最后一次被调用时是否成功返回了特定的值。需要将一个 spy 函数传递给 `expect`。 @@ -1323,7 +1367,11 @@ test('spy function returns bananas on a last call', () => { ## toHaveNthReturnedWith +<<<<<<< HEAD - **类型**: `(time: number, returnValue: any) => Awaitable` +======= +- **Type:** `(time: number, returnValue: any) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 我们可以调用这个断言来检查函数是否在特定的调用中成功返回了带有特定参数的值。需要将一个 spy 函数传递给 `expect`。 @@ -1344,7 +1392,11 @@ test('spy function returns bananas on second call', () => { ## toHaveResolved +<<<<<<< HEAD - **类型**: `() => Awaitable` +======= +- **Type:** `() => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 这个断言检查函数是否至少一次成功地解析了一个值( i.e. ,没有被拒绝)。需要将一个 spy 函数传递给 `expect`。 @@ -1370,7 +1422,11 @@ test('spy function resolved a value', async () => { ## toHaveResolvedTimes +<<<<<<< HEAD - **类型**: `(amount: number) => Awaitable` +======= +- **Type:** `(amount: number) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 此断言检查函数是否已成功解析值精确次数(即未 reject)。需要将 spy 函数传递给`expect`。 @@ -1391,7 +1447,11 @@ test('spy function resolved a value two times', async () => { ## toHaveResolvedWith +<<<<<<< HEAD - **类型:** `(returnValue: any) => Awaitable` +======= +- **Type:** `(returnValue: any) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 您可以调用此断言来检查函数是否至少成功解析过一次某个值。需要将 spy 函数传递给`expect`。 @@ -1411,7 +1471,11 @@ test('spy function resolved a product', async () => { ## toHaveLastResolvedWith +<<<<<<< HEAD - **类型:** `(returnValue: any) => Awaitable` +======= +- **Type:** `(returnValue: any) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 您可以调用此断言来检查函数在上次调用时是否已成功解析某个值。需要将 spy 函数传递给`expect`。 @@ -1432,7 +1496,11 @@ test('spy function resolves bananas on a last call', async () => { ## toHaveNthResolvedWith +<<<<<<< HEAD - **类型:** `(time: number, returnValue: any) => Awaitable` +======= +- **Type:** `(time: number, returnValue: any) => Awaitable` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 你可以调用此断言来检查函数在特定调用中是否成功解析了某个值。需要将一个 spy 函数传递给 `expect`。 diff --git a/api/test.md b/api/test.md index 9fa7336f..883a7354 100644 --- a/api/test.md +++ b/api/test.md @@ -616,7 +616,11 @@ test.each` ``` ::: tip +<<<<<<< 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 diff --git a/config/allowonly.md b/config/allowonly.md index 7e932f0e..f48c979d 100644 --- a/config/allowonly.md +++ b/config/allowonly.md @@ -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 允许运行这些测试。 diff --git a/config/bail.md b/config/bail.md index 855d23d0..827eaf48 100644 --- a/config/bail.md +++ b/config/bail.md @@ -5,9 +5,15 @@ outline: deep # bail +<<<<<<< HEAD - **类型:** `number` - **默认值:** `0` - **命令行终端:** `--bail=` +======= +- **Type:** `number` +- **Default:** `0` +- **CLI:** `--bail=` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 当指定数量的测试用例失败时立即终止测试执行。 diff --git a/config/browser/provider.md b/config/browser/provider.md index 06f9c0f7..0c0e3f76 100644 --- a/config/browser/provider.md +++ b/config/browser/provider.md @@ -61,7 +61,11 @@ export default defineConfig({ }) ``` +<<<<<<< HEAD ## 自定义 Provider advanced {#custom-provider} +======= +## Custom Provider advanced {#custom-provider} +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf ::: danger 高级 API 自定义 provider API 处于高度实验阶段,可能会在小版本之间发生变化。如果你只需要在浏览器中运行测试,请使用 [`browser.instances`](/config/browser/instances) 选项替代。 diff --git a/config/cache.md b/config/cache.md index 8eddc27f..1e8a47ff 100644 --- a/config/cache.md +++ b/config/cache.md @@ -5,8 +5,13 @@ outline: deep # cache +<<<<<<< HEAD - **类型:** `false` - **命令行终端:** `--no-cache`, `--cache=false` +======= +- **Type:** `false` +- **CLI:** `--no-cache`, `--cache=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 使用此选项可禁用缓存功能。当前 Vitest 会缓存测试结果,以便优先运行耗时较长和失败的测试。 diff --git a/config/chaiconfig.md b/config/chaiconfig.md index f6edf81a..ecda8bb0 100644 --- a/config/chaiconfig.md +++ b/config/chaiconfig.md @@ -29,6 +29,10 @@ outline: deep - **类型:** `number` - **默认值:** `40` +<<<<<<< HEAD 设置断言错误中实际值与期望值的长度阈值。当超过该阈值时(例如处理大型数据结构),值将被截断显示为类似 `[ Array(3) ]` 或 `{ Object (prop1, prop2) }` 的形式。若需完全禁用截断功能,请将该值设为 `0`。 此配置项会影响 `test.each` 标题及断言错误信息内部值的截断显示。 +======= +Sets length threshold for actual and expected values in assertion error messages. If this threshold is exceeded, for example for large data structures, the value is replaced with something like `[ Array(3) ]` or `{ Object (prop1, prop2) }`. Set it to `0` if you want to disable truncating altogether. +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf diff --git a/config/coverage.md b/config/coverage.md index c3c23949..f4356436 100644 --- a/config/coverage.md +++ b/config/coverage.md @@ -65,7 +65,11 @@ npx vitest --coverage.enabled --coverage.provider=istanbul - **可用的测试提供者:** `'v8' | 'istanbul'` - **命令行终端:** `--coverage.clean`, `--coverage.clean=false` +<<<<<<< HEAD 运行测试前清除代码覆盖率结果。 +======= +Clean coverage results before running tests. +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf ## coverage.cleanOnRerun @@ -390,6 +394,46 @@ Vitest 会将所有文件(包括匹配 glob 模式的文件)计入全局覆 处理代码覆盖率结果时使用的并发限制。 +## coverage.instrumenter 4.1.5 {#coverage-instrumenter} + +- **Type:** `(options: InstrumenterOptions) => CoverageInstrumenter` +- **Available for providers:** `'istanbul'` + +Factory for a custom instrumenter to use in place of the default `istanbul-lib-instrument`. Vitest calls the factory once during initialization and reuses the returned instrumenter for every file. The rest of the Istanbul pipeline (collection, merging, reporting) is unchanged. + +The factory receives an `InstrumenterOptions` object with Vitest's runtime coverage settings, and must return an object implementing the `CoverageInstrumenter` interface. Both types are exported from `vitest/node`. + + +```ts +interface InstrumenterOptions { + coverageVariable: string + coverageGlobalScope: string + coverageGlobalScopeFunc: boolean + ignoreClassMethods: string[] +} + +interface CoverageInstrumenter { + instrumentSync: (code: string, filename: string, inputSourceMap?: any) => string + lastSourceMap: () => any + lastFileCoverage: () => any +} +``` + + +```ts +import { defineConfig } from 'vitest/config' +import { createInstrumenter } from '@vitest/some-custom-instrumenter' + +export default defineConfig({ + test: { + coverage: { + provider: 'istanbul', + instrumenter: options => createInstrumenter(options), + } + } +}) +``` + ## coverage.customProviderModule - **类型:** `string` diff --git a/config/css.md b/config/css.md index 05806a87..f12b26a8 100644 --- a/config/css.md +++ b/config/css.md @@ -5,7 +5,11 @@ outline: deep # css +<<<<<<< HEAD - **类型:** `boolean | { include?, exclude?, modules? }` +======= +- **Type:** `boolean | { include?, exclude?, modules? }` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 配置是否应处理 CSS。当被排除时,CSS 文件将被替换为空字符串以绕过后续处理。CSS 模块将返回代理对象以不影响运行时。 @@ -15,8 +19,13 @@ outline: deep ## css.include +<<<<<<< HEAD - **类型:** `RegExp | RegExp[]` - **默认值:** `[]` +======= +- **Type:** `RegExp | RegExp[]` +- **Default:** `[]` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 使用 RegExp 模式进行匹配,指定哪些 CSS 文件应返回实际内容并通过 Vite 处理。 @@ -26,13 +35,19 @@ outline: deep ## css.exclude +<<<<<<< HEAD - **类型:** `RegExp | RegExp[]` - **默认值:** `[]` +======= +- **Type:** `RegExp | RegExp[]` +- **Default:** `[]` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 使用 RegExp 模式指定哪些 CSS 文件应返回空内容。 ## css.modules +<<<<<<< HEAD - **类型:** `{ classNameStrategy? }` - **默认值:** `{}` @@ -40,6 +55,15 @@ outline: deep - **类型:** `'stable' | 'scoped' | 'non-scoped'` - **默认值:** `'stable'` +======= +- **Type:** `{ classNameStrategy? }` +- **Default:** `{}` + +### css.modules.classNameStrategy + +- **Type:** `'stable' | 'scoped' | 'non-scoped'` +- **Default:** `'stable'` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 如果您决定处理 CSS 文件,可以配置 CSS modules 中的类名是否应限定作用域。您可以选择以下选项之一: diff --git a/config/dangerouslyignoreunhandlederrors.md b/config/dangerouslyignoreunhandlederrors.md index 67cd7a5e..ace676fe 100644 --- a/config/dangerouslyignoreunhandlederrors.md +++ b/config/dangerouslyignoreunhandlederrors.md @@ -5,9 +5,15 @@ outline: deep # dangerouslyIgnoreUnhandledErrors +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf - `--dangerouslyIgnoreUnhandledErrors` - `--dangerouslyIgnoreUnhandledErrors=false` diff --git a/config/deps.md b/config/deps.md index d6dc33ae..62a4bb26 100644 --- a/config/deps.md +++ b/config/deps.md @@ -111,8 +111,13 @@ TypeError: default is not a function ## deps.moduleDirectories +<<<<<<< HEAD - **类型:** `string[]` - **默认值:** `['node_modules']` +======= +- **Type:** `string[]` +- **Default:** `['node_modules']` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 配置一个视为模块目录的目录列表。此配置选项会影响 [`vi.mock`](/api/vi#vi-mock) 的行为:当未提供工厂并且你正在模拟的路径与 `moduleDirectories` 值之一匹配时,Vitest 将尝试 通过在项目的 [root](/config/root) 中查找 `__mocks__` 文件夹来解析 mock。 diff --git a/config/diff.md b/config/diff.md index 8104285f..44646dd2 100644 --- a/config/diff.md +++ b/config/diff.md @@ -56,37 +56,61 @@ export default { ## diff.expand +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `true` - **命令行终端:** `--diff.expand=false` +======= +- **Type:** `boolean` +- **Default:** `true` +- **CLI:** `--diff.expand=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 展开所有公共行。 ## diff.truncateThreshold +<<<<<<< HEAD - **类型:** `number` - **默认值:** `0` - **命令行终端:** `--diff.truncateThreshold=` +======= +- **Type:** `number` +- **Default:** `0` +- **CLI:** `--diff.truncateThreshold=` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 差异结果的最大显示长度。超过此阈值的差异将被截断。默认值 0 表示不截断。 ## diff.truncateAnnotation +<<<<<<< HEAD - **类型:** `string` - **默认值:** `'... Diff result is truncated'` - **命令行终端:** `--diff.truncateAnnotation=` +======= +- **Type:** `string` +- **Default:** `'... Diff result is truncated'` +- **CLI:** `--diff.truncateAnnotation=` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 如果差异结果被截断,在末尾输出的注释。 ## diff.truncateAnnotationColor +<<<<<<< HEAD - **类型:** `DiffOptionsColor = (arg: string) => string` - **默认值:** `noColor = (string: string): string => string` +======= +- **Type:** `DiffOptionsColor = (arg: string) => string` +- **Default:** `noColor = (string: string): string => string` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 截断注释的颜色,默认无颜色输出。 ## diff.printBasicPrototype +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` @@ -98,3 +122,16 @@ export default { - **默认值:** `20` (或比较不同类型时为 `8`) 打印嵌套对象时递归的最大深度。 +======= +- **Type:** `boolean` +- **Default:** `false` + +Print basic prototype `Object` and `Array` in diff output. + +## diff.maxDepth + +- **Type:** `number` +- **Default:** `20` (or `8` when comparing different types) + +Limit the depth to recurse when printing nested objects. +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf diff --git a/config/forcereruntriggers.md b/config/forcereruntriggers.md index 7a104366..032304f0 100644 --- a/config/forcereruntriggers.md +++ b/config/forcereruntriggers.md @@ -5,8 +5,13 @@ outline: deep # forceRerunTriggers +<<<<<<< HEAD - **类型:** `string[]` - **默认值:** `['**/package.json/**', '**/vitest.config.*/**', '**/vite.config.*/**']` +======= +- **Type:** `string[]` +- **Default:** `['**/package.json/**', '**/vitest.config.*/**', '**/vite.config.*/**']` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 将触发整个测试套件重新运行的文件路径(glob 模式)。当与 `--changed` 参数配合使用时,如果在 git diff 中发现触发文件,就会运行整个测试套件。 diff --git a/config/logheapusage.md b/config/logheapusage.md index 5e6304a5..be32f9c8 100644 --- a/config/logheapusage.md +++ b/config/logheapusage.md @@ -5,8 +5,14 @@ outline: deep # logHeapUsage +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** `--logHeapUsage`, `--logHeapUsage=false` +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** `--logHeapUsage`, `--logHeapUsage=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 在每次测试后显示堆使用情况。适用于调试内存泄漏。 diff --git a/config/maxconcurrency.md b/config/maxconcurrency.md index 8db782db..b447da70 100644 --- a/config/maxconcurrency.md +++ b/config/maxconcurrency.md @@ -5,9 +5,15 @@ outline: deep # maxConcurrency +<<<<<<< HEAD - **类型:** `number` - **默认值:** `5` - **命令行终端:** `--max-concurrency=10`, `--maxConcurrency=10` +======= +- **Type:** `number` +- **Default:** `5` +- **CLI:** `--max-concurrency=10`, `--maxConcurrency=10` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 使用 `test.concurrent` 或 `describe.concurrent` 时,可以同时运行的测试和钩子的最大并发数量。 diff --git a/config/mode.md b/config/mode.md index 55a55d56..51b9bc0c 100644 --- a/config/mode.md +++ b/config/mode.md @@ -9,4 +9,8 @@ outline: deep - **命令行终端:** `--mode=staging` - **默认值:** `'test'` +<<<<<<< HEAD 覆盖 Vite 模式。 +======= +Overrides Vite mode. +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf diff --git a/config/onstacktrace.md b/config/onstacktrace.md index 08f342d2..555015ab 100644 --- a/config/onstacktrace.md +++ b/config/onstacktrace.md @@ -5,7 +5,11 @@ outline: deep # onStackTrace +<<<<<<< HEAD - **类型**: `(error: Error, frame: ParsedStack) => boolean | void` +======= +- **Type:** `(error: Error, frame: ParsedStack) => boolean | void` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 在处理错误时,对每个堆栈跟踪的每一帧应用过滤函数。此功能不适用于 [`printConsoleTrace`](/config/printconsoletrace#printconsoletrace) 打印的堆栈跟踪。第一个参数 `error` 是一个 `TestError` 类型。 diff --git a/config/passwithnotests.md b/config/passwithnotests.md index 2eac469a..f55c382f 100644 --- a/config/passwithnotests.md +++ b/config/passwithnotests.md @@ -5,8 +5,14 @@ outline: deep # passWithNoTests +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** `--passWithNoTests`, `--passWithNoTests=false` +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** `--passWithNoTests`, `--passWithNoTests=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 如果未发现任何测试,Vitest 不会报错。 diff --git a/config/pool.md b/config/pool.md index a46a0473..ae6ac981 100644 --- a/config/pool.md +++ b/config/pool.md @@ -13,7 +13,11 @@ outline: deep ## threads +<<<<<<< HEAD 启用多线程。使用 threads 线程池时,你无法使用与进程相关的 API,如 `process.chdir()`。某些用原生语言编写的库(如`Prisma`、`bcrypt` 和 `canvas`)在多线程中运行时存在问题,容易导致段错误。在这些情况下,建议改用 `forks` 线程池。 +======= +Enable multi-threading. When using threads you are unable to use process related APIs such as `process.chdir()`. Some libraries written in native languages, such as `Prisma`, `bcrypt` and `canvas`, have problems when running in multiple threads and run into segfaults. In these cases it is advised to use `forks` pool instead. +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf ## forks diff --git a/config/reporters.md b/config/reporters.md index f9f8f018..a9d74af6 100644 --- a/config/reporters.md +++ b/config/reporters.md @@ -42,7 +42,7 @@ type ConfigReporter = string | Reporter | [string, object?] - [`tap-flat`](/guide/reporters#tap-flat-reporter) - [`hanging-process`](/guide/reporters#hanging-process-reporter) - [`github-actions`](/guide/reporters#github-actions-reporter) -- [`agent`](/guide/reporters#agent-reporter) +- [`minimal`](/guide/reporters#minimal-reporter) (aliased as `agent`) - [`blob`](/guide/reporters#blob-reporter) ## 示例 {#example} diff --git a/config/resolvesnapshotpath.md b/config/resolvesnapshotpath.md index 85f6aaad..76ccfb5a 100644 --- a/config/resolvesnapshotpath.md +++ b/config/resolvesnapshotpath.md @@ -5,8 +5,13 @@ outline: deep # resolveSnapshotPath +<<<<<<< HEAD - **类型**: `(testPath: string, snapExtension: string, context: { config: SerializedConfig }) => string` - **默认值**: 将快照文件存储在 `__snapshots__` 目录中 +======= +- **Type:** `(testPath: string, snapExtension: string, context: { config: SerializedConfig }) => string` +- **Default:** stores snapshot files in `__snapshots__` directory +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 覆盖默认的快照路径。例如,将快照文件与测试文件存放在同一目录下: diff --git a/config/runner.md b/config/runner.md index aacbd269..074fb067 100644 --- a/config/runner.md +++ b/config/runner.md @@ -5,7 +5,12 @@ outline: deep # runner +<<<<<<< HEAD - **类型**: `VitestRunnerConstructor` - **默认值**: 运行测试时为 `node`,运行基准测试时为 `benchmark` +======= +- **Type:** `VitestRunnerConstructor` +- **Default:** `node`, when running tests, or `benchmark`, when running benchmarks +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 自定义测试运行器的路径。这是一项高级功能,应与自定义库运行器一起使用。你可以在 [文档](/api/advanced/runner) 中阅读更多相关信息。 diff --git a/config/sequence.md b/config/sequence.md index 58f0fa24..90eb51b6 100644 --- a/config/sequence.md +++ b/config/sequence.md @@ -5,7 +5,11 @@ outline: deep # sequence +<<<<<<< HEAD - **类型:** `{ sequencer?, shuffle?, seed?, hooks?, setupFiles?, groupOrder }` +======= +- **Type:** `{ sequencer?, shuffle?, seed?, hooks?, setupFiles?, groupOrder }` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 控制测试排序方式的选项。 @@ -17,8 +21,13 @@ npx vitest --sequence.shuffle --sequence.seed=1000 ## sequence.sequencer +<<<<<<< HEAD - **类型:** `TestSequencerConstructor` - **默认值:** `BaseSequencer` +======= +- **Type:** `TestSequencerConstructor` +- **Default:** `BaseSequencer` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 自定义类,用于定义分片和排序方法。如果只需重定义 `sort` 或 `shard` 其中一种方法,可从 `vitest/node` 扩展 `BaseSequencer` 基类,但需确保两个方法同时存在。 @@ -91,9 +100,15 @@ export default defineConfig({ ## sequence.shuffle +<<<<<<< HEAD - **类型:** `boolean | { files?, tests? }` - **默认值:** `false` - **命令行终端:** `--sequence.shuffle`, `--sequence.shuffle=false` +======= +- **Type:** `boolean | { files?, tests? }` +- **Default:** `false` +- **CLI:** `--sequence.shuffle`, `--sequence.shuffle=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 如果希望测试文件和测试用例随机执行,可通过此选项或 CLI 参数 [`--sequence.shuffle`](/guide/cli) 启用。 @@ -101,25 +116,43 @@ Vitest 通常使用缓存对测试进行排序,使耗时较长的测试优先 ### sequence.shuffle.files {#sequence-shuffle-files} +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** `--sequence.shuffle.files`, `--sequence.shuffle.files=false` +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** `--sequence.shuffle.files`, `--sequence.shuffle.files=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 是否启用文件随机排序,请注意启用此选项后,耗时较长的测试将无法优先启动执行。 ### sequence.shuffle.tests {#sequence-shuffle-tests} +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** `--sequence.shuffle.tests`, `--sequence.shuffle.tests=false` +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** `--sequence.shuffle.tests`, `--sequence.shuffle.tests=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf Whether to randomize tests. ## sequence.concurrent {#sequence-concurrent} +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** `--sequence.concurrent`, `--sequence.concurrent=false` +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** `--sequence.concurrent`, `--sequence.concurrent=false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 如果希望测试并行运行,可通过此选项或 CLI 参数 [`--sequence.concurrent`](/guide/cli) 启用。 @@ -129,17 +162,29 @@ Whether to randomize tests. ## sequence.seed +<<<<<<< HEAD - **类型:** `number` - **默认值:** `Date.now()` - **命令行终端:** `--sequence.seed=1000` +======= +- **Type:** `number` +- **Default:** `Date.now()` +- **CLI:** `--sequence.seed=1000` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 设置随机种子,当测试以随机顺序运行时生效。 ## sequence.hooks +<<<<<<< HEAD - **类型:** `'stack' | 'list' | 'parallel'` - **默认值:** `'stack'` - **命令行终端:** `--sequence.hooks=` +======= +- **Type:** `'stack' | 'list' | 'parallel'` +- **Default:** `'stack'` +- **CLI:** `--sequence.hooks=` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 调整钩子函数的执行顺序: @@ -153,9 +198,15 @@ Whether to randomize tests. ## sequence.setupFiles {#sequence-setupfiles} +<<<<<<< HEAD - **类型:** `'list' | 'parallel'` - **默认值:** `'parallel'` - **命令行终端:** `--sequence.setupFiles=` +======= +- **Type:** `'list' | 'parallel'` +- **Default:** `'parallel'` +- **CLI:** `--sequence.setupFiles=` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 调整配置文件的执行顺序: diff --git a/config/slowtestthreshold.md b/config/slowtestthreshold.md index e72488ba..271cfa34 100644 --- a/config/slowtestthreshold.md +++ b/config/slowtestthreshold.md @@ -5,8 +5,14 @@ outline: deep # slowTestThreshold +<<<<<<< HEAD - **类型:** `number` - **默认值:** `300` - **命令行终端:** `--slow-test-threshold=`, `--slowTestThreshold=` +======= +- **Type:** `number` +- **Default:** `300` +- **CLI:** `--slow-test-threshold=`, `--slowTestThreshold=` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 测试或测试套件执行时间超过该毫秒数即被视为缓慢,并在测试结果中予以标注。 diff --git a/config/tasktitlevalueformattruncate.md b/config/tasktitlevalueformattruncate.md new file mode 100644 index 00000000..5bb7eb88 --- /dev/null +++ b/config/tasktitlevalueformattruncate.md @@ -0,0 +1,15 @@ +--- +title: taskTitleValueFormatTruncate | Config +outline: deep +--- + +# taskTitleValueFormatTruncate {#tasktitlevalueformattruncate} + +- **Type** `number` +- **Default:** `40` + +Sets the length limit for formatted values interpolated into generated task titles. + +This affects values inserted by APIs like `test.each` and `test.for`, including both `$value` and `%` placeholder formatting. + +Set it to `0` to disable truncation. diff --git a/config/testnamepattern.md b/config/testnamepattern.md index 3768d40b..7a46ff47 100644 --- a/config/testnamepattern.md +++ b/config/testnamepattern.md @@ -5,8 +5,13 @@ outline: deep # testNamePattern {#testnamepattern} +<<<<<<< HEAD - **类型:** `string | RegExp` - **命令行终端:** `-t `, `--testNamePattern=`, `--test-name-pattern=` +======= +- **Type:** `string | RegExp` +- **CLI:** `-t `, `--testNamePattern=`, `--test-name-pattern=` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 运行名称完全匹配该模式的测试。如果在此属性中添加 `OnlyRunThis`,则测试中不包含 `OnlyRunThis` 关键字的用例将会被跳过。 diff --git a/config/typecheck.md b/config/typecheck.md index 4666c13e..bd692085 100644 --- a/config/typecheck.md +++ b/config/typecheck.md @@ -9,24 +9,41 @@ outline: deep ## typecheck.enabled {#typecheck-enabled} +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** `--typecheck`, `--typecheck.enabled` +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** `--typecheck`, `--typecheck.enabled` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 在常规测试的同时启用类型检查。 ## typecheck.only {#typecheck-only} +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` - **命令行终端:** `--typecheck.only` +======= +- **Type:** `boolean` +- **Default:** `false` +- **CLI:** `--typecheck.only` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 仅运行类型检查测试,当类型检查启用时生效。在 CLI 中使用此选项会自动启用类型检查。 ## typecheck.checker +<<<<<<< HEAD - **类型:** `'tsc' | 'vue-tsc' | string` - **默认值:** `tsc` +======= +- **Type:** `'tsc' | 'vue-tsc' | string` +- **Default:** `tsc` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 用于类型检查的工具。Vitest 将根据所选类型启动特定参数的进程以便解析,检查器需实现与 `tsc` 相同的输出格式。 @@ -39,6 +56,7 @@ outline: deep ## typecheck.include +<<<<<<< HEAD - **类型:** `string[]` - **默认值:** `['**/*.{test,spec}-d.?(c|m)[jt]s?(x)']` @@ -55,13 +73,36 @@ outline: deep - **类型:** `boolean` - **默认值:** `false` +======= +- **Type:** `string[]` +- **Default:** `['**/*.{test,spec}-d.?(c|m)[jt]s?(x)']` + +Glob pattern for files that should be treated as test files. + +## typecheck.exclude + +- **Type:** `string[]` +- **Default:** `['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**']` + +Glob pattern for files that should not be treated as test files. + +## typecheck.allowJs + +- **Type:** `boolean` +- **Default:** `false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 检查有 `@ts-check` 注释的 JS 文件。 如果你在 tsconfig 中启用它,则不会覆盖它。 ## typecheck.ignoreSourceErrors +<<<<<<< HEAD - **类型:** `boolean` - **默认值:** `false` +======= +- **Type:** `boolean` +- **Default:** `false` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 如果 Vitest 在测试文件之外发现错误,则不失败。这不会显示非测试文件的错误。 @@ -69,14 +110,24 @@ outline: deep ## typecheck.tsconfig +<<<<<<< HEAD - **类型:** `string` - **默认值:** _尝试查找最近的 tsconfig.json_ +======= +- **Type:** `string` +- **Default:** _tries to find closest tsconfig.json_ +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 自定义 tsconfig 的路径,相对于项目根目录。 ## typecheck.spawnTimeout +<<<<<<< HEAD - **类型:** `number` - **默认值:** `10_000` +======= +- **Type:** `number` +- **Default:** `10_000` +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 启动类型检查器所需的最短时间(毫秒)。 diff --git a/guide/advanced/reporters.md b/guide/advanced/reporters.md index 8416d4e2..64bcb93b 100644 --- a/guide/advanced/reporters.md +++ b/guide/advanced/reporters.md @@ -72,7 +72,38 @@ class MyReporter implements Reporter { } ``` +<<<<<<< HEAD ## 导出报告器 {#exported-reporters} +======= +## Storing artifacts on file system + +::: tip +Vitest provides [`vitest.createReport`](/api/advanced/vitest.html#createreport) that exposes collection of utilities for writing artifacts on file system conveniently. +::: + +If your custom reporter needs to store any artifacts on file system it should place them inside `.vitest` directory. This directory is a convention that Vitest reporters and third party integrations can use to co-locate their results in a single directory. This way users of your custom reporter do not need to add multiple exclusion in their `.gitignore`. Only the `.vitest` is needed. + +Reporters and other integrations should respect following rules around `.vitest` directory: + +- `.vitest` directory is placed in [the `root` of the project](/config/root) +- Reporter can create `.vitest` directory if it does not already exist +- Reporter should never remove `.vitest` directory +- Reporter should create their own directory inside `.vitest`, for example `.vitest/yaml-reporter/` +- Reporter can remove their own specific directory inside `.vitest`, for example `.vitest/yaml-reporter/` + +```ansi +.vitest +│ +├── yaml-reporter +│ ├── results.yaml +│ └── summary.yaml +│ +└── junit-reporter + └── report.xml +``` + +## Exported Reporters +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf `vitest` 附带了一些 [内置报告器](/guide/reporters),我们可以开箱即用。 diff --git a/guide/cli-generated.md b/guide/cli-generated.md index 727ab561..a6af9dfd 100644 --- a/guide/cli-generated.md +++ b/guide/cli-generated.md @@ -102,7 +102,11 @@ - **命令行终端:** `--reporter ` - **配置:** [reporters](/config/reporters) +<<<<<<< HEAD 指定报告器(default、agent、blob、verbose、dot、json、tap、tap-flat、junit、tree、hanging-process、github-actions) +======= +Specify reporters (default, agent, minimal, blob, verbose, dot, json, tap, tap-flat, junit, tree, hanging-process, github-actions) +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf ### outputFile diff --git a/guide/cli.md b/guide/cli.md index 0a5de212..d90ef25d 100644 --- a/guide/cli.md +++ b/guide/cli.md @@ -194,8 +194,13 @@ vitest --api=false ### changed +<<<<<<< HEAD - **类型**: `boolean | string` - **默认值**: false +======= +- **Type:** `boolean | string` +- **Default:** false +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 设置为 true 时,仅对已更改的文件运行测试。默认情况下,将考虑所有未提交的更改(包括已暂存和未暂存的文件)。 @@ -209,8 +214,13 @@ vitest --api=false ### shard +<<<<<<< HEAD - **类型**: `string` - **默认值**: disabled +======= +- **Type:** `string` +- **Default:** disabled +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 测试套件分片,格式为 `/`,其中 diff --git a/guide/index.md b/guide/index.md index 880617cb..fb11c21b 100644 --- a/guide/index.md +++ b/guide/index.md @@ -49,7 +49,18 @@ Vitest 需要 Vite >=v6.0.0 和 Node >=v20.0.0 `npx` 是一个命令行工具,用于执行指定的命令。默认情况下,`npx` 会首先检查本地项目的二进制文件中是否存在该命令。如果在那里没有找到,`npx` 会在系统的 `$PATH` 中查找并执行该命令(如果找到的话)。如果两个位置都没有找到该命令,`npx` 会在执行之前将其安装在临时位置。 +<<<<<<< HEAD ## 编写测试 {#writing-tests} +======= +Vitest and third party integrations can use `.vitest` directory to store generated artifacts. It's recommended to add this in your `.gitignore`. + +``` sh [.gitignore] +# Vitest reports and artifacts +.vitest/ +``` + +## Writing Tests +>>>>>>> 1f86109e54689db534e518c85a6a73daa82d5bcf 例如,我们将编写一个简单的测试来验证将两个数字相加的函数的输出。 diff --git a/guide/reporters.md b/guide/reporters.md index b6d7ed23..99a38bf7 100644 --- a/guide/reporters.md +++ b/guide/reporters.md @@ -100,7 +100,7 @@ export default defineConfig({ 默认情况下(即如果没有指定报告器),Vitest 会在底部显示运行测试的摘要及其状态。一旦测试套件通过,其状态将被报告在摘要的顶部。 ::: tip -When Vitest detects it is running inside an AI coding agent, the [`agent`](#agent-reporter) reporter is used instead to reduce output and minimize token usage. You can override this by explicitly configuring the [`reporters`](/config/reporters) option. +When Vitest detects it is running inside an AI coding agent, the [`minimal`](#minimal-reporter) reporter is used instead to reduce output and minimize token usage. You can override this by explicitly configuring the [`reporters`](/config/reporters) option. ::: 我们可以通过配置报告器来禁用摘要: @@ -670,21 +670,24 @@ export default defineConfig({ }) ``` -### Agent Reporter +### Minimal Reporter -Outputs a minimal report optimized for AI coding assistants and LLM-based workflows. Only failed tests and their error messages are displayed. Console logs from passing tests and the summary section are suppressed to reduce token usage. +- **Alias:** `agent` -This reporter is automatically enabled when no `reporters` option is configured and Vitest detects it is running inside an AI coding agent. If you configure custom reporters, you can explicitly add `agent`: +Outputs a minimal report containing only failed tests and their error messages. Console logs from passing tests and the summary section are also suppressed. + +::: tip Agent Reporter +This reporter is well optimized for AI coding assistants and LLM-based workflows to reduce token usage. It is automatically enabled when no `reporters` option is configured and Vitest detects it is running inside an AI coding agent. If you configure custom reporters, you can explicitly add `minimal` or `agent`: :::code-group ```bash [CLI] -npx vitest --reporter=agent +npx vitest --reporter=minimal ``` ```ts [vitest.config.ts] export default defineConfig({ test: { - reporters: ['agent'] + reporters: ['minimal'] }, }) ``` diff --git a/guide/snapshot.md b/guide/snapshot.md index 14477b13..4a92a548 100644 --- a/guide/snapshot.md +++ b/guide/snapshot.md @@ -239,8 +239,8 @@ import { expect, test, Snapshots } from 'vitest' const { toMatchFileSnapshot, toMatchInlineSnapshot, toMatchSnapshot } = Snapshots expect.extend({ - toMatchTrimmedSnapshot(received: string, length: number) { - return toMatchSnapshot.call(this, received.slice(0, length)) + toMatchTrimmedSnapshot(received: string) { + return toMatchSnapshot.call(this, received.slice(0, 10)) }, toMatchTrimmedInlineSnapshot(received: string, inlineSnapshot?: string) { return toMatchInlineSnapshot.call(this, received.slice(0, 10), inlineSnapshot) @@ -251,15 +251,19 @@ expect.extend({ }) test('file snapshot', () => { + // create __snapshots__/demo.test.ts with + // > exports[`file snapshot 1`] = `"extra long"` expect('extra long string oh my gerd').toMatchTrimmedSnapshot(10) }) test('inline snapshot', () => { - expect('extra long string oh my gerd').toMatchTrimmedInlineSnapshot() + expect('super long string oh my gerd').toMatchTrimmedInlineSnapshot(`"super long"`) }) test('raw file snapshot', async () => { - await expect('extra long string oh my gerd').toMatchTrimmedFileSnapshot('./raw-file.txt') + // create raw-file.txt with: + // > crazy long + await expect('crazy long string oh my gerd').toMatchTrimmedFileSnapshot('./raw-file.txt') }) ```