From cf300431381ba6183464d59447e1fce2abecfc36 Mon Sep 17 00:00:00 2001 From: antoinebrault Date: Mon, 18 Feb 2019 20:39:22 -0500 Subject: [PATCH 1/2] [jest] support optional methods/properties from interfaces in spyOn --- types/jest/index.d.ts | 7 ++++--- types/jest/jest-tests.ts | 41 ++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index f0382d2cb5..437c524e94 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -220,9 +220,10 @@ declare namespace jest { * spy.mockRestore(); * }); */ - function spyOn>(object: T, method: M, accessType: 'get'): SpyInstance; - function spyOn>(object: T, method: M, accessType: 'set'): SpyInstance; - function spyOn>(object: T, method: M): T[M] extends (...args: any[]) => any ? SpyInstance, ArgsType> : never; + function spyOn>>(object: T, method: M, accessType: 'get'): SpyInstance[M], []>; + function spyOn>>(object: T, method: M, accessType: 'set'): SpyInstance[M]]>; + function spyOn>>(object: T, method: M): Required[M] extends (...args: any[]) => any ? + 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). diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 41f011c2f2..93ba9980f6 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -349,21 +349,15 @@ const mockContextVoid = jest.fn().mock; const mockContextString = jest.fn(() => "").mock; jest.fn().mockClear(); - jest.fn().mockReset(); - jest.fn().mockRestore(); +jest.fn().mockImplementation((test: number) => test); +jest.fn().mockResolvedValue(1); -const spiedTarget = { - returnsVoid(): void { }, - setValue(value: string): void { - this.value = value; - }, - returnsString(): string { - return ""; - } -}; - +interface SpyInterface { + prop?: number; + method?: (arg1: boolean) => void; +} class SpiedTargetClass { private _value = 3; private _value2 = ''; @@ -380,6 +374,15 @@ class SpiedTargetClass { this._value2 = value2; } } +const spiedTarget = { + returnsVoid(): void { }, + setValue(value: string): void { + this.value = value; + }, + returnsString(): string { + return ""; + } +}; const spiedTarget2 = new SpiedTargetClass(); // $ExpectError @@ -425,11 +428,17 @@ const spy5 = jest.spyOn(spiedTarget2, "value", "get"); spy5.mockReturnValue('5'); // $ExpectType SpyInstance -const spy6 = jest.spyOn(spiedTarget2, "value", "set"); +jest.spyOn(spiedTarget2, "value", "set"); -// should compile -jest.fn().mockImplementation((test: number) => test); -jest.fn().mockResolvedValue(1); +let spyInterfaceImpl: SpyInterface = {}; +// $ExpectError +jest.spyOn(spyInterfaceImpl, "method", "get"); +// $ExpectError +jest.spyOn(spyInterfaceImpl, "prop"); +// $ExpectType SpyInstance +jest.spyOn(spyInterfaceImpl, "prop", "get"); +// $ExpectType SpyInstance +jest.spyOn(spyInterfaceImpl, "method"); interface Type1 { a: number; } interface Type2 { b: number; } From b983812c4dc891c0fc7e9794c454b28e43978d1d Mon Sep 17 00:00:00 2001 From: antoinebrault Date: Tue, 19 Feb 2019 19:46:29 -0500 Subject: [PATCH 2/2] cleanup --- types/jest/jest-tests.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 693c16d44f..578ce0132e 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -358,6 +358,15 @@ interface SpyInterface { prop?: number; method?: (arg1: boolean) => void; } +const spiedTarget = { + returnsVoid(): void { }, + setValue(value: string): void { + this.value = value; + }, + returnsString(): string { + return ""; + } +}; class SpiedTargetClass { private _value = 3; private _value2 = ''; @@ -374,15 +383,7 @@ class SpiedTargetClass { this._value2 = value2; } } -const spiedTarget = { - returnsVoid(): void { }, - setValue(value: string): void { - this.value = value; - }, - returnsString(): string { - return ""; - } -}; + const spiedTarget2 = new SpiedTargetClass(); // $ExpectError