From 4fa791b02754acb292019db553fe3f075b1c2ce3 Mon Sep 17 00:00:00 2001 From: koutaro-masaki <74706880+koutaro-masaki@users.noreply.github.com> Date: Sun, 7 Jun 2026 17:53:28 +0900 Subject: [PATCH 1/2] docs: document the assertion function limitation in in-source testing (#10527) --- api/assert.md | 28 ++++++++++++++++++++++++++++ guide/in-source.md | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/api/assert.md b/api/assert.md index a10d06634..ee3a14c10 100644 --- a/api/assert.md +++ b/api/assert.md @@ -2,6 +2,34 @@ Vitest reexports the `assert` method from [`chai`](https://www.chaijs.com/api/assert/) for verifying invariants. +::: 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 - **Type:** `(expression: any, message?: string) => asserts expression` diff --git a/guide/in-source.md b/guide/in-source.md index f22b1843e..05e6f8864 100644 --- a/guide/in-source.md +++ b/guide/in-source.md @@ -152,6 +152,10 @@ To get TypeScript support for `import.meta.vitest`, add `vitest/importMeta` to y Reference to [`examples/in-source-test`](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test) for the full example. +::: 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 This feature could be useful for: From 2faf0d3031b3c8fea4349f2fb70e9199a71e8a72 Mon Sep 17 00:00:00 2001 From: Todor Andonov <45210624+todor-a@users.noreply.github.com> Date: Sun, 7 Jun 2026 17:53:14 +0300 Subject: [PATCH 2/2] feat(cli): add `--repeats` CLI option (#10504) --- .vitepress/config.ts | 4 ++++ config/repeats.md | 14 ++++++++++++++ guide/cli-generated.md | 7 +++++++ 3 files changed, 25 insertions(+) create mode 100644 config/repeats.md diff --git a/.vitepress/config.ts b/.vitepress/config.ts index 6a57443d8..c42839e96 100644 --- a/.vitepress/config.ts +++ b/.vitepress/config.ts @@ -507,6 +507,10 @@ export default ({ mode }: { mode: string }) => { text: 'retry', link: '/config/retry', }, + { + text: 'repeats', + link: '/config/repeats', + }, { text: 'onConsoleLog', link: '/config/onconsolelog', diff --git a/config/repeats.md b/config/repeats.md new file mode 100644 index 000000000..733644c5f --- /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 7538e4f2e..9fe528713 100644 --- a/guide/cli-generated.md +++ b/guide/cli-generated.md @@ -637,6 +637,13 @@ Delay in milliseconds between retry attempts (default: `0`) Regex pattern to match error messages that should trigger a retry. Only errors matching this pattern will cause a retry (default: retry on all errors) +### repeats + +- **CLI:** `--repeats ` +- **Config:** [repeats](/config/repeats) + +Repeat every test a specific number of times regardless of the result (default: `0`) + ### diff.aAnnotation - **CLI:** `--diff.aAnnotation `