Skip to content
Open
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: 7 additions & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export default ({ mode }: { mode: string }) => {
},
}) as any,
],
define: {
__VITEST_VERSION__: JSON.stringify(version),
},
},
markdown: {
config(md) {
Expand Down Expand Up @@ -229,6 +232,10 @@ export default ({ mode }: { mode: string }) => {
text: '团队',
link: '/team',
},
{
text: 'Releases',
link: '/releases',
},
],
},
{
Expand Down
172 changes: 172 additions & 0 deletions .vitepress/theme/SupportedVersions.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<script setup lang="ts">
import { computed, ref } from 'vue'

declare const __VITEST_VERSION__: string

// Constants
const supportedVersionMessage = {
color: 'var(--vp-c-brand-1)',
text: 'supported',
}
const notSupportedVersionMessage = {
color: 'var(--vp-c-danger-1)',
text: 'not supported',
}
const previousMajorLatestMinors: Record<string, string> = {
1: '1.6',
2: '2.1',
3: '3.2',
4: '4.1',
}

// Current latest Vitest version and support info
const parsedViteVersion = parseVersion(__VITEST_VERSION__)!
const supportInfo = computeSupportInfo(parsedViteVersion)

// Check supported version input
const checkedVersion = ref(`${Math.max(parsedViteVersion.major - 2, 2)}.0.0`)
const checkedResult = computed(() => {
const version = checkedVersion.value
if (!isValidVitestVersion(version)) {
return notSupportedVersionMessage
}

const parsedVersion = parseVersion(checkedVersion.value)
if (!parsedVersion) {
return notSupportedVersionMessage
}

const satisfies = (targetVersion: string) => {
const compared = parseVersion(targetVersion)!
return (
parsedVersion.major === compared.major
&& parsedVersion.minor >= compared.minor
)
}
const satisfiesOneSupportedVersion
= parsedVersion.major >= parsedViteVersion.major // Treat future major versions as supported
|| supportInfo.regularPatches.some(satisfies)
|| supportInfo.importantFixes.some(satisfies)
|| supportInfo.securityPatches.some(satisfies)

return satisfiesOneSupportedVersion
? supportedVersionMessage
: notSupportedVersionMessage
})

function parseVersion(version: string) {
let [major, minor, patch] = version.split('.').map((v) => {
const num = /^\d+$/.exec(v)?.[0]

Check failure on line 59 in .vitepress/theme/SupportedVersions.vue

View workflow job for this annotation

GitHub Actions / autofix

Move this regular expression to module scope to avoid re-compilation on every call
return num ? Number.parseInt(num) : null
})
if (!major) {
return null
}
minor ??= 0
patch ??= 0

return { major, minor, patch }
}

function computeSupportInfo(
version: NonNullable<ReturnType<typeof parseVersion>>,
) {
const { major, minor } = version
const f = (versions: string[]) => {
return versions
.map(v => previousMajorLatestMinors[v] ?? v)
.filter((version) => {
if (!isValidVitestVersion(version)) {
return false
}
// Negative versions are invalid
if (/-\d/.test(version)) {

Check failure on line 83 in .vitepress/theme/SupportedVersions.vue

View workflow job for this annotation

GitHub Actions / autofix

Move this regular expression to module scope to avoid re-compilation on every call
return false
}
return true
})
}

return {
regularPatches: f([`${major}.${minor}`]),
importantFixes: f([`${major - 1}`, `${major}.${minor - 1}`]),
// unlike vite, we only support the last major version
securityPatches: f([`${major - 1}`, `${major}.${minor - 1}`]),
}
}

function versionsToText(versions: string[]) {
versions = versions.map(v => `<code>vitest@${v}</code>`)
if (versions.length === 0) {
return ''
}
if (versions.length === 1) {
return versions[0]
}
return (
`${versions.slice(0, -1).join(', ')} and ${versions[versions.length - 1]}`
)
}

function isValidVitestVersion(version: string) {
if (version.length === 1) {
version += '.'
}
// Vitest 0.x shouldn't be mentioned
if (version.startsWith('0.')) {
return false
}
return true
}
</script>

<template>
<div>
<ul>
<li v-if="supportInfo.regularPatches.length">
Regular patches are released for
<span v-html="versionsToText(supportInfo.regularPatches)" />.
</li>
<li v-if="supportInfo.importantFixes.length">
Important fixes and security patches are backported to
<span v-html="versionsToText(supportInfo.importantFixes)" />.
</li>
<li>
All versions before these are no longer supported. Users should upgrade
to receive updates.
</li>
</ul>
<p>
If you're using Vitest
<input
v-model="checkedVersion"
class="checked-input"
type="text"
placeholder="0.0.0"
>, it is
<strong :style="{ color: checkedResult.color }">{{
checkedResult.text
}}</strong>.
</p>
</div>
</template>

<style scoped>
.checked-input {
display: inline-block;
padding: 0px 5px;
width: 100px;
color: var(--vp-c-text-1);
background: var(--vp-c-bg-soft);
font-size: var(--vp-code-font-size);
font-family: var(--vp-font-family-mono);
border: 1px solid var(--vp-c-divider);
border-radius: 5px;
transition: border-color 0.1s;
}

.checked-input:focus,
.checked-input:hover {
border-color: var(--vp-c-brand);
}
</style>
9 changes: 8 additions & 1 deletion api/browser/svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ export function render<C extends Component>(
Component: ComponentImport<C>,
options?: ComponentOptions<C>,
renderOptions?: SetupOptions
): RenderResult<C> & PromiseLike<RenderResult<C>>
): Promise<RenderResult<C>>
```

`render` 函数会记录一个 `svelte.render` 追踪标记,该标记可在 [Trace View](/guide/browser/trace-view) 中查看。

<<<<<<< HEAD
::: warning
同步调用 `render` 的方式已被弃用,并将在下一个主要版本中移除。请始终使用 `await` 处理其返回结果:

Expand All @@ -54,6 +55,9 @@ const screen = await render(Component) // [!code ++]
:::

### 选项 {#options}
=======
### Options
>>>>>>> b8458066305a759bf414605c23780c31dccbd917

`render` 函数支持两种传参方式,一种是向 [`mount`](https://svelte.dev/docs/svelte/imperative-component-api#mount) 传入配置选项,另一种则是直接向组件传入属性:

Expand Down Expand Up @@ -174,10 +178,13 @@ function unmount(): Promise<void>

卸载并销毁 Svelte 组件。同时会在 [Trace View](/guide/browser/trace-view) 中记录一个 `svelte.unmount` 追踪标记。此功能适用于测试组件从页面中移除时的行为(例如测试是否未遗留事件监听器导致内存泄漏)。

<<<<<<< HEAD
::: warning
同步调用 `unmount` 的方式已被弃用,并将在下一个主要版本中移除。请始终使用 `await` 处理其返回结果:
:::

=======
>>>>>>> b8458066305a759bf414605c23780c31dccbd917
```ts
import { render } from 'vitest-browser-svelte'

