Skip to content

Commit

Permalink
feat(testing): expose all types for util mocked (#2096)
Browse files Browse the repository at this point in the history
Closes #2086
  • Loading branch information
ahnpnl committed Nov 4, 2020
1 parent f47c016 commit b1d072b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/utils/testing.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
type MockableFunction = (...args: any[]) => any
type MethodKeysOf<T> = { [K in keyof T]: T[K] extends MockableFunction ? K : never }[keyof T]
type PropertyKeysOf<T> = { [K in keyof T]: T[K] extends MockableFunction ? never : K }[keyof T]
type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never
type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never
export type MockableFunction = (...args: any[]) => any
export type MethodKeysOf<T> = { [K in keyof T]: T[K] extends MockableFunction ? K : never }[keyof T]
export type PropertyKeysOf<T> = { [K in keyof T]: T[K] extends MockableFunction ? never : K }[keyof T]
export type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never
export type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never

interface MockWithArgs<T extends MockableFunction> extends jest.MockInstance<ReturnType<T>, ArgumentsOf<T>> {
export interface MockWithArgs<T extends MockableFunction> extends jest.MockInstance<ReturnType<T>, ArgumentsOf<T>> {
new (...args: ConstructorArgumentsOf<T>): T
(...args: ArgumentsOf<T>): ReturnType<T>
}

type MaybeMockedConstructor<T> = T extends new (...args: any[]) => infer R
export type MaybeMockedConstructor<T> = T extends new (...args: any[]) => infer R
? jest.MockInstance<R, ConstructorArgumentsOf<T>>
: T
type MockedFunction<T extends MockableFunction> = MockWithArgs<T> & { [K in keyof T]: T[K] }
type MockedFunctionDeep<T extends MockableFunction> = MockWithArgs<T> & MockedObjectDeep<T>
type MockedObject<T> = MaybeMockedConstructor<T> &
export type MockedFunction<T extends MockableFunction> = MockWithArgs<T> & { [K in keyof T]: T[K] }
export type MockedFunctionDeep<T extends MockableFunction> = MockWithArgs<T> & MockedObjectDeep<T>
export type MockedObject<T> = MaybeMockedConstructor<T> &
{ [K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunction<T[K]> : T[K] } &
{ [K in PropertyKeysOf<T>]: T[K] }
type MockedObjectDeep<T> = MaybeMockedConstructor<T> &
export type MockedObjectDeep<T> = MaybeMockedConstructor<T> &
{ [K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunctionDeep<T[K]> : T[K] } &
{ [K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]> }

Expand Down

0 comments on commit b1d072b

Please sign in to comment.