@@ -8,7 +8,7 @@ outline: deep
8
8
此页面提供有关 Vitest API 中实验性浏览器模式功能的信息,该功能允许你在浏览器中本地运行测试,提供对窗口和文档等浏览器全局变量的访问。此功能目前正在开发中,API 未来可能会更改。
9
9
10
10
::: tip
11
- If you are looking for documentation for ` expect ` , ` vi ` or any general API like workspaces or type testing, refer to the [ "Getting Started" guide ] ( /guide/ ) .
11
+ 如果你正在寻找关于 ` expect ` 、 ` vi ` 或任何通用 API(如工作区或类型测试)的文档,请参阅 [ "入门指南" ] ( . /guide/) 。
12
12
:::
13
13
14
14
<img alt =" Vitest UI " img-light src =" /ui-browser-1-light.png " >
@@ -206,8 +206,7 @@ import { defineWorkspace } from 'vitest/config'
206
206
export default defineWorkspace ([
207
207
{
208
208
test: {
209
- // an example of file based convention,
210
- // you don't have to follow it
209
+ // 文件约定的示例,你不必遵循它。
211
210
include: [
212
211
' tests/unit/**/*.{test,spec}.ts' ,
213
212
' tests/**/*.unit.{test,spec}.ts' ,
@@ -218,8 +217,7 @@ export default defineWorkspace([
218
217
},
219
218
{
220
219
test: {
221
- // an example of file based convention,
222
- // you don't have to follow it
220
+ // 文件约定的示例,你不必遵循它。
223
221
include: [
224
222
' tests/browser/**/*.{test,spec}.ts' ,
225
223
' tests/**/*.browser.{test,spec}.ts' ,
@@ -409,14 +407,13 @@ import { render } from './my-render-function.js'
409
407
test (' properly handles form inputs' , async () => {
410
408
render () // mount DOM elements
411
409
412
- // Asserts initial state.
410
+ // 断言初始状态。
413
411
await expect .element (page .getByText (' Hi, my name is Alice' )).toBeInTheDocument ()
414
412
415
- // Get the input DOM node by querying the associated label.
413
+ // 通过查询关联的标签获取输入的 DOM 节点。
416
414
const usernameInput = page .getByLabelText (/ username/ i )
417
415
418
- // Type the name into the input. This already validates that the input
419
- // is filled correctly, no need to check the value manually.
416
+ // 将名称输入到输入框中。这已经验证了输入框中的值是正确的,无需手动检查其值。
420
417
await usernameInput .fill (' Bob' )
421
418
422
419
await expect .element (page .getByText (' Hi, my name is Bob' )).toBeInTheDocument ()
@@ -456,14 +453,13 @@ import Component from './Component.vue'
456
453
test (' properly handles v-model' , async () => {
457
454
const screen = render (Component )
458
455
459
- // Asserts initial state.
456
+ // 断言初始状态。
460
457
await expect .element (screen .getByText (' Hi, my name is Alice' )).toBeInTheDocument ()
461
458
462
- // Get the input DOM node by querying the associated label.
459
+ // 通过查询关联的标签获取输入的 DOM 节点。
463
460
const usernameInput = screen .getByLabelText (/ username/ i )
464
461
465
- // Type the name into the input. This already validates that the input
466
- // is filled correctly, no need to check the value manually.
462
+ // 将名称输入到输入框中。这已经验证了输入框中的值是正确的,无需手动检查其值。
467
463
await usernameInput .fill (' Bob' )
468
464
469
465
await expect .element (screen .getByText (' Hi, my name is Bob' )).toBeInTheDocument ()
@@ -490,14 +486,14 @@ import { render } from 'vitest-browser-react'
490
486
import Fetch from ' ./fetch'
491
487
492
488
test (' loads and displays greeting' , async () => {
493
- // Render a React element into the DOM
489
+ // 将一个 React 元素渲染到 DOM 中。
494
490
const screen = render (<Fetch url = " /greeting" />)
495
491
496
492
await screen .getByText (' Load Greeting' ).click ()
497
- // wait before throwing an error if it cannot find an element
493
+ // 如果找不到元素,则等待一段时间后再抛出错误。
498
494
const heading = screen .getByRole (' heading' )
499
495
500
- // assert that the alert message is correct
496
+ // 断言警告消息是正确的。
501
497
await expect .element (heading ).toHaveTextContent (' hello there' )
502
498
await expect .element (screen .getByRole (' button' )).toBeDisabled ()
503
499
})
@@ -538,12 +534,11 @@ test('shows the children when the checkbox is checked', async () => {
538
534
539
535
const screen = page .elementLocator (baseElement )
540
536
541
- // .query() will return the element or null if it cannot be found.
542
- // .element() will return the element or throw an error if it cannot be found.
537
+ // .query() 将返回找到的元素或在未找到时返回 null。
538
+ // .element() 会返回该元素,如果找不到该元素则会抛出错误。
543
539
expect (screen .getByText (testMessage ).query ()).not .toBeInTheDocument ()
544
540
545
- // The queries can accept a regex to make your selectors more
546
- // resilient to content tweaks and changes.
541
+ // 查询可以接受正则表达式,使选择器更能适应内容的调整和变化。
547
542
await screen .getByLabelText (/ show/ i ).click ()
548
543
549
544
await expect .element (screen .getByText (testMessage )).toBeInTheDocument ()
0 commit comments