@@ -225,12 +225,12 @@ export function foobar() {
225
225
import { vi } from ' vitest'
226
226
import * as mod from ' ./foobar.js'
227
227
228
- // this will only affect "foo" outside of the original module
228
+ // 这只会影响在原始模块之外的 "foo"
229
229
vi .spyOn (mod , ' foo' )
230
230
vi .mock (' ./foobar.js' , async (importOriginal ) => {
231
231
return {
232
232
... (await importOriginal ()),
233
- // this will only affect "foo" outside of the original module
233
+ // 这只会影响在原始模块之外的 "foo"
234
234
foo : () => ' mocked' ,
235
235
}
236
236
})
@@ -244,7 +244,7 @@ import * as mod from './foobar.js'
244
244
245
245
vi .spyOn (mod , ' foo' )
246
246
247
- // exported foo references mocked method
247
+ // 导出的 foo 引用模拟的方法
248
248
mod .foobar (mod .foo )
249
249
```
250
250
@@ -259,7 +259,7 @@ export function foobar(injectedFoo) {
259
259
}
260
260
```
261
261
262
- 这就是预期行为。当以这种方式进行嘲讽时,这通常是坏代码的标志 。考虑将代码重构为多个文件,或者使用[ 依赖项注入] ( https://en.wikipedia.org/wiki/dependency_injection ) 等技术来改进应用程序体系结构。
262
+ 这就是预期行为。当以这种方式包含 mock 时,这通常是不良代码的标志 。考虑将代码重构为多个文件,或者使用[ 依赖项注入] ( https://en.wikipedia.org/wiki/dependency_injection ) 等技术来改进应用程序体系结构。
263
263
264
264
### 示例
265
265
@@ -410,7 +410,7 @@ afterAll(() => server.close())
410
410
afterEach (() => server .resetHandlers ())
411
411
```
412
412
413
- > 使用 ` onUnhandleRequest: 'error' ` 配置服务器可以确保每当有没有相应请求处理程序的请求时都会引发错误 。
413
+ > 使用 ` onUnhandleRequest: 'error' ` 配置服务器可以确保即使某个请求没有相应的请求处理程序,也会抛出错误 。
414
414
415
415
### 示例
416
416
@@ -422,7 +422,7 @@ MSW 能做的还有很多。你可以访问 cookie 和查询参数、定义模
422
422
423
423
## 计时器
424
424
425
- 每当测试代码涉及到 ` 超时 ` 或者间隔时 ,并不是让我们的测试程序进行等待或者超时。我们也可以通过模拟对 ` setTimeout ` 和 ` setInterval ` 的调用来使用 "fake" 计时器来加速测试。
425
+ 每当测试代码涉及到 timeout 或者 interval 时 ,并不是让我们的测试程序进行等待或者超时。我们也可以通过模拟对 ` setTimeout ` 和 ` setInterval ` 的调用来使用 "fake" 计时器来加速测试。
426
426
427
427
有关更深入的详细 API 描述,参阅 [ ` vi.usefaketimers ` api 部分] ( /api/vi#vi-usefaketimers ) 。
428
428
@@ -432,11 +432,11 @@ MSW 能做的还有很多。你可以访问 cookie 和查询参数、定义模
432
432
import { afterEach , beforeEach , describe , expect , it , vi } from ' vitest'
433
433
434
434
function executeAfterTwoHours (func ) {
435
- setTimeout (func, 1000 * 60 * 60 * 2 ) // 2 hours
435
+ setTimeout (func, 1000 * 60 * 60 * 2 ) // 2小时
436
436
}
437
437
438
438
function executeEveryMinute (func ) {
439
- setInterval (func, 1000 * 60 ) // 1 minute
439
+ setInterval (func, 1000 * 60 ) // 1分钟
440
440
}
441
441
442
442
const mock = vi .fn (() => console .log (' executed' ))
@@ -455,7 +455,7 @@ describe('delayed execution', () => {
455
455
})
456
456
it (' should not execute the function' , () => {
457
457
executeAfterTwoHours (mock)
458
- // advancing by 2ms won't trigger the func
458
+ // 前进2毫秒并不会触发方法
459
459
vi .advanceTimersByTime (2 )
460
460
expect (mock).not .toHaveBeenCalled ()
461
461
})
@@ -534,7 +534,7 @@ vi.spyOn(exports, 'method').mockImplementation(() => {})
534
534
535
535
- 模拟模块导出 class implementation
536
536
537
- ` vi.mock ` and prototype 的示例:
537
+ ` vi.mock ` 和 prototype 的示例:
538
538
539
539
``` ts
540
540
// some-path.ts
@@ -549,10 +549,10 @@ vi.mock('./some-path.js', () => {
549
549
SomeClass .prototype .someMethod = vi .fn ()
550
550
return { SomeClass }
551
551
})
552
- // SomeClass.mock.instances will have SomeClass
552
+ // SomeClass.mock.instances 上将会有 someMethod 方法
553
553
```
554
554
555
- ` vi.mock ` and return value 的示例 :
555
+ ` vi.mock ` 和返回值配合的示例 :
556
556
557
557
``` ts
558
558
import { SomeClass } from ' ./some-path.js'
@@ -563,7 +563,7 @@ vi.mock('./some-path.js', () => {
563
563
}))
564
564
return { SomeClass }
565
565
})
566
- // SomeClass.mock.returns will have returned object
566
+ // SomeClass.mock.returns 将会返回对象
567
567
```
568
568
569
569
` vi.spyOn ` 的示例:
@@ -572,7 +572,7 @@ vi.mock('./some-path.js', () => {
572
572
import * as exports from ' ./some-path.js'
573
573
574
574
vi .spyOn (exports , ' SomeClass' ).mockImplementation (() => {
575
- // whatever suites you from first two examples
575
+ // 前两个例子中有非常适合你的
576
576
})
577
577
```
578
578
@@ -607,15 +607,15 @@ vi.mock('./some-path.js', () => {
607
607
method: vi .fn (),
608
608
}
609
609
}
610
- // now every time that useObject() is called it will
611
- // return the same object reference
610
+ // 现在每次调用 useObject() 后,都会
611
+ // 返回相同的对象引用
612
612
return _cache
613
613
}
614
614
return { useObject }
615
615
})
616
616
617
617
const obj = useObject ()
618
- // obj.method was called inside some-path
618
+ // obj.method 在 some-path 内调用
619
619
expect (obj .method ).toHaveBeenCalled ()
620
620
```
621
621
@@ -633,8 +633,8 @@ vi.mock('./some-path.js', async () => {
633
633
mocked: vi .fn (),
634
634
}
635
635
})
636
- original () // has original behaviour
637
- mocked () // is a spy function
636
+ original () // 有原始的行为
637
+ mocked () // 是一个 spy 函数
638
638
```
639
639
640
640
- 模拟当前日期
@@ -648,7 +648,7 @@ const mockDate = new Date(2022, 0, 1)
648
648
vi .setSystemTime (mockDate )
649
649
const now = new Date ()
650
650
expect (now .valueOf ()).toBe (mockDate .valueOf ())
651
- // reset mocked time
651
+ // 重置模拟的时间
652
652
vi .useRealTimers ()
653
653
```
654
654
@@ -668,7 +668,7 @@ expect(__VERSION__).toBe('1.0.0')
668
668
``` ts
669
669
import { beforeEach , expect , it } from ' vitest'
670
670
671
- // you can reset it in beforeEach hook manually
671
+ // 你可以在 beforeEach 钩子里手动重置
672
672
const originalViteEnv = import .meta .env .VITE_ENV
673
673
674
674
beforeEach (() => {
@@ -686,7 +686,7 @@ it('changes value', () => {
686
686
``` ts
687
687
import { expect , it , vi } from ' vitest'
688
688
689
- // before running tests "VITE_ENV" is "test"
689
+ // 在运行测试之前, "VITE_ENV" 的值是 "test"
690
690
import .meta .env .VITE_ENV === ' test'
691
691
692
692
it (' changes value' , () => {
0 commit comments