Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
87 changes: 87 additions & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,62 @@ export default ({ mode }: { mode: string }) => {
],
},
{
<<<<<<< HEAD
text: '浏览器模式',
=======
Comment thread
NoiseFan marked this conversation as resolved.
Outdated
text: 'Learn',
collapsed: false,
items: [
{
text: 'Writing Tests',
link: '/guide/learn/writing-tests',
docFooterText: 'Writing Tests | Learn',
},
{
text: 'Using Matchers',
link: '/guide/learn/matchers',
docFooterText: 'Using Matchers | Learn',
},
{
text: 'Testing Async Code',
link: '/guide/learn/async',
docFooterText: 'Testing Async Code | Learn',
},
{
text: 'Setup and Teardown',
link: '/guide/learn/setup-teardown',
docFooterText: 'Setup and Teardown | Learn',
},
{
text: 'Mock Functions',
link: '/guide/learn/mock-functions',
docFooterText: 'Mock Functions | Learn',
},
{
text: 'Snapshot Testing',
link: '/guide/learn/snapshots',
docFooterText: 'Snapshot Testing | Learn',
},
{
text: 'Testing in Practice',
link: '/guide/learn/testing-in-practice',
docFooterText: 'Testing in Practice | Learn',
},
{
text: 'Debugging Tests',
link: '/guide/learn/debugging-tests',
docFooterText: 'Debugging Tests | Learn',
},
{
text: 'Writing Tests with AI',
link: '/guide/learn/writing-tests-with-ai',
docFooterText: 'Writing Tests with AI | Learn',
},
],
},
{
text: 'Browser Mode',
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16
collapsed: false,
items: [
{
Expand Down Expand Up @@ -796,6 +851,7 @@ export default ({ mode }: { mode: string }) => {
collapsed: true,
items: [
{
<<<<<<< HEAD
text: '模拟日期',
link: '/guide/mocking/dates',
},
Expand Down Expand Up @@ -825,6 +881,37 @@ export default ({ mode }: { mode: string }) => {
},
{
text: '模拟类',
=======
text: 'Dates',
link: '/guide/mocking/dates',
},
{
text: 'Functions',
link: '/guide/mocking/functions',
},
{
text: 'Globals',
link: '/guide/mocking/globals',
},
{
text: 'Modules',
link: '/guide/mocking/modules',
},
{
text: 'File System',
link: '/guide/mocking/file-system',
},
{
text: 'Requests',
link: '/guide/mocking/requests',
},
{
text: 'Timers',
link: '/guide/mocking/timers',
},
{
text: 'Classes',
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16
link: '/guide/mocking/classes',
},
],
Expand Down
19 changes: 19 additions & 0 deletions api/browser/interactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ await originalUserEvent.keyboard('{/Shift}') // 没有放开 shift 键,因为
这种行为更有用,因为我们并没有模拟键盘,而是实际按下了 Shift 键,所以保留原来的行为会在字段中键入时造成意想不到的问题。
:::

::: warning
With `playwright` and `webdriverio` providers, interactions are performed by the underlying browser driver. That means some interaction state, like pressed keys or pointer position and the resulting hover state, can persist between tests in the same file.

Vitest resets unreleased keyboard state automatically before starting each test case, but pointer position and the resulting hover state are not reset automatically since resetting pointer position can be expensive.

This applies both to `userEvent.*` calls and locator shortcuts like `locator.click()` or `locator.hover()`, because they use the same underlying interaction state.

If your tests depend on a neutral hover state, reset it explicitly, for example in `beforeEach`:

```ts
import { beforeEach } from 'vitest'
import { userEvent } from 'vitest/browser'

beforeEach(async () => {
await userEvent.unhover(document.body)
})
```
:::

## userEvent.click

```ts
Expand Down
4 changes: 4 additions & 0 deletions config/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ npx vitest --coverage.enabled --coverage.provider=istanbul

你可以在 Vitest UI 模式中查看代码覆盖率报告。更多详情请参阅 [Vitest UI Coverage](/guide/coverage#vitest-ui)。

::: tip AI coding agents
When Vitest detects it is running inside an AI coding agent, it automatically adds the `text-summary` reporter and sets `skipFull: true` on the `text` reporter to reduce output and minimize token usage.
:::

## coverage.reportOnFailure {#coverage-reportonfailure}

- **类型:** `boolean`
Expand Down
37 changes: 37 additions & 0 deletions config/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,41 @@

如果禁用模块运行器,Vitest 会使用原生 [Node.js 模块加载器](https://nodejs.org/api/module.html#customization-hooks) 来转换文件,以支持 `import.meta.vitest`、`vi.mock` 和 `vi.hoisted` 功能。

<<<<<<< HEAD

Check failure on line 480 in config/experimental.md

View workflow job for this annotation

GitHub Actions / autofix

Unexpected additional H1 heading found
如果你不使用这些特性,可禁用此功能以提升性能。
=======
If you don't use these features, you can disable this to improve performance.

## experimental.preParse <Version type="experimental">4.1.3</Version> {#experimental-preparse}

- **Type:** `boolean`
- **Default:** `false`

Parses test specifications before running them. This applies the [`.only`](/api/test#test-only) modifier, the [`-t`](/config/testnamepattern) test name pattern, [`--tags-filter`](/guide/test-tags#syntax), [test lines](/api/advanced/test-specification#testlines), and [test IDs](/api/advanced/test-specification#testids) across all files without executing them. For example, if only a single test is marked with `.only`, Vitest will skip all other tests in all files.

::: tip
This option is recommended when using [`.only`](/api/test#test-only), the [`-t`](/config/testnamepattern) flag, or [`--tags-filter`](/guide/test-tags#syntax).

Enabling it unconditionally may slow down your test runs due to the additional parsing step.
:::

::: warning
Pre-parsing uses static analysis (AST parsing) instead of executing your test files. This means that test names, tags, and modifiers (`.only`, `.skip`, `.todo`) must be statically analyzable. Dynamic test names (e.g., names stored in variables or returned from function calls) and non-literal tags will not be resolved correctly.

```ts
// ✅ works — static string literal
test('adds numbers', () => {})

// ✅ works — static tags
test('my test', { tags: ['unit'] }, () => {})

// ❌ won't match correctly — dynamic name
const name = getName()
test(name, () => {})

// ❌ won't match correctly — dynamic tags
const tags = getTags()
test('my test', { tags }, () => {})
```
:::
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16
10 changes: 10 additions & 0 deletions config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@

由于 Vitest 使用 Vite 的配置,我们也可以使用 [Vite](https://vitejs.dev/config/) 中的任何配置选项。例如,使用 `define` 来定义全局变量,或者使用 `resolve.alias` 来定义别名——这些选项应该在顶级定义,而不是在 `test` 属性内部。

<<<<<<< HEAD

Check failure on line 82 in config/index.md

View workflow job for this annotation

GitHub Actions / autofix

Unexpected additional H1 heading found
在 [项目](/guide/projects) 配置中不支持的配置选项旁边会显示 <CRoot /> 图标。这意味着它们只能在 Vitest 根配置文件中进行设置。
=======
## Automatic Dependency Installation

Vitest will prompt you to install certain dependencies if they are not already installed. You can disable this behavior by setting the `VITEST_SKIP_INSTALL_CHECKS=1` environment variable.

## Config Options

Configuration options that are not supported inside a [project](/guide/projects) config have <CRoot /> icon next to them. This means they can only be set in the root Vitest config.
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16
7 changes: 7 additions & 0 deletions guide/cli-generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,3 +963,10 @@ watch 模式下重新运行测试时清除终端屏幕(默认值:`true`)
- **Config:** [experimental.vcsProvider](/config/experimental#experimental-vcsprovider)

Custom provider for detecting changed files. (default: `git`)

### experimental.preParse

- **CLI:** `--experimental.preParse`
- **Config:** [experimental.preParse](/config/experimental#experimental-preparse)

Parse test specifications before running them. This will apply `.only` flag and test name pattern across all files without running them. (default: `false`)
9 changes: 9 additions & 0 deletions guide/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,12 @@ export function ignored() { // [!code error]

<img alt="html coverage in Vitest UI" img-light src="/ui-coverage-1-light.png">
<img alt="html coverage in Vitest UI" img-dark src="/ui-coverage-1-dark.png">

## Coverage in Agent Environments

When Vitest detects it is running inside an AI coding agent, it automatically adjusts the default `text` reporter to reduce output and minimize token usage:

- `skipFull: true` is set on the `text` reporter, so files with 100% coverage are omitted from the terminal output.
- The [`text-summary`](/config/coverage#coverage-reporter) reporter is added automatically, so the agent always sees a concise totals table even when `skipFull` hides all individual files.

These adjustments only apply when the `text` reporter is already part of the active reporter list (it is included in the default). Explicitly configured reporters are never removed.
4 changes: 4 additions & 0 deletions guide/extending-matchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ When using the global `expect` with concurrent tests, `this.task` is `undefined`

断言是否以 [`soft`](/api/expect#soft) 方式调用。您无需手动处理该逻辑,Vitest 始终会捕获错误。

## `assertion` <Advanced /> <Version type="experimental">4.1.4</Version> {#assertion}

The underlying [Chai assertion](https://www.chaijs.com/guide/plugins/) object. This is the same instance that Chai plugins receive, giving you access to Chai's flag system and chainable methods. This can be useful for building custom matchers that need to interact with Chai's internals.

::: tip
以上并非全部可用属性,仅列出最实用的部分。其他状态值由 Vitest 内部使用。
:::
14 changes: 14 additions & 0 deletions guide/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@

<div h-2 />

<<<<<<< HEAD

Check failure on line 20 in guide/features.md

View workflow job for this annotation

GitHub Actions / autofix

Unexpected additional H1 heading found
与 Vite 的配置、转换器、解析器和插件通用,将会使用应用中的相同配置来运行测试。
=======
::: tip
This page is a high-level overview of Vitest's capabilities. If you're new to Vitest, we recommend reading the [Learn](/guide/learn/writing-tests) tutorial first for a hands-on introduction.
:::

## Shared Config between Test, Dev and Build
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16

了解更多信息 [配置 Vitest](/guide/#configuring-vitest) 。

<<<<<<< HEAD
## 监听模式(watch mode) {#watch-mode}
=======
Learn more at [Configuring Vitest](/config/).

## Watch Mode
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16

```bash
$ vitest
Expand Down
36 changes: 36 additions & 0 deletions guide/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
---
<<<<<<< HEAD
title: 快速起步 | 指南
=======
title: Getting Started | Guide
Comment thread
NoiseFan marked this conversation as resolved.
Outdated
next:
text: Writing Tests
link: /guide/learn/writing-tests
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16
---

# 快速起步 {#getting-started}

Check failure on line 12 in guide/index.md

View workflow job for this annotation

GitHub Actions / autofix

Unexpected additional H1 heading found

## 总览 {#overview}

Expand Down Expand Up @@ -95,10 +102,17 @@
如果使用 Bun 作为软件包管理器,请确保使用 `bun run test` 命令而不是 `bun test` 命令,否则 Bun 将运行自己的测试运行程序。
:::

<<<<<<< HEAD

Check failure on line 105 in guide/index.md

View workflow job for this annotation

GitHub Actions / autofix

Unexpected additional H1 heading found
了解更多关于 Vitest 的使用,请参考 [API 索引](/api/test) 部分。
=======
Your first test is passing! Continue to [Writing Tests](/guide/learn/writing-tests) to learn about organizing tests, reading test output, and the core testing patterns you'll use every day.

To run tests once without watching for file changes, use `vitest run`. You can also pass additional flags like `--reporter` or `--coverage`. For a full list of CLI options, run `npx vitest --help` or see the [CLI guide](/guide/cli).
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16

## 配置 Vitest {#configuring-vitest}

<<<<<<< HEAD
Vitest 的主要优势之一是它与 Vite 的统一配置。如果存在,`vitest` 将读取你的根目录 `vite.config.ts` 以匹配插件并设置为你的 Vite 应用。例如,你的 Vite 有 [resolve.alias](https://cn.vitejs.dev/config/#resolve-alias) 和 [plugins](https://cn.vitejs.dev/guide/using-plugins.html) 的配置将会在 Vitest 中开箱即用。如果你想在测试期间想要不同的配置,你可以:

- 创建 `vitest.config.ts`,优先级将会最高。
Expand Down Expand Up @@ -227,7 +241,10 @@

## 自动安装依赖项 {#automatic-dependency-installation}

如果某些依赖项尚未安装,Vitest 会提示您安装。您可以通过设置 `VITEST_SKIP_INSTALL_CHECKS=1` 环境变量来禁用此行为。

Check failure on line 244 in guide/index.md

View workflow job for this annotation

GitHub Actions / autofix

Unexpected additional H1 heading found
=======
Vitest reads your `vite.config.*` by default, so your existing Vite plugins and configuration work out-of-the-box. You can also create a dedicated `vitest.config.*` for test-specific settings. See the [Config Reference](/config/) for details.
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16

## IDE 集成 {#ide-integrations}

Expand All @@ -241,6 +258,7 @@

| 示例 | 源代码 | 演练场 |
|---|---|---|
<<<<<<< HEAD
| `basic` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/basic) | [在线演示](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/basic?initialPath=__vitest__/) |
| `fastify` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/fastify) | [在线演示](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/fastify?initialPath=__vitest__/) |
| `in-source-test` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test) | [在线演示](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/in-source-test?initialPath=__vitest__/) |
Expand Down Expand Up @@ -303,5 +321,23 @@
然后,回到你的 Vitest 项目并运行 `pnpm link --global vitest`(或者使用你的其他包管理工具来全局链接 `Vitest`)。

## 社区 {#community}
=======
| `basic` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/basic) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/basic?initialPath=__vitest__/) |
| `fastify` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/fastify) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/fastify?initialPath=__vitest__/) |
| `in-source-test` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/in-source-test?initialPath=__vitest__/) |
| `lit` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/lit) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/lit?initialPath=__vitest__/) |
| `vue` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/vue) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/vue?initialPath=__vitest__/) |
| `marko` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/marko) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/marko?initialPath=__vitest__/) |
| `preact` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/preact) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/preact?initialPath=__vitest__/) |
| `qwik` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/qwik) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/qwik?initialPath=__vitest__/) |
| `react` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/react) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/react?initialPath=__vitest__/) |
| `solid` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/solid) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/solid?initialPath=__vitest__/) |
| `svelte` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/svelte) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/svelte?initialPath=__vitest__/) |
| `profiling` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/profiling) | Not Available |
| `typecheck` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/typecheck) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/typecheck?initialPath=__vitest__/) |
| `projects` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/projects) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/projects?initialPath=__vitest__/) |

## Community
>>>>>>> a141f9d49fa021ae0f1d6962f9dbc8bce3bbdd16

如果你有疑问或者需要帮助,可以到 [Discord](https://chat.vitest.dev) 和 [GitHub Discussions](https://github.com/vitest-dev/vitest/discussions) 社区来寻求帮助。
Loading
Loading