@@ -41,11 +41,11 @@ expect(input).toBe(2) // jest API
4141
4242## assert
4343
44- - ** Type :** ` Chai.AssertStatic `
44+ - ** 类型 :** ` Chai.AssertStatic `
4545
46- Vitest reexports chai's [ ` assert ` API] ( https://www.chaijs.com/api/assert/ ) as ` expect.assert ` for convenience. You can see the supported methods on the [ Assert API page] ( /api/assert ) .
46+ Vitest 将 Chai 的 [ ` assert ` API] ( https://www.chaijs.com/api/assert/ ) 以 ` expect.assert ` 的形式重新导出。你可以在 [ Assert API page] ( /api/assert ) 页面查看支持的方法。
4747
48- This is especially useful if you need to narrow down the type, since ` expect.to* ` methods do not support that:
48+ 如果你需要缩小类型范围时,这将特别有用,因为 ` expect.to* ` 方法不支持此功能:
4949
5050``` ts
5151interface Cat {
@@ -61,12 +61,12 @@ type Animal = Cat | Dog
6161const animal: Animal = { __type: ' Dog' , bark : () => {} }
6262
6363expect .assert (animal .__type === ' Dog' )
64- // does not show a type error!
64+ // 不显示类型错误!
6565expect (animal .bark ()).toBeUndefined ()
6666```
6767
6868::: tip
69- Note that ` expect.assert ` also supports other type-narrowing methods (like ` assert.isDefined ` , ` assert.exists ` and so on).
69+ 注意, ` expect.assert ` 还支持其他缩小类型的方法(如: ` assert.isDefined ` , ` assert.exists ` 等)。
7070:::
7171
7272## soft
@@ -79,10 +79,10 @@ Note that `expect.assert` also supports other type-narrowing methods (like `asse
7979import { expect , test } from ' vitest'
8080
8181test (' expect.soft test' , () => {
82- expect .soft (1 + 1 ).toBe (3 ) // mark the test as fail and continue
83- expect .soft (1 + 2 ).toBe (4 ) // mark the test as fail and continue
82+ expect .soft (1 + 1 ).toBe (3 ) // 将期望标记为失败并继续
83+ expect .soft (1 + 2 ).toBe (4 ) // 将期望标记为失败并继续
8484})
85- // reporter will report both errors at the end of the run
85+ // 在执行结束后,报告器会报告这两个错误。
8686```
8787
8888它也可以与 ` expect ` 一起使用。 如果 ` expect ` 断言失败,测试将终止并显示所有错误。
@@ -91,9 +91,9 @@ test('expect.soft test', () => {
9191import { expect , test } from ' vitest'
9292
9393test (' expect.soft test' , () => {
94- expect .soft (1 + 1 ).toBe (3 ) // mark the test as fail and continue
95- expect (1 + 2 ).toBe (4 ) // failed and terminate the test, all previous errors will be output
96- expect .soft (1 + 3 ).toBe (5 ) // do not run
94+ expect .soft (1 + 1 ).toBe (3 ) // 将期望标记为失败并继续
95+ expect (1 + 2 ).toBe (4 ) // 测试失败并终止执行,所有先前错误将被输出
96+ expect .soft (1 + 3 ).toBe (5 ) // 不再执行
9797})
9898```
9999
@@ -195,13 +195,13 @@ test('stocks are the same', () => {
195195import { expect , test } from ' vitest'
196196
197197test .fails (' decimals are not equal in javascript' , () => {
198- expect (0.2 + 0.1 ).toBe (0.3 ) // 0.2 + 0.1 is 0.30000000000000004
198+ expect (0.2 + 0.1 ).toBe (0.3 ) // 0.2 + 0.1 = 0.30000000000000004
199199})
200200
201201test (' decimals are rounded to 5 after the point' , () => {
202- // 0.2 + 0.1 is 0.30000 | "000000000004" removed
202+ // 0.2 + 0.1 等于 0.30000 | "000000000004" 被移除
203203 expect (0.2 + 0.1 ).toBeCloseTo (0.3 , 5 )
204- // nothing from 0.30000000000000004 is removed
204+ // 没有从 0.30000000000000004 移除任何内容
205205 expect (0.2 + 0.1 ).not .toBeCloseTo (0.3 , 50 )
206206})
207207```
@@ -590,9 +590,9 @@ test('the fruit list contains orange', () => {
590590 expect (getAllFruits ()).toContain (' orange' )
591591
592592 const element = document .querySelector (' #el' )
593- // element has a class
593+ // element 有 class 属性 flex
594594 expect (element .classList ).toContain (' flex' )
595- // element is inside another one
595+ // element 是 #wrapper 的子元素
596596 expect (document .querySelector (' #wrapper' )).toContain (element )
597597})
598598```
@@ -664,25 +664,25 @@ const invoice = {
664664}
665665
666666test (' John Doe Invoice' , () => {
667- expect (invoice ).toHaveProperty (' isActive' ) // assert that the key exists
668- expect (invoice ).toHaveProperty (' total_amount' , 5000 ) // assert that the key exists and the value is equal
667+ expect (invoice ).toHaveProperty (' isActive' ) // 断言对应的key存在
668+ expect (invoice ).toHaveProperty (' total_amount' , 5000 ) // 断言对应的key存在,并且值相等
669669
670- expect (invoice ).not .toHaveProperty (' account' ) // assert that this key does not exist
670+ expect (invoice ).not .toHaveProperty (' account' ) // 断言对应的key不存在
671671
672- // Deep referencing using dot notation
672+ // 使用点号进行深层引用
673673 expect (invoice ).toHaveProperty (' customer.first_name' )
674674 expect (invoice ).toHaveProperty (' customer.last_name' , ' Doe' )
675675 expect (invoice ).not .toHaveProperty (' customer.location' , ' India' )
676676
677- // Deep referencing using an array containing the key
677+ // 使用含键名的数组进行深层引用
678678 expect (invoice ).toHaveProperty (' items[0].type' , ' apples' )
679- expect (invoice ).toHaveProperty (' items.0.type' , ' apples' ) // dot notation also works
679+ expect (invoice ).toHaveProperty (' items.0.type' , ' apples' ) // 也可以使用点号
680680
681- // Deep referencing using an array containing the keyPath
681+ // 通过包含键路径的数组实现深层引用
682682 expect (invoice ).toHaveProperty ([' items' , 0 , ' type' ], ' apples' )
683- expect (invoice ).toHaveProperty ([' items' , ' 0' , ' type' ], ' apples' ) // string notation also works
683+ expect (invoice ).toHaveProperty ([' items' , ' 0' , ' type' ], ' apples' ) // 字符串表示法同样适用
684684
685- // Wrap your key in an array to avoid the key from being parsed as a deep reference
685+ // 将键名包裹在数组中,避免其被解析为深层引用
686686 expect (invoice ).toHaveProperty ([' P.O' ], ' 12345' )
687687})
688688```
@@ -790,15 +790,15 @@ function getFruitStock(type: string) {
790790 throw new Error (' Pineapples are not in stock' )
791791 }
792792
793- // Do some other stuff
793+ // 做一些其他的东西
794794}
795795
796796test (' throws on pineapples' , () => {
797- // Test that the error message says " stock" somewhere: these are equivalent
797+ // 测试错误信息包含 “ stock”,这两种写法是等效的
798798 expect (() => getFruitStock (' pineapples' )).toThrowError (/ stock/ )
799799 expect (() => getFruitStock (' pineapples' )).toThrowError (' stock' )
800800
801- // Test the exact error message
801+ // 测试确切的错误信息
802802 expect (() => getFruitStock (' pineapples' )).toThrowError (
803803 / ^ Pineapples are not in stock$ /
804804 )
@@ -872,7 +872,7 @@ import { expect, test } from 'vitest'
872872
873873test (' matches inline snapshot' , () => {
874874 const data = { foo: new Set ([' bar' , ' snapshot' ]) }
875- // Vitest will update following content when updating the snapshot
875+ // Vitest 将在更新快照的同时更新以下内容
876876 expect (data ).toMatchInlineSnapshot (`
877877 {
878878 "foo": Set {
@@ -1371,7 +1371,7 @@ async function buyApples() {
13711371}
13721372
13731373test (' buyApples returns new stock id' , async () => {
1374- // toEqual returns a promise now, so you HAVE to await it
1374+ // toEqual 现在返回一个 Promise,调用时需添加 await
13751375 await expect (buyApples ()).resolves .toEqual ({ id: 1 }) // jest API
13761376 await expect (buyApples ()).resolves .to .equal ({ id: 1 }) // chai API
13771377})
@@ -1403,7 +1403,7 @@ async function buyApples(id) {
14031403}
14041404
14051405test (' buyApples throws an error when no id provided' , async () => {
1406- // toThrow returns a promise now, so you HAVE to await it
1406+ // toThrow 现在返回一个 Promise,调用时需添加 await
14071407 await expect (buyApples ()).rejects .toThrow (' no id' )
14081408})
14091409```
@@ -1464,7 +1464,7 @@ function onSelect(cb) {
14641464 cbs .push (cb )
14651465}
14661466
1467- // after selecting from db, we call all callbacks
1467+ // 在数据库查询操作之后,立即执行全部回调函数
14681468function select(id ) {
14691469 return db .select ({ id }).then ((data ) => {
14701470 return Promise .all (cbs .map (cb => cb (data )))
@@ -1474,11 +1474,11 @@ function select(id) {
14741474test (' callback was called' , async () => {
14751475 expect .hasAssertions ()
14761476 onSelect ((data ) => {
1477- // should be called on select
1477+ // 必须在 select 操作时调用
14781478 expect (data ).toBeTruthy ()
14791479 })
1480- // if not awaited, test will fail
1481- // if you don't have expect.hasAssertions(), test will pass
1480+ // 若没有添加 await 测试将会失败
1481+ // 若缺少 expect.hasAssertions(), 测试仍会通过
14821482 await select (3 )
14831483})
14841484```
@@ -1519,7 +1519,7 @@ test.each(errorDirs)('build fails with "%s"', async (dir) => {
15191519 expect (err .message ).toBe (` ${dir }/src does not exist ` )
15201520 break
15211521 default :
1522- // to exhaust all error tests
1522+ // 必须覆盖所有错误测试场景
15231523 expect .unreachable (' All error test must be handled' )
15241524 break
15251525 }
0 commit comments