diff --git a/packages/vitest/src/integrations/spy.ts b/packages/vitest/src/integrations/spy.ts index 99191f66c230..88fc4a24aa3b 100644 --- a/packages/vitest/src/integrations/spy.ts +++ b/packages/vitest/src/integrations/spy.ts @@ -16,7 +16,7 @@ interface MockResultThrow { type MockResult = MockResultReturn | MockResultThrow | MockResultIncomplete -export interface SpyContext { +export interface MockContext { calls: TArgs[] instances: TReturns[] invocationCallOrder: number[] @@ -37,11 +37,9 @@ type Classes = { }[keyof T] & (string | symbol) export interface SpyInstance { - (...args: TArgs): TReturns - getMockName(): string mockName(n: string): this - mock: SpyContext + mock: MockContext mockClear(): this mockReset(): this mockRestore(): void @@ -57,20 +55,22 @@ export interface SpyInstance { mockRejectedValueOnce(obj: any): this } -export interface SpyInstanceFn extends SpyInstance { - (...args: TArgs): TReturns +export interface MockInstance extends SpyInstance {} + +export interface Mock extends SpyInstance { new (...args: TArgs): TReturns + (...args: TArgs): TReturns } export type MaybeMockedConstructor = T extends new ( ...args: Array ) => infer R - ? SpyInstanceFn, R> + ? Mock, R> : T -export type MockedFunction = SpyInstanceFn, ReturnType> & { +export type MockedFunction = Mock, ReturnType> & { [K in keyof T]: T[K]; } -export type MockedFunctionDeep = SpyInstanceFn, ReturnType> & MockedObjectDeep +export type MockedFunctionDeep = Mock, ReturnType> & MockedObjectDeep export type MockedObject = MaybeMockedConstructor & { [K in Methods]: T[K] extends Procedure ? MockedFunction @@ -94,6 +94,26 @@ export type MaybeMocked = T extends Procedure ? MockedObject : T +interface Constructable { + new (...args: any[]): any +} + +export type MockedClass = MockInstance< + InstanceType, + T extends new (...args: infer P) => any ? P : never +> & { + prototype: T extends { prototype: any } ? Mocked : never +} & T + +export type Mocked = { + [P in keyof T]: T[P] extends (...args: infer Args) => infer Returns + ? MockInstance + : T[P] extends Constructable + ? MockedClass + : T[P] +} & +T + export type EnhancedSpy = SpyInstance & SpyImpl export const spies = new Set() @@ -244,12 +264,12 @@ function enhanceSpy( return stub as any } -export function fn(): SpyInstanceFn +export function fn(): Mock export function fn( implementation: (...args: TArgs) => R -): SpyInstanceFn +): Mock export function fn( implementation?: (...args: TArgs) => R, -): SpyInstanceFn { - return enhanceSpy(tinyspy.spyOn({ fn: implementation || (() => {}) }, 'fn')) as unknown as SpyInstanceFn +): Mock { + return enhanceSpy(tinyspy.spyOn({ fn: implementation || (() => {}) }, 'fn')) as unknown as Mock } diff --git a/packages/vitest/src/types/index.ts b/packages/vitest/src/types/index.ts index fc9c8bb812f8..6635ce313c97 100644 --- a/packages/vitest/src/types/index.ts +++ b/packages/vitest/src/types/index.ts @@ -14,6 +14,9 @@ export type { MockedFunction, MockedObject, SpyInstance, - SpyInstanceFn, - SpyContext, + MockInstance, + Mock, + MockContext, + Mocked, + MockedClass, } from '../integrations/spy'