Skip to content

Commit

Permalink
chore: cleaup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 23, 2022
1 parent 9ede26e commit 15ebb29
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions packages/vitest/src/integrations/spy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ type Classes<T> = {
}[keyof T] & (string | symbol)

export interface SpyInstance<TArgs extends any[] = any[], TReturns = any> {
(...args: TArgs): TReturns

getMockName(): string
mockName(n: string): this
mock: MockContext<TArgs, TReturns>
Expand All @@ -57,6 +55,8 @@ export interface SpyInstance<TArgs extends any[] = any[], TReturns = any> {
mockRejectedValueOnce(obj: any): this
}

export interface MockInstance<A extends any[] = any[], R = any> extends SpyInstance<A, R> {}

export interface Mock<TArgs extends any[] = any, TReturns = any> extends SpyInstance<TArgs, TReturns> {
new (...args: TArgs): TReturns
(...args: TArgs): TReturns
Expand Down Expand Up @@ -94,6 +94,26 @@ export type MaybeMocked<T> = T extends Procedure
? MockedObject<T>
: T

interface Constructable {
new (...args: any[]): any
}

export type MockedClass<T extends Constructable> = MockInstance<
InstanceType<T>,
T extends new (...args: infer P) => any ? P : never
> & {
prototype: T extends { prototype: any } ? Mocked<T['prototype']> : never
} & T

export type Mocked<T> = {
[P in keyof T]: T[P] extends (...args: infer Args) => infer Returns
? MockInstance<Args, Returns>
: T[P] extends Constructable
? MockedClass<T[P]>
: T[P]
} &
T

export type EnhancedSpy<TArgs extends any[] = any[], TReturns = any> = SpyInstance<TArgs, TReturns> & SpyImpl<TArgs, TReturns>

export const spies = new Set<SpyInstance>()
Expand Down

0 comments on commit 15ebb29

Please sign in to comment.