Skip to content

Commit f5e0b98

Browse files
syi0808sheremet-va
andauthored
feat(vitest): add return type and promisable mockFactory (#6139)
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com>
1 parent 5932a7f commit f5e0b98

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

docs/api/vi.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This section describes the API that you can use when [mocking a module](/guide/m
1717
### vi.mock
1818

1919
- **Type**: `(path: string, factory?: (importOriginal: () => unknown) => unknown) => void`
20-
- **Type**: `<T>(path: Promise<T>, factory?: (importOriginal: () => T) => unknown) => void`
20+
- **Type**: `<T>(path: Promise<T>, factory?: (importOriginal: () => T) => T | Promise<T>) => void`
2121

2222
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 define them inside [`vi.hoisted`](#vi-hoisted) and reference them inside `vi.mock`.
2323

@@ -31,7 +31,9 @@ Vitest will not mock modules that were imported inside a [setup file](/config/#s
3131

3232
If `factory` is defined, all imports will return its result. Vitest calls factory only once and caches results for all subsequent imports until [`vi.unmock`](#vi-unmock) or [`vi.doUnmock`](#vi-dounmock) is called.
3333

34-
Unlike in `jest`, the factory can be asynchronous. You can use [`vi.importActual`](#vi-importactual) or a helper with the factory passed in as the first argument, and get the original module inside. Vitest also supports a module promise instead of a string in `vi.mock` method for better IDE support (when file is moved, path will be updated, `importOriginal` also inherits the type automatically).
34+
Unlike in `jest`, the factory can be asynchronous. You can use [`vi.importActual`](#vi-importactual) or a helper with the factory passed in as the first argument, and get the original module inside.
35+
36+
Vitest also supports a module promise instead of a string in the `vi.mock` and `vi.doMock` methods for better IDE support. When the file is moved, the path will be updated, and `importOriginal` also inherits the type automatically. Using this signature will also enforce factory return type to be compatible with the original module (but every export is optional).
3537

3638
```ts twoslash
3739
// @filename: ./path/to/module.js
@@ -143,7 +145,7 @@ If there is no `__mocks__` folder or a factory provided, Vitest will import the
143145
### vi.doMock
144146

145147
- **Type**: `(path: string, factory?: (importOriginal: () => unknown) => unknown) => void`
146-
- **Type**: `<T>(path: Promise<T>, factory?: (importOriginal: () => T) => unknown) => void`
148+
- **Type**: `<T>(path: Promise<T>, factory?: (importOriginal: () => T) => T | Promise<T>) => void`
147149

148150
The same as [`vi.mock`](#vi-mock), but it's not hoisted to the top of the file, so you can reference variables in the global file scope. The next [dynamic import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) of the module will be mocked.
149151

packages/vitest/src/types/mocker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
type Promisable<T> = T | Promise<T>
2+
13
export type MockFactoryWithHelper<M = unknown> = (
2-
importOriginal: <T extends M>() => Promise<T>
3-
) => any
4+
importOriginal: <T extends M = M>() => Promise<T>
5+
) => Promisable<Partial<M>>
46
export type MockFactory = () => any
57

68
export type MockMap = Map<string, Record<string, string | null | MockFactory>>

test/config/test/workers-option.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getWorkersCountByPercentage } from 'vitest/src/utils/workers.js'
44
import * as testUtils from '../../test-utils'
55

66
vi.mock(import('node:os'), async importOriginal => ({
7+
...(await importOriginal()),
78
default: {
89
...(await importOriginal()).default,
910
availableParallelism: () => 10,

test/core/test/mocking/factory.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ vi.mock('../../src/mocks/log.ts', async () => {
5454
},
5555
}
5656
})
57-
57+
// @ts-expect-error null is not allowed to mocked implementation
5858
vi.mock('../../src/mocks/default.ts', () => null)
5959

6060
describe('mocking with factory', () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy