Skip to content

Commit ce5be34

Browse files
committed
docs(en): merging all conflicts
2 parents 4cfd11d + 9424d22 commit ce5be34

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

api/vi.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,15 +937,49 @@ callFunctionWithSideEffect()
937937
+ const { value } = await import('./some/module.js')
938938
```
939939

940+
<<<<<<< HEAD
940941
运行 `vitest` 时,可以使用 `vi.hoisted` 方法自动完成此操作。
942+
=======
943+
When running `vitest`, you can do this automatically by using `vi.hoisted` method. Under the hood, Vitest will convert static imports into dynamic ones with preserved live-bindings.
944+
>>>>>>> 9424d225d38946632ed6ee68933f140b826cde9e
941945
942946
```diff
943947
- callFunctionWithSideEffect()
944948
import { value } from './some/module.js'
945949
+ vi.hoisted(() => callFunctionWithSideEffect())
946950
```
947951

952+
<<<<<<< HEAD
948953
该方法返回从工厂返回的值。 如果我们需要轻松访问本地定义的变量,可以在我们的 `vi.mock` 工厂中使用该值:
954+
=======
955+
::: warning IMPORTS ARE NOT AVAILABLE
956+
Running code before the imports means that you cannot access imported variables because they are not defined yet:
957+
958+
```ts
959+
import { value } from './some/module.js'
960+
961+
vi.hoisted(() => { value }) // throws an error // [!code warning]
962+
```
963+
964+
This code will produce an error:
965+
966+
```
967+
Cannot access '__vi_import_0__' before initialization
968+
```
969+
970+
If you need to access a variable from another module inside of `vi.hoisted`, use dynamic import:
971+
972+
```ts
973+
await vi.hoisted(async () => {
974+
const { value } = await import('./some/module.js')
975+
})
976+
```
977+
978+
However, it is discourage to import anything inside of `vi.hoisted` because imports are already hoisted - if you need to execute something before the tests are running, just execute it in the imported module itself.
979+
:::
980+
981+
This method returns the value that was returned from the factory. You can use that value in your `vi.mock` factories if you need easy access to locally defined variables:
982+
>>>>>>> 9424d225d38946632ed6ee68933f140b826cde9e
949983
950984
```ts
951985
import { expect, vi } from 'vitest'
@@ -966,7 +1000,7 @@ expect(originalMethod()).toBe(100)
9661000
请注意,即使我们的环境不支持顶级等待,也可以异步调用此方法:
9671001

9681002
```ts
969-
const promised = await vi.hoisted(async () => {
1003+
const json = await vi.hoisted(async () => {
9701004
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
9711005
return response.json()
9721006
})

guide/mocking.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,11 @@ afterAll(() => server.close())
479479
afterEach(() => server.resetHandlers())
480480
```
481481

482+
<<<<<<< HEAD
482483
> 使用 `onUnhandleRequest: 'error'` 配置服务器可以确保即使某个请求没有相应的请求处理程序,也会抛出错误。
484+
=======
485+
> Configuring the server with `onUnhandledRequest: 'error'` ensures that an error is thrown whenever there is a request that does not have a corresponding request handler.
486+
>>>>>>> 9424d225d38946632ed6ee68933f140b826cde9e
483487
484488
### 了解更多
485489

0 commit comments

Comments
 (0)