Skip to content

Commit f2a17c2

Browse files
authored
Merge pull request #785 from NoiseFan/docs/mocking
docs: update /guide/mocking
2 parents 9b9bb09 + cd0d4f7 commit f2a17c2

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

guide/mocking.md

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@ outline: false
1313

1414
如果你不熟悉 `vi.fn``vi.mock``vi.spyOn` 方法,请先查看[API部分](/api/vi)
1515

16-
Vitest has a comprehensive list of guides regarding mocking:
16+
Vitest 提供了有关模拟的全部指南:
1717

18-
- [Mocking Classes](/guide/mocking/classes.md)
19-
- [Mocking Dates](/guide/mocking/dates.md)
20-
- [Mocking the File System](/guide/mocking/file-system.md)
21-
- [Mocking Functions](/guide/mocking/functions.md)
22-
- [Mocking Globals](/guide/mocking/globals.md)
23-
- [Mocking Modules](/guide/mocking/modules.md)
24-
- [Mocking Requests](/guide/mocking/requests.md)
25-
- [Mocking Timers](/guide/mocking/timers.md)
18+
- [模拟类](/guide/mocking/classes.md)
19+
- [模拟日期](/guide/mocking/dates.md)
20+
- [模拟文件系统](/guide/mocking/file-system.md)
21+
- [模拟函数](/guide/mocking/functions.md)
22+
- [模拟全局对象](/guide/mocking/globals.md)
23+
- [模拟模块](/guide/mocking/modules.md)
24+
- [模拟请求](/guide/mocking/requests.md)
25+
- [模拟计时器](/guide/mocking/timers.md)
2626

2727
为了更简单快捷地开始使用模拟,你可以查看下面的备忘单。
2828

29-
## 备忘单 {#cheat-sheet}
29+
## 速查表 {#cheat-sheet}
3030

31-
I want to…
32-
33-
### Mock exported variables
31+
### 模拟导出变量 {#mock-exported-variables}
3432
```js [example.js]
3533
export const getter = 'variable'
3634
```
@@ -41,18 +39,10 @@ vi.spyOn(exports, 'getter', 'get').mockReturnValue('mocked')
4139
```
4240

4341
::: warning
44-
此方法在浏览器模式中无法使用。如需替代方案,请查看 [限制部分](/guide/browser/#spying-on-module-exports)
42+
此方法在浏览器模式中无法使用。如需替代方案,请查看 [限制](/guide/browser/#spying-on-module-exports) 部分
4543
:::
4644

47-
### 对模块中导出的函数进行 mock。 {#mock-an-exported-function}
48-
49-
```ts
50-
import * as exports from 'some-path'
51-
vi.spyOn(exports, 'getter', 'get')
52-
vi.spyOn(exports, 'setter', 'set')
53-
```
54-
55-
### 模拟模块导出函数 {#mock-an-exported-class-implementation}
45+
### 模拟导出函数 {#mock-an-exported-function}
5646

5747
1. `vi.mock` 的示例:
5848

@@ -81,10 +71,10 @@ vi.spyOn(exports, 'method').mockImplementation(() => {})
8171
```
8272

8373
::: warning
84-
`vi.spyOn` 示例在浏览器模式中无法使用。如需替代方案,请查看 [限制部分](/guide/browser/#spying-on-module-exports)
74+
`vi.spyOn` 示例在浏览器模式中无法使用。如需替代方案,请查看 [限制](/guide/browser/#spying-on-module-exports) 部分
8575
:::
8676

87-
### `vi.mock``.prototype` 的示例:
77+
### 模拟导出类的实现 {#mock-an-exported-class-implementation}
8878

8979
1. 一个使用假 class 的示例:
9080
```ts [example.js]
@@ -112,7 +102,7 @@ vi.spyOn(mod, 'SomeClass').mockImplementation(class FakeClass {
112102
```
113103

114104
::: warning
115-
vi.spyOn 的示例无法在浏览器模式中正常使用。如需替代方案,请查看 [限制部分](/guide/browser/#spying-on-module-exports)
105+
vi.spyOn 的示例无法在浏览器模式中正常使用。如需替代方案,请查看 [限制](/guide/browser/#spying-on-module-exports) 部分
116106
:::
117107

118108
### 监听一个函数是否返回了一个对象 {#spy-on-an-object-returned-from-a-function}

0 commit comments

Comments
 (0)