You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/vi.md
+69-2
Original file line number
Diff line number
Diff line change
@@ -114,11 +114,55 @@ import { vi } from 'vitest'
114
114
115
115
When using `vi.useFakeTimers`, `Date.now` calls are mocked. If you need to get real time in milliseconds, you can call this function.
116
116
117
+
## vi.hoisted
118
+
119
+
-**Type**: `<T>(factory: () => T) => T`
120
+
-**Version**: Since Vitest 0.31.0
121
+
122
+
All static `import` statements in ES modules are hoisted to top of the file, so any code that is define before the imports will actually be executed after imports are evaluated.
123
+
124
+
Hovewer it can be useful to invoke some side effect like mocking dates before importing a module.
125
+
126
+
To bypass this limitation, you can rewrite static imports into dynamic ones like this:
127
+
128
+
```diff
129
+
callFunctionWithSideEffect()
130
+
- import { value } from './some/module.ts'
131
+
+ const { value } = await import('./some/module.ts')
132
+
```
133
+
134
+
When running `vitest`, you can do this automatically by using `vi.hoisted` method.
135
+
136
+
```diff
137
+
- callFunctionWithSideEffect()
138
+
import { value } from './some/module.ts'
139
+
+ vi.hoisted(() => callFunctionWithSideEffect())
140
+
```
141
+
142
+
This method returns the value that was returned from the factory. You can use that value in your `vi.mock` factories if you need an easy access to locally defined variables:
Substitutes all imported modules from provided `path` with another module. You can use configured Vite aliases inside a path. The call to `vi.mock` is hoisted, so it doesn't matter where you call it. It will always be executed before all imports.
165
+
Substitutes all imported modules from provided `path` with another module. You can use configured Vite aliases inside a path. The call to `vi.mock` is hoisted, so it doesn't matter where you call it. It will always be executed before all imports. If you need to reference some variables outside of its scope, you can defined them inside [`vi.hoisted`](/api/vi#vi-hoisted) and reference inside `vi.mock`.
122
166
123
167
::: warning
124
168
`vi.mock` works only for modules that were imported with the `import` keyword. It doesn't work with `require`.
@@ -151,6 +195,29 @@ import { vi } from 'vitest'
151
195
This also means that you cannot use any variables inside the factory that are defined outside the factory.
152
196
153
197
If you need to use variables inside the factory, try [`vi.doMock`](#vi-domock). It works the same way but isn't hoisted. Beware that it only mocks subsequent imports.
198
+
199
+
You can also reference variables defined by `vi.hoisted` method if it was declared before `vi.mock`:
200
+
201
+
```ts
202
+
import { namedExport } from'./path/to/module.js'
203
+
204
+
const mocks =vi.hoisted(() => {
205
+
return {
206
+
namedExport: vi.fn(),
207
+
}
208
+
})
209
+
210
+
vi.mock('./path/to/module.js', () => {
211
+
return {
212
+
namedExport: mocks.namedExport,
213
+
}
214
+
})
215
+
216
+
vi.mocked(namedExport).mockReturnValue(100)
217
+
218
+
expect(namedExport()).toBe(100)
219
+
expect(namedExport).toBe(mocks.namedExport)
220
+
```
154
221
:::
155
222
156
223
::: warning
@@ -199,7 +266,7 @@ import { vi } from 'vitest'
199
266
```
200
267
201
268
::: warning
202
-
Beware that if you don't call `vi.mock`, modules **are not** mocked automatically.
269
+
Beware that if you don't call `vi.mock`, modules **are not** mocked automatically. To replicate Jest's automocking behaviour, you can call `vi.mock` for each required module inside [`setupFiles`](/config/#setupfiles).
203
270
:::
204
271
205
272
If there is no `__mocks__` folder or a factory provided, Vitest will import the original module and auto-mock all its exports. For the rules applied, see [algorithm](/guide/mocking#automocking-algorithm).
This is an advanced API for library authors. If you just need to run tests in a browser, use the [browser](/config/#browser) option.
1036
1036
:::
1037
1037
1038
+
#### browser.slowHijackESM
1039
+
1040
+
-**Type:**`boolean`
1041
+
-**Default:**`true`
1042
+
-**Version:** Since Vitest 0.31.0
1043
+
1044
+
When running tests in Node.js Vitest can use its own module resolution to easily mock modules with `vi.mock` syntax. However it's not so easy to replicate ES module resolution in browser, so we need to transform your source files before browser can consume it.
1045
+
1046
+
This option has no effect on tests running inside Node.js.
1047
+
1048
+
This options is enabled by default when running in the browser. If you don't rely on spying on ES modules with `vi.spyOn` and don't use `vi.mock`, you can disable this to get a slight boost to performance.
1049
+
1050
+
1038
1051
### clearMocks
1039
1052
1040
1053
-**Type:**`boolean`
@@ -1358,7 +1371,7 @@ The number of milliseconds after which a test is considered slow and reported as
0 commit comments