From 9ede26e3695a610877cf77ad364861f0473cf325 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 22 Jun 2022 13:45:43 +0300 Subject: [PATCH 1/3] feat: rename mock types to match jest's --- packages/vitest/src/integrations/spy.ts | 22 +++++++++++----------- packages/vitest/src/types/index.ts | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/vitest/src/integrations/spy.ts b/packages/vitest/src/integrations/spy.ts index 99191f66c230..63a3ea6dab78 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[] @@ -41,7 +41,7 @@ export interface SpyInstance { getMockName(): string mockName(n: string): this - mock: SpyContext + mock: MockContext mockClear(): this mockReset(): this mockRestore(): void @@ -57,20 +57,20 @@ export interface SpyInstance { mockRejectedValueOnce(obj: any): this } -export interface SpyInstanceFn extends SpyInstance { - (...args: TArgs): TReturns +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 @@ -244,12 +244,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..49c012f44c3c 100644 --- a/packages/vitest/src/types/index.ts +++ b/packages/vitest/src/types/index.ts @@ -14,6 +14,6 @@ export type { MockedFunction, MockedObject, SpyInstance, - SpyInstanceFn, - SpyContext, + Mock, + MockContext, } from '../integrations/spy' From 15ebb2961fc5314f2696c4603bc80e51229d8a5a Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 23 Jun 2022 09:29:27 +0300 Subject: [PATCH 2/3] chore: cleaup --- packages/vitest/src/integrations/spy.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/integrations/spy.ts b/packages/vitest/src/integrations/spy.ts index 63a3ea6dab78..88fc4a24aa3b 100644 --- a/packages/vitest/src/integrations/spy.ts +++ b/packages/vitest/src/integrations/spy.ts @@ -37,8 +37,6 @@ type Classes = { }[keyof T] & (string | symbol) export interface SpyInstance { - (...args: TArgs): TReturns - getMockName(): string mockName(n: string): this mock: MockContext @@ -57,6 +55,8 @@ export interface SpyInstance { mockRejectedValueOnce(obj: any): this } +export interface MockInstance extends SpyInstance {} + export interface Mock extends SpyInstance { new (...args: TArgs): TReturns (...args: TArgs): TReturns @@ -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() From 9feccf089bb7e61b4e496e5fcd13b8c375f8f4a0 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 23 Jun 2022 09:32:30 +0300 Subject: [PATCH 3/3] chore: expose types --- packages/vitest/src/types/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/vitest/src/types/index.ts b/packages/vitest/src/types/index.ts index 49c012f44c3c..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, + MockInstance, Mock, MockContext, + Mocked, + MockedClass, } from '../integrations/spy'