From 2084c7df144ee0c91a1c02ab518230e277cbc26c Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Fri, 4 Nov 2022 16:52:05 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#62781=20[jest]=20a?= =?UTF-8?q?dd=20`jest.Spied*`=20utility=20types=20by=20@mrazauskas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [jest] add `jest.Spied*` utility types * add me to code owners list --- types/jest/index.d.ts | 46 +++++++++++++++++++++++++++++++++------- types/jest/jest-tests.ts | 15 +++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 227ed3c5a5e7b8..d6c02bc10ed55a 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -26,6 +26,7 @@ // Pawel Fajfer // Alexandre Germain // Adam Jones +// Tom Mrazauskas // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // Minimum TypeScript Version: 4.3 @@ -389,18 +390,18 @@ declare namespace jest { : Value extends Func ? SpyInstance, ArgsType> : never; - function spyOn>>( - object: T, - method: M, - ): FunctionProperties>[M] extends Func - ? SpyInstance>[M]>, ArgsType>[M]>> - : never; function spyOn>>( object: T, method: M, ): Required[M] extends new (...args: any[]) => any ? SpyInstance[M]>, ConstructorArgsType[M]>> : never; + function spyOn>>( + object: T, + method: M, + ): FunctionProperties>[M] extends Func + ? SpyInstance>[M]>, ArgsType>[M]>> + : never; /** * Indicates that the module system should never return a mocked version of * the specified module from require() (e.g. that it should always return the real module). @@ -1166,9 +1167,38 @@ declare namespace jest { interface SpyInstance extends MockInstance {} /** - * Represents a function that has been spied on. + * Constructs the type of a spied class. */ - type SpiedFunction any> = SpyInstance, ArgsType>; + type SpiedClass any> = SpyInstance< + InstanceType, + ConstructorParameters + >; + + /** + * Constructs the type of a spied function. + */ + type SpiedFunction any> = SpyInstance, ArgsType>; + + /** + * Constructs the type of a spied getter. + */ + type SpiedGetter = SpyInstance; + + /** + * Constructs the type of a spied setter. + */ + type SpiedSetter = SpyInstance; + + /** + * Constructs the type of a spied class or function. + */ + type Spied any) | ((...args: any) => any)> = T extends abstract new ( + ...args: any + ) => any + ? SpiedClass + : T extends (...args: any) => any + ? SpiedFunction + : never; /** * Wrap a function with mock definitions diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 105f2896ecc5bd..4dc25350cb8952 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -674,6 +674,21 @@ jest.spyOn(spyWithIndexSignatureImpl, 'prop'); // $ExpectType SpyInstance<{ some: string; }, []> jest.spyOn(spyWithIndexSignatureImpl, 'prop', 'get'); +let typedSpy: jest.Spied; +typedSpy = jest.spyOn(spiedTarget, 'returnsVoid'); + +let typedSpy1: jest.SpiedClass; +typedSpy1 = jest.spyOn(globalThis, 'Date'); + +let typedSpy2: jest.SpiedFunction; +typedSpy2 = jest.spyOn(spiedTarget, 'setValue'); + +let typedSpy3: jest.SpiedGetter; +typedSpy3 = jest.spyOn(spiedTarget2, 'value', 'get'); + +let typedSpy4: jest.SpiedSetter; +typedSpy4 = jest.spyOn(spiedTarget2, 'value', 'set'); + // $ExpectType MockedObjectDeep<{}> jest.mocked({}); // $ExpectType MockedObjectDeep<{}>