Expand Down
16 changes: 13 additions & 3 deletions api/browser/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ test('counter button increments the count', async () => {
export function render(
component: Component,
options?: ComponentRenderOptions,
): RenderResult & PromiseLike<RenderResult>
): Promise<RenderResult>
```

`render` 函数会记录一个 `vue.render` 追踪标记,该标记可在 [Trace View](/guide/browser/trace-view) 中查看。

<<<<<<< HEAD
::: warning
同步调用 `render` 的方式已被弃用,并将在下一个主要版本中移除。请始终使用 `await` 处理其返回结果:

Expand All @@ -55,6 +56,9 @@ const screen = await render(Component) // [!code ++]
:::

### 选项 {#options}
=======
### Options
>>>>>>> b8458066305a759bf414605c23780c31dccbd917

`render` 函数支持 `@vue/test-utils` 中 [`mount` 选项](https://test-utils.vuejs.org/api/#mount) 的全部参数(除 `attachTo` 外,需改用 `container`)。此外还额外支持 `container` 和 `baseElement` 参数。

Expand Down Expand Up @@ -136,16 +140,19 @@ function debug(
#### rerender

```ts
function rerender(props: Partial<Props>): void & PromiseLike<void>
function rerender(props: Partial<Props>): Promise<void>
```
同时还会在 [Trace View](/guide/browser/trace-view) 中记录一个 `vue.rerender` 追踪标记。

为了更好地确保组件正确地更新属性,建议测试负责属性更新的组件本身,以避免在测试中依赖实现细节。尽管如此,如果你更倾向于在测试中更新已渲染组件的属性,可以使用此函数来实现。

<<<<<<< HEAD
::: warning
同步调用 `render` 的方式已被弃用,并将在下一个主要版本中移除。请始终使用 `await` 处理其返回结果:
:::

=======
>>>>>>> b8458066305a759bf414605c23780c31dccbd917
```js
import { render } from 'vitest-browser-vue'

Expand All @@ -158,15 +165,18 @@ rerender({ number: 2 })
#### unmount

```ts
function unmount(): void & PromiseLike<void>
function unmount(): Promise<void>
```

该操作会触发组件卸载,同时在 [跟踪视图](/guide/browser/trace-view) 中记录 `vue.unmount` 标记点。此功能特别适用于测试组件从页面移除时的行为(例如验证是否残留事件处理器导致内存泄漏)。

<<<<<<< HEAD
::: warning
同步调用 `unmount` 的方式已被弃用,将在下一主要版本中移除。请使用 `await` 进行异步调用。
:::

=======
>>>>>>> b8458066305a759bf414605c23780c31dccbd917
#### emitted

```ts
Expand Down
13 changes: 12 additions & 1 deletion config/browser/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ outline: deep

用于通过 `getByTestId` 定位器查找元素的属性。

## browser.locators.exact <Version type="experimental">4.1.3</Version> {#browser-locators-exact}
## browser.locators.exact

<<<<<<< HEAD
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove unresolved merge-conflict markers from docs

This commit leaves raw merge-conflict markers (e.g. <<<<<<< HEAD / ======= / >>>>>>>) in multiple published docs pages, which causes users to see conflict noise and contradictory guidance (for example, competing defaults and duplicated headings) instead of a single canonical instruction set. Please resolve the conflicts and keep only one final version of each affected section before merging.

Useful? React with 👍 / 👎.

- **类型:** `boolean`
- **默认值:** `false`

Expand All @@ -24,6 +25,16 @@ outline: deep
```ts
// 当 exact: false(默认值)时,会匹配 "Hello, World!"、"Say Hello, World" 等文本
// 当 exact: true 时,仅精确匹配字符串 "Hello, World"
=======
- **Type:** `boolean`
- **Default:** `true`

When set to `true`, [locators](/api/browser/locators) match text exactly by default, requiring a full, case-sensitive match. Individual locator calls can override this default via their own `exact` option.

```ts
// With exact: true (default), this only matches the string "Hello, World" exactly.
// With exact: false, this matches "Hello, World!", "Say Hello, World", etc.
>>>>>>> b8458066305a759bf414605c23780c31dccbd917
const locator = page.getByText('Hello, World', { exact: true })
await locator.click()
```
Expand Down
5 changes: 5 additions & 0 deletions config/forcereruntriggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ outline: deep

# forceRerunTriggers <CRoot />

<<<<<<< HEAD
- **类型:** `string[]`
- **默认值:** `['**/package.json/**', '**/vitest.config.*/**', '**/vite.config.*/**']`
=======
- **Type:** `string[]`
- **Default:** `['**/package.json', '**/vitest.config.*', '**/vite.config.*']`
>>>>>>> b8458066305a759bf414605c23780c31dccbd917

将触发整个测试套件重新运行的文件路径(glob 模式)。当与 `--changed` 参数配合使用时,如果在 git diff 中发现触发文件,就会运行整个测试套件。

Expand Down
4 changes: 2 additions & 2 deletions guide/browser/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ import { render } from 'vitest-browser-vue'
import Component from './Component.vue'

test('properly handles v-model', async () => {
const screen = render(Component)
const screen = await render(Component)

// 断言初始状态。
await expect.element(screen.getByText('Hi, my name is Alice')).toBeInTheDocument()
Expand All @@ -448,7 +448,7 @@ import { render } from 'vitest-browser-svelte'
import Greeter from './greeter.svelte'

test('greeting appears on click', async () => {
const screen = render(Greeter, { name: 'World' })
const screen = await render(Greeter, { name: 'World' })

const button = screen.getByRole('button')
await button.click()
Expand Down
4 changes: 4 additions & 0 deletions guide/cli-generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,13 @@
- **命令行终端:** `--browser.locators.exact`
- **配置:** [browser.locators.exact](/config/browser/locators#locators-exact)

<<<<<<< HEAD
定位器是否默认需完全匹配文本内容(默认值:`false`)
=======
Should locators match the text exactly by default (default: `true`)
>>>>>>> b8458066305a759bf414605c23780c31dccbd917

### pool

Check failure on line 471 in guide/cli-generated.md

View workflow job for this annotation

GitHub Actions / autofix

Heading level skipped from 1 to 3

- **命令行终端:** `--pool <pool>`
- **配置:** [pool](/config/pool)
Expand Down
Loading
Loading