diff --git a/.vitepress/config.ts b/.vitepress/config.ts index c00fcac6..c88bb295 100644 --- a/.vitepress/config.ts +++ b/.vitepress/config.ts @@ -515,6 +515,10 @@ export default ({ mode }: { mode: string }) => { text: 'retry', link: '/config/retry', }, + { + text: 'repeats', + link: '/config/repeats', + }, { text: 'onConsoleLog', link: '/config/onconsolelog', diff --git a/api/assert.md b/api/assert.md index f7ed29d7..b8380d4a 100644 --- a/api/assert.md +++ b/api/assert.md @@ -2,6 +2,34 @@ Vitest 从 [`chai`](https://www.chaijs.com/api/assert/) 重新导出了 `assert` 方法,用于验证不变量。 +::: warning In-Source Testing {#in-source-testing} +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: + +::: code-group +```ts [Annotated variable] +if (import.meta.vitest) { + const { test, assert } = import.meta.vitest // [!code --] + const { test } = import.meta.vitest // [!code ++] + const assert: Chai.Assert = import.meta.vitest.assert // [!code ++] + + test('assert', () => { + assert('foo' !== 'bar', 'foo should not be equal to bar') + }) +} +``` +```ts [Direct call] +if (import.meta.vitest) { + const { test, assert } = import.meta.vitest // [!code --] + const { test } = import.meta.vitest // [!code ++] + + test('assert', () => { + assert('foo' !== 'bar', 'foo should not be equal to bar') // [!code --] + import.meta.vitest!.assert('foo' !== 'bar', 'foo should not be equal to bar') // [!code ++] + }) +} +``` +::: + ## assert - **类型:** `(expression: any, message?: string) => asserts expression` diff --git a/config/repeats.md b/config/repeats.md new file mode 100644 index 00000000..733644c5 --- /dev/null +++ b/config/repeats.md @@ -0,0 +1,14 @@ +--- +title: repeats | Config +outline: deep +--- + +# repeats + +- **Type:** `number` +- **Default:** `0` +- **CLI:** `--repeats=` + +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. + +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. diff --git a/guide/cli-generated.md b/guide/cli-generated.md index c99b18cd..7f9b92fb 100644 --- a/guide/cli-generated.md +++ b/guide/cli-generated.md @@ -637,6 +637,13 @@ UI 模式和 HTML 报告器中提供的 HTML 覆盖率输出目录。 触发重试操作的错误信息匹配正则表达式。仅当错误信息符合该模式时才会执行重试(默认值:所有错误都会触发重试) +### repeats + +- **CLI:** `--repeats ` +- **Config:** [repeats](/config/repeats) + +Repeat every test a specific number of times regardless of the result (default: `0`) + ### diff.aAnnotation - **命令行终端:** `--diff.aAnnotation ` diff --git a/guide/in-source.md b/guide/in-source.md index ec956321..7ab32553 100644 --- a/guide/in-source.md +++ b/guide/in-source.md @@ -155,7 +155,15 @@ module.exports = { 完整的示例请参考 [`examples/in-source-test`](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test)。 +<<<<<<< HEAD ## 说明 {#notes} +======= +::: warning +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. +::: + +## Notes +>>>>>>> 2faf0d3031b3c8fea4349f2fb70e9199a71e8a72 此功能可用于: