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: 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
174 changes: 174 additions & 0 deletions .vitepress/theme/SupportedVersions.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<script setup lang="ts">
import { computed, ref } from 'vue'

declare const __VITEST_VERSION__: string

const DIGITS_ONLY_RE = /^\d+$/
const NEGATIVE_VERSION_RE = /-\d/

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 = DIGITS_ONLY_RE.exec(v)?.[0]
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 (NEGATIVE_VERSION_RE.test(version)) {
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.at(-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>
15 changes: 1 addition & 14 deletions api/browser/svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,11 @@ 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) 中查看。

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

```ts
const screen = render(Component) // [!code --]
const screen = await render(Component) // [!code ++]
```
:::

### 选项 {#options}

`render` 函数支持两种传参方式,一种是向 [`mount`](https://svelte.dev/docs/svelte/imperative-component-api#mount) 传入配置选项,另一种则是直接向组件传入属性:
Expand Down Expand Up @@ -174,10 +165,6 @@ function unmount(): Promise<void>

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

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

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

Expand Down
23 changes: 3 additions & 20 deletions api/browser/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,11 @@ 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) 中查看。

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

```ts
const screen = render(Component) // [!code --]
const screen = await render(Component) // [!code ++]
```
:::

### 选项 {#options}

`render` 函数支持 `@vue/test-utils` 中 [`mount` 选项](https://test-utils.vuejs.org/api/#mount) 的全部参数(除 `attachTo` 外,需改用 `container`)。此外还额外支持 `container` 和 `baseElement` 参数。
Expand Down Expand Up @@ -136,16 +127,12 @@ 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` 追踪标记。

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

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

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

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

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

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

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

#### emitted

```ts
Expand Down
8 changes: 4 additions & 4 deletions config/browser/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ outline: deep

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

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

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

当设置为 `true` 时,[定位器](/api/browser/locators) 默认会执行精确文本匹配,要求完全且区分大小写的匹配。单个定位器调用可通过自身的 `exact` 选项覆盖此默认行为。

```ts
// 当 exact: false(默认值)时,会匹配 "Hello, World!"、"Say Hello, World" 等文本
// 当 exact: true 时,仅精确匹配字符串 "Hello, World"
// 当 exact: true(默认值)时,仅精确匹配字符串 "Hello, World"
// 当 exact: false 时,会匹配 "Hello, World!"、"Say Hello, World" 等文本
const locator = page.getByText('Hello, World', { exact: true })
await locator.click()
```
Expand Down
2 changes: 1 addition & 1 deletion config/forcereruntriggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ outline: deep
# forceRerunTriggers <CRoot />

- **类型:** `string[]`
- **默认值:** `['**/package.json/**', '**/vitest.config.*/**', '**/vite.config.*/**']`
- **默认值:** `['**/package.json', '**/vitest.config.*', '**/vite.config.*']`

将触发整个测试套件重新运行的文件路径(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
2 changes: 1 addition & 1 deletion guide/cli-generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ UI 模式和 HTML 报告器中提供的 HTML 覆盖率输出目录。
- **命令行终端:** `--browser.locators.exact`
- **配置:** [browser.locators.exact](/config/browser/locators#locators-exact)

定位器是否默认需完全匹配文本内容(默认值:`false`)
定位器是否默认需完全匹配文本内容(默认值:`true`)

### pool

Expand Down
13 changes: 12 additions & 1 deletion guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ outline: deep

# 迁移指南 {#migration-guide}

[迁移至 Vitest 3.0](https://v3.vitest.dev/guide/migration) | [迁移至 Vitest 2.0](https://v2.vitest.dev/guide/migration)
[迁移至 Vitest 4.0](https://v4.vitest.dev/guide/migration) | [迁移至 Vitest 3.0](https://v3.vitest.dev/guide/migration)

## 迁移至 Vitest 5.0 {#vitest-5}

Expand Down Expand Up @@ -55,6 +55,17 @@ export async function customClick(
await context.page.locator(selector).click()
}
```
<!-- TODO: translation -->
### Locators are Strict by Default

Browser locators now match the text exactly by default, requiring a full, case-sensitive match. To keep the previous behaviour, you can set [`browser.locators.exact`](/config/browser/locators#browser-locators-exact) to `false`.

```ts
// With exact: true (default), this only matches the string "Hello, World" exactly.
// With exact: false, this matches "Hello, World!", "Say Hello, World", etc.
const locator = page.getByText('Hello, World', { exact: true })
await locator.click()
```

### 移除了已弃用的入口 {#removed-deprecated-entrypoints}

Expand Down
Loading
Loading