From f08b42b765cace3295309f27521aa58e20720253 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 18 Oct 2022 21:17:53 +0300 Subject: [PATCH 1/2] [jest] add `jest.Spied*` utility types --- types/jest/index.d.ts | 45 +++++++++++++++++++++++++++++++++------- types/jest/jest-tests.ts | 15 ++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index b32fc3f83dac35..99238c918ae8d7 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -389,18 +389,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 +1166,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 438e830ec08609..5f3560362d650a 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<{}> From 48c7862b140c4c2c5ae433506f0ff9bd28842004 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Tue, 18 Oct 2022 21:23:42 +0300 Subject: [PATCH 2/2] add me to code owners list --- types/jest/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 99238c918ae8d7..81ce41f1624ce4 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