@@ -60,7 +60,7 @@ const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
6060expect(person.greet('Alice')).toBe('mocked')
6161expect(spy.mock.calls).toEqual([['Alice']])
6262
63- // clear call history but keep mock implementation
63+ // 清空历史,但是保留模拟对象的实现
6464spy.mockClear()
6565expect(spy.mock.calls).toEqual([])
6666expect(person.greet('Bob')).toBe('mocked')
@@ -87,7 +87,7 @@ function mockImplementation(fn: T): Mock<T>
8787
8888` ` ` ts
8989const mockFn = vi.fn().mockImplementation((apples: number) => apples + 1)
90- // or: vi.fn(apples => apples + 1);
90+ // 或: vi.fn(apples => apples + 1);
9191
9292const NelliesBucket = mockFn(0)
9393const BobsBucket = mockFn(1)
@@ -110,11 +110,11 @@ function mockImplementationOnce(fn: T): Mock<T>
110110` ` ` ts
111111const myMockFn = vi
112112 .fn()
113- .mockImplementationOnce(() => true) // 1st call
114- .mockImplementationOnce(() => false) // 2nd call
113+ .mockImplementationOnce(() => true) // 1st 调用
114+ .mockImplementationOnce(() => false) // 2nd 调用
115115
116- myMockFn() // 1st call: true
117- myMockFn() // 2nd call: false
116+ myMockFn() // 1st 调用: true
117+ myMockFn() // 2nd 调用: false
118118` ` `
119119
120120当 mock 函数用完所有实现后,如果之前调用过 ` vi.fn(() => defaultValue) ` 或 ` .mockImplementation(() => defaultValue) ` ,它将调用设置的默认实现:
@@ -163,7 +163,7 @@ myMockFn() // 'original'
163163test('async callback', () => {
164164 const myMockFn = vi.fn(() => 'original')
165165
166- // We await this call since the callback is async
166+ // 由于回调是异步的,我们要等待这个调用
167167 await myMockFn.withImplementation(
168168 () => 'temp',
169169 async () => {
@@ -188,7 +188,7 @@ function mockRejectedValue(value: unknown): Mock<T>
188188` ` ` ts
189189const asyncMock = vi.fn().mockRejectedValue(new Error('Async error'))
190190
191- await asyncMock() // throws Error<'Async error'>
191+ await asyncMock() // 抛出 Error<'Async error'>
192192` ` `
193193
194194## mockRejectedValueOnce
@@ -206,7 +206,7 @@ const asyncMock = vi
206206 .mockRejectedValueOnce(new Error('Async error'))
207207
208208await asyncMock() // 'first call'
209- await asyncMock() // throws Error<'Async error'>
209+ await asyncMock() // 抛出 Error<'Async error'>
210210` ` `
211211
212212## mockReset
@@ -233,7 +233,7 @@ const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
233233expect(person.greet('Alice')).toBe('mocked')
234234expect(spy.mock.calls).toEqual([['Alice']])
235235
236- // clear call history and reset implementation, but method is still spied
236+ // 清空历史和重置实现,但是方法依然是被监视
237237spy.mockReset()
238238expect(spy.mock.calls).toEqual([])
239239expect(person.greet).toBe(spy)
@@ -261,7 +261,7 @@ const spy = vi.spyOn(person, 'greet').mockImplementation(() => 'mocked')
261261expect(person.greet('Alice')).toBe('mocked')
262262expect(spy.mock.calls).toEqual([['Alice']])
263263
264- // clear call history and restore spied object method
264+ // 清空调用历史和恢复被监视对象方法
265265spy.mockRestore()
266266expect(spy.mock.calls).toEqual([])
267267expect(person.greet).not.toBe(spy)
@@ -392,8 +392,8 @@ argument.value = 10
392392
393393expect(fn).toHaveBeenCalledWith({ value: 0 }) // [!code --]
394394
395- // The equality check is done against the original argument,
396- // but its property was changed between the call and assertion
395+ // 相等性检查是针对原始参数进行的,
396+ // 但是,该参数的属性在调用和断言之间发生了更改。
397397expect(fn).toHaveBeenCalledWith({ value: 10 }) // [!code ++]
398398` ` `
399399
@@ -423,8 +423,8 @@ const lastCall: Parameters<T> | undefined
423423interface MockResultReturn<T> {
424424 type: 'return'
425425 /**
426- * The value that was returned from the function.
427- * If the function returned a Promise, then this will be a resolved value.
426+ * 函数返回的值。
427+ * 如果函数返回的是一个 Promise,那么这将是一个已解析的值。
428428 */
429429 value: T
430430}
@@ -437,7 +437,7 @@ interface MockResultIncomplete {
437437interface MockResultThrow {
438438 type: 'throw'
439439 /**
440- * An error that was thrown during function execution.
440+ * 函数执行过程中抛出的错误。
441441 */
442442 value: any
443443}
@@ -466,21 +466,21 @@ const fn = vi
466466 throw new Error('thrown error')
467467 })
468468
469- const result = fn() // returned 'result'
469+ const result = fn() // 返回 'result'
470470
471471try {
472- fn() // threw Error
472+ fn() // 抛出错误
473473}
474474catch {}
475475
476476fn.mock.results
477477=== [
478- // first result
478+ // 首个结果
479479 {
480480 type: 'return',
481481 value: 'result',
482482 },
483- // last result
483+ // 最后结果
484484 {
485485 type: 'throw',
486486 value: Error,
0 commit comments