Skip to content

Commit 2a4be1a

Browse files
authored
Merge pull request #378 from yionr/patch-3
docs(cn): typo & translate in mocking.md
2 parents deddf3b + 422e6dc commit 2a4be1a

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

guide/mocking.md

+22-22
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,12 @@ export function foobar() {
225225
import { vi } from 'vitest'
226226
import * as mod from './foobar.js'
227227

228-
// this will only affect "foo" outside of the original module
228+
// 这只会影响在原始模块之外的 "foo"
229229
vi.spyOn(mod, 'foo')
230230
vi.mock('./foobar.js', async (importOriginal) => {
231231
return {
232232
...(await importOriginal()),
233-
// this will only affect "foo" outside of the original module
233+
// 这只会影响在原始模块之外的 "foo"
234234
foo: () => 'mocked',
235235
}
236236
})
@@ -244,7 +244,7 @@ import * as mod from './foobar.js'
244244

245245
vi.spyOn(mod, 'foo')
246246

247-
// exported foo references mocked method
247+
// 导出的 foo 引用模拟的方法
248248
mod.foobar(mod.foo)
249249
```
250250

@@ -259,7 +259,7 @@ export function foobar(injectedFoo) {
259259
}
260260
```
261261

262-
这就是预期行为。当以这种方式进行嘲讽时,这通常是坏代码的标志。考虑将代码重构为多个文件,或者使用[依赖项注入](https://en.wikipedia.org/wiki/dependency_injection)等技术来改进应用程序体系结构。
262+
这就是预期行为。当以这种方式包含 mock 时,这通常是不良代码的标志。考虑将代码重构为多个文件,或者使用[依赖项注入](https://en.wikipedia.org/wiki/dependency_injection)等技术来改进应用程序体系结构。
263263

264264
### 示例
265265

@@ -410,7 +410,7 @@ afterAll(() => server.close())
410410
afterEach(() => server.resetHandlers())
411411
```
412412

413-
> 使用 `onUnhandleRequest: 'error'` 配置服务器可以确保每当有没有相应请求处理程序的请求时都会引发错误
413+
> 使用 `onUnhandleRequest: 'error'` 配置服务器可以确保即使某个请求没有相应的请求处理程序,也会抛出错误
414414
415415
### 示例
416416

@@ -422,7 +422,7 @@ MSW 能做的还有很多。你可以访问 cookie 和查询参数、定义模
422422

423423
## 计时器
424424

425-
每当测试代码涉及到 `超时` 或者间隔时,并不是让我们的测试程序进行等待或者超时。我们也可以通过模拟对 `setTimeout``setInterval` 的调用来使用 "fake" 计时器来加速测试。
425+
每当测试代码涉及到 timeout 或者 interval 时,并不是让我们的测试程序进行等待或者超时。我们也可以通过模拟对 `setTimeout``setInterval` 的调用来使用 "fake" 计时器来加速测试。
426426

427427
有关更深入的详细 API 描述,参阅 [`vi.usefaketimers` api 部分](/api/vi#vi-usefaketimers)
428428

@@ -432,11 +432,11 @@ MSW 能做的还有很多。你可以访问 cookie 和查询参数、定义模
432432
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
433433

434434
function executeAfterTwoHours(func) {
435-
setTimeout(func, 1000 * 60 * 60 * 2) // 2 hours
435+
setTimeout(func, 1000 * 60 * 60 * 2) // 2小时
436436
}
437437

438438
function executeEveryMinute(func) {
439-
setInterval(func, 1000 * 60) // 1 minute
439+
setInterval(func, 1000 * 60) // 1分钟
440440
}
441441

442442
const mock = vi.fn(() => console.log('executed'))
@@ -455,7 +455,7 @@ describe('delayed execution', () => {
455455
})
456456
it('should not execute the function', () => {
457457
executeAfterTwoHours(mock)
458-
// advancing by 2ms won't trigger the func
458+
// 前进2毫秒并不会触发方法
459459
vi.advanceTimersByTime(2)
460460
expect(mock).not.toHaveBeenCalled()
461461
})
@@ -534,7 +534,7 @@ vi.spyOn(exports, 'method').mockImplementation(() => {})
534534

535535
- 模拟模块导出 class implementation
536536

537-
`vi.mock` and prototype 的示例:
537+
`vi.mock` prototype 的示例:
538538

539539
```ts
540540
// some-path.ts
@@ -549,10 +549,10 @@ vi.mock('./some-path.js', () => {
549549
SomeClass.prototype.someMethod = vi.fn()
550550
return { SomeClass }
551551
})
552-
// SomeClass.mock.instances will have SomeClass
552+
// SomeClass.mock.instances 上将会有 someMethod 方法
553553
```
554554

555-
`vi.mock` and return value 的示例:
555+
`vi.mock` 和返回值配合的示例:
556556

557557
```ts
558558
import { SomeClass } from './some-path.js'
@@ -563,7 +563,7 @@ vi.mock('./some-path.js', () => {
563563
}))
564564
return { SomeClass }
565565
})
566-
// SomeClass.mock.returns will have returned object
566+
// SomeClass.mock.returns 将会返回对象
567567
```
568568

569569
`vi.spyOn` 的示例:
@@ -572,7 +572,7 @@ vi.mock('./some-path.js', () => {
572572
import * as exports from './some-path.js'
573573

574574
vi.spyOn(exports, 'SomeClass').mockImplementation(() => {
575-
// whatever suites you from first two examples
575+
// 前两个例子中有非常适合你的
576576
})
577577
```
578578

@@ -607,15 +607,15 @@ vi.mock('./some-path.js', () => {
607607
method: vi.fn(),
608608
}
609609
}
610-
// now every time that useObject() is called it will
611-
// return the same object reference
610+
// 现在每次调用 useObject() 后,都会
611+
// 返回相同的对象引用
612612
return _cache
613613
}
614614
return { useObject }
615615
})
616616

617617
const obj = useObject()
618-
// obj.method was called inside some-path
618+
// obj.method some-path 内调用
619619
expect(obj.method).toHaveBeenCalled()
620620
```
621621

@@ -633,8 +633,8 @@ vi.mock('./some-path.js', async () => {
633633
mocked: vi.fn(),
634634
}
635635
})
636-
original() // has original behaviour
637-
mocked() // is a spy function
636+
original() // 有原始的行为
637+
mocked() // 是一个 spy 函数
638638
```
639639

640640
- 模拟当前日期
@@ -648,7 +648,7 @@ const mockDate = new Date(2022, 0, 1)
648648
vi.setSystemTime(mockDate)
649649
const now = new Date()
650650
expect(now.valueOf()).toBe(mockDate.valueOf())
651-
// reset mocked time
651+
// 重置模拟的时间
652652
vi.useRealTimers()
653653
```
654654

@@ -668,7 +668,7 @@ expect(__VERSION__).toBe('1.0.0')
668668
```ts
669669
import { beforeEach, expect, it } from 'vitest'
670670

671-
// you can reset it in beforeEach hook manually
671+
// 你可以在 beforeEach 钩子里手动重置
672672
const originalViteEnv = import.meta.env.VITE_ENV
673673

674674
beforeEach(() => {
@@ -686,7 +686,7 @@ it('changes value', () => {
686686
```ts
687687
import { expect, it, vi } from 'vitest'
688688

689-
// before running tests "VITE_ENV" is "test"
689+
// 在运行测试之前, "VITE_ENV" 的值是 "test"
690690
import.meta.env.VITE_ENV === 'test'
691691

692692
it('changes value', () => {

0 commit comments

Comments
 (0)