From b1d072b52b9a7665b3a6914b0895f84f6ee3f8c0 Mon Sep 17 00:00:00 2001 From: Ahn Date: Wed, 4 Nov 2020 07:11:43 +0100 Subject: [PATCH] feat(testing): expose all types for util `mocked` (#2096) Closes #2086 --- src/utils/testing.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/utils/testing.ts b/src/utils/testing.ts index 63c531bef6..4f1fb7e4d2 100644 --- a/src/utils/testing.ts +++ b/src/utils/testing.ts @@ -1,23 +1,23 @@ -type MockableFunction = (...args: any[]) => any -type MethodKeysOf = { [K in keyof T]: T[K] extends MockableFunction ? K : never }[keyof T] -type PropertyKeysOf = { [K in keyof T]: T[K] extends MockableFunction ? never : K }[keyof T] -type ArgumentsOf = T extends (...args: infer A) => any ? A : never -type ConstructorArgumentsOf = T extends new (...args: infer A) => any ? A : never +export type MockableFunction = (...args: any[]) => any +export type MethodKeysOf = { [K in keyof T]: T[K] extends MockableFunction ? K : never }[keyof T] +export type PropertyKeysOf = { [K in keyof T]: T[K] extends MockableFunction ? never : K }[keyof T] +export type ArgumentsOf = T extends (...args: infer A) => any ? A : never +export type ConstructorArgumentsOf = T extends new (...args: infer A) => any ? A : never -interface MockWithArgs extends jest.MockInstance, ArgumentsOf> { +export interface MockWithArgs extends jest.MockInstance, ArgumentsOf> { new (...args: ConstructorArgumentsOf): T (...args: ArgumentsOf): ReturnType } -type MaybeMockedConstructor = T extends new (...args: any[]) => infer R +export type MaybeMockedConstructor = T extends new (...args: any[]) => infer R ? jest.MockInstance> : T -type MockedFunction = MockWithArgs & { [K in keyof T]: T[K] } -type MockedFunctionDeep = MockWithArgs & MockedObjectDeep -type MockedObject = MaybeMockedConstructor & +export type MockedFunction = MockWithArgs & { [K in keyof T]: T[K] } +export type MockedFunctionDeep = MockWithArgs & MockedObjectDeep +export type MockedObject = MaybeMockedConstructor & { [K in MethodKeysOf]: T[K] extends MockableFunction ? MockedFunction : T[K] } & { [K in PropertyKeysOf]: T[K] } -type MockedObjectDeep = MaybeMockedConstructor & +export type MockedObjectDeep = MaybeMockedConstructor & { [K in MethodKeysOf]: T[K] extends MockableFunction ? MockedFunctionDeep : T[K] } & { [K in PropertyKeysOf]: MaybeMockedDeep }