|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
1 | 2 | /** Common type definitions. */ |
2 | 3 | import type { AsymmetricMatcher } from '@vitest/expect' |
3 | 4 | import type { MockedClass, MockedFunction } from 'vitest' |
4 | 5 |
|
5 | 6 | /** Any function. */ |
6 | | -export type AnyFunction = (...args: never[]) => unknown |
| 7 | +export type AnyFunction = (...args: any[]) => any |
7 | 8 |
|
8 | 9 | /** Any constructor. */ |
9 | | -export type AnyConstructor = new (...args: never[]) => unknown |
| 10 | +export type AnyConstructor = new (...args: any[]) => any |
10 | 11 |
|
11 | | -/** Any callable, for use in `extends` */ |
12 | | -export type AnyCallable = AnyFunction | AnyConstructor |
| 12 | +/** |
| 13 | + * Minimally typed version of Vitest's `MockInstance`. |
| 14 | + * |
| 15 | + * Ensures backwards compatibility with vitest@<=1 |
| 16 | + */ |
| 17 | +export interface MockInstance<TFunc extends AnyFunction = AnyFunction> { |
| 18 | + getMockName(): string |
| 19 | + getMockImplementation(): TFunc | undefined |
| 20 | + mockImplementation: (impl: TFunc) => this |
| 21 | + mock: MockContext<TFunc> |
| 22 | +} |
| 23 | + |
| 24 | +export interface MockContext<TFunc extends AnyFunction> { |
| 25 | + calls: Parameters<TFunc>[] |
| 26 | +} |
| 27 | + |
| 28 | +/** A function, constructor, or `vi.spyOn` return that's been mocked. */ |
| 29 | +export type MockSource = AnyFunction | AnyConstructor | MockInstance |
13 | 30 |
|
14 | 31 | /** Extract parameters from either a function or constructor. */ |
15 | | -export type ParametersOf<TFunc extends AnyCallable> = TFunc extends new ( |
| 32 | +export type ParametersOf<TMock extends MockSource> = TMock extends new ( |
16 | 33 | ...args: infer P |
17 | 34 | ) => unknown |
18 | 35 | ? P |
19 | | - : TFunc extends (...args: infer P) => unknown |
| 36 | + : TMock extends (...args: infer P) => unknown |
20 | 37 | ? P |
21 | | - : never |
| 38 | + : TMock extends MockInstance<(...args: infer P) => unknown> |
| 39 | + ? P |
| 40 | + : never |
22 | 41 |
|
23 | 42 | /** Extract return type from either a function or constructor */ |
24 | | -export type ReturnTypeOf<TFunc extends AnyCallable> = TFunc extends new ( |
| 43 | +export type ReturnTypeOf<TMock extends MockSource> = TMock extends new ( |
25 | 44 | ...args: never[] |
26 | 45 | ) => infer R |
27 | 46 | ? R |
28 | | - : TFunc extends (...args: never[]) => infer R |
| 47 | + : TMock extends (...args: any[]) => infer R |
29 | 48 | ? R |
30 | | - : never |
| 49 | + : TMock extends MockInstance<(...args: never[]) => infer R> |
| 50 | + ? R |
| 51 | + : never |
31 | 52 |
|
32 | | -export type AsFunction<TFunc extends AnyCallable> = ( |
33 | | - ...args: ParametersOf<TFunc> |
34 | | -) => ReturnTypeOf<TFunc> |
| 53 | +/** Convert a function or constructor type into a function type. */ |
| 54 | +export type AsFunction<TMock extends MockSource> = ( |
| 55 | + ...args: ParametersOf<TMock> |
| 56 | +) => ReturnTypeOf<TMock> |
35 | 57 |
|
36 | 58 | /** Accept a value or an AsymmetricMatcher in an arguments array */ |
37 | 59 | export type WithMatchers<T extends unknown[]> = { |
38 | 60 | [K in keyof T]: AsymmetricMatcher<unknown> | T[K] |
39 | 61 | } |
40 | 62 |
|
41 | | -export type Mock<TFunc extends AnyCallable> = TFunc extends AnyFunction |
42 | | - ? MockedFunction<TFunc> |
43 | | - : TFunc extends AnyConstructor |
44 | | - ? MockedClass<TFunc> |
45 | | - : never |
| 63 | +/** A mocked function or constructor. */ |
| 64 | +export type Mock<TMock extends MockSource = MockSource> = |
| 65 | + TMock extends MockInstance<infer TFunc> |
| 66 | + ? MockedFunction<TFunc> |
| 67 | + : TMock extends AnyFunction |
| 68 | + ? MockedFunction<TMock> |
| 69 | + : TMock extends AnyConstructor |
| 70 | + ? MockedClass<TMock> |
| 71 | + : never |
0 commit comments