Skip to content
Merged
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
7 changes: 0 additions & 7 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { defineConfig } from 'vitepress'
import {
groupIconMdPlugin,
groupIconVitePlugin,
localIconLoader,
} from 'vitepress-plugin-group-icons'
import llmstxt from 'vitepress-plugin-llms'
import { tabsMarkdownPlugin } from 'vitepress-plugin-tabs'
Expand Down Expand Up @@ -72,17 +71,11 @@ export default ({ mode }: { mode: string }) => {
groupIconVitePlugin({
customIcon: {
'CLI': 'vscode-icons:file-type-shell',
'vitest.shims': localIconLoader(import.meta.url, '../public/logo-without-border.svg'),
'vitest.config': localIconLoader(import.meta.url, '../public/logo-without-border.svg'),
'vitest.workspace': localIconLoader(import.meta.url, '../public/logo-without-border.svg'),
'.spec.ts': 'vscode-icons:file-type-testts',
'.test.ts': 'vscode-icons:file-type-testts',
'.spec.js': 'vscode-icons:file-type-testjs',
'.test.js': 'vscode-icons:file-type-testjs',
'marko': 'vscode-icons:file-type-marko',
'qwik': 'logos:qwik-icon',
'next': '',
'vite.config': localIconLoader(import.meta.url, '../public/logo-without-border-vite.svg'),
},
}) as any,
llmstxt(),
Expand Down
70 changes: 70 additions & 0 deletions api/browser/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ export const server: {
}
```

<!-- TODO: translation -->

## `utils`

Utility functions useful for custom render libraries.
Expand Down Expand Up @@ -225,3 +227,71 @@ export const utils: {
getElementError(selector: string, container?: Element): Error
}
```

### configurePrettyDOM <Version>4.0.0</Version> {#configureprettydom}

The `configurePrettyDOM` function allows you to configure default options for the `prettyDOM` and `debug` functions. This is useful for customizing how HTML is formatted in test failure messages.

```ts
import { utils } from 'vitest/browser'

utils.configurePrettyDOM({
maxDepth: 3,
filterNode: 'script, style, [data-test-hide]'
})
```

#### Options

- **`maxDepth`** - Maximum depth to print nested elements (default: `Infinity`)
- **`maxLength`** - Maximum length of the output string (default: `7000`)
- **`filterNode`** - A CSS selector string or function to filter out nodes from the output. When a string is provided, elements matching the selector will be excluded. When a function is provided, it should return `false` to exclude a node.
- **`highlight`** - Enable syntax highlighting (default: `true`)
- And other options from [`pretty-format`](https://www.npmjs.com/package/@vitest/pretty-format)

#### Filtering with CSS Selectors <Version>4.1.0</Version> {#filtering-with-css-selectors}

The `filterNode` option allows you to hide irrelevant markup (like scripts, styles, or hidden elements) from test failure messages, making it easier to identify the actual cause of failures.

```ts
import { utils } from 'vitest/browser'

// Filter out common noise elements
utils.configurePrettyDOM({
filterNode: 'script, style, [data-test-hide]'
})

// Or use directly with prettyDOM
const html = utils.prettyDOM(element, undefined, {
filterNode: 'script, style'
})
```

**Common Patterns:**

Filter out scripts and styles:
```ts
utils.configurePrettyDOM({ filterNode: 'script, style' })
```

Hide specific elements with data attributes:
```ts
utils.configurePrettyDOM({ filterNode: '[data-test-hide]' })
```

Hide nested content within an element:
```ts
// Hides all children of elements with data-test-hide-content
utils.configurePrettyDOM({ filterNode: '[data-test-hide-content] *' })
```

Combine multiple selectors:
```ts
utils.configurePrettyDOM({
filterNode: 'script, style, [data-test-hide], svg'
})
```

::: tip
This feature is inspired by Testing Library's [`defaultIgnore`](https://testing-library.com/docs/dom-testing-library/api-configuration/#defaultignore) configuration.
:::
34 changes: 34 additions & 0 deletions config/browser/playwright.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,37 @@ await userEvent.click(page.getByRole('button'), {
timeout: 1_000,
})
```

<!-- TODO: translation -->

## `persistentContext` <Version>4.1.0</Version> {#persistentcontext}

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

When enabled, Vitest uses Playwright's [persistent context](https://playwright.dev/docs/api/class-browsertype#browser-type-launch-persistent-context) instead of a regular browser context. This allows browser state (cookies, localStorage, DevTools settings, etc.) to persist between test runs.

::: warning
This option is ignored when running tests in parallel (e.g. when headless with [`fileParallelism`](/config/fileparallelism) enalbed) since persistent context cannot be shared across parallel sessions.
:::

- When set to `true`, the user data is stored in `./node_modules/.cache/vitest-playwright-user-data`
- When set to a string, the value is used as the path to the user data directory

```ts [vitest.config.js]
import { playwright } from '@vitest/browser-playwright'
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
browser: {
provider: playwright({
persistentContext: true,
// or specify a custom directory:
// persistentContext: './my-browser-data',
}),
instances: [{ browser: 'chromium' }],
},
},
})
```
4 changes: 2 additions & 2 deletions config/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ If a `RegExp` is provided, it is matched against the full file path.
- **Type:** `boolean`
- **Default:** `false`

When a dependency is a valid ESM package, try to guess the cjs version based on the path. This might be helpful, if a dependency has the wrong ESM file.
When enabled, Vitest will try to guess a CommonJS build for an ESM entry by checking a few common CJS/UMD file name and folder patterns (like `.mjs`, `.umd.js`, `.cjs.js`, `umd/`, `cjs/`, `lib/`).

This might potentially cause some misalignment if a package has different logic in ESM and CJS mode.
This is a best-effort heuristic to work around confusing or incorrect ESM/CJS packaging and may not work for all dependencies.
45 changes: 45 additions & 0 deletions guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,51 @@ tests/test1.test.ts
tests/test2.test.ts
```

<!-- TODO: translation -->

## Shell Autocompletions

Vitest provides shell autocompletions for commands, options, and option values powered by [`@bomb.sh/tab`](https://github.com/bombshell-dev/tab).

### Setup

For permanent setup in zsh, add this to your `~/.zshrc`:

```bash
# Add to ~/.zshrc for permanent autocompletions (same can be done for other shells)
source <(vitest complete zsh)
```

### Package Manager Integration

`@bomb.sh/tab` integrates with [package managers](https://github.com/bombshell-dev/tab?tab=readme-ov-file#package-manager-completions). Autocompletions work when running vitest directly:

::: code-group

```bash [npm]
npm vitest <Tab>
```

```bash [npm]
npm exec vitest <Tab>
```

```bash [pnpm]
pnpm vitest <Tab>
```

```bash [yarn]
yarn vitest <Tab>
```

```bash [bun]
bun vitest <Tab>
```

:::

For package manager autocompletions, you should install [tab's package manager completions](https://github.com/bombshell-dev/tab?tab=readme-ov-file#package-manager-completions) separately.

## 选项 {#options}

::: tip
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"vite": "^7.3.1",
"vite-plugin-pwa": "^1.2.0",
"vitepress": "2.0.0-alpha.15",
"vitepress-plugin-group-icons": "^1.6.5",
"vitepress-plugin-group-icons": "^1.7.1",
"vitepress-plugin-llms": "^1.10.0",
"vitepress-plugin-tabs": "^0.7.3",
"vitest": "^4.0.17",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions todo.md

This file was deleted.