From 3236add57d42a007dd994bf2fe5fd56abff06bfd Mon Sep 17 00:00:00 2001 From: Antoine Brault Date: Mon, 10 Dec 2018 10:28:16 -0500 Subject: [PATCH] rollback default values --- types/jest-when/index.d.ts | 2 +- types/jest/index.d.ts | 6 +++--- types/jest/jest-tests.ts | 13 ++++++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts index fbd938b5ff..c76fdd1a9b 100644 --- a/types/jest-when/index.d.ts +++ b/types/jest-when/index.d.ts @@ -9,7 +9,7 @@ export type PartialMockInstance = Pick, 'mockReturnValue' | 'mockReturnValueOnce' | 'mockResolvedValue' | 'mockResolvedValueOnce' | 'mockRejectedValue' | 'mockRejectedValueOnce'>; -export interface When { +export interface When { (fn: jest.Mock): When; calledWith(...matchers: Y): PartialMockInstance; expectCalledWith(...matchers: Y): PartialMockInstance; diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 02b6cb5cba..b7486db0ac 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -121,7 +121,7 @@ declare namespace jest { /** * Returns whether the given function is a mock function. */ - function isMockFunction(fn: any): fn is Mock; + function isMockFunction(fn: any): fn is Mock; /** * Mocks a module with an auto-mocked version when it is being required. */ @@ -763,12 +763,12 @@ declare namespace jest { new (...args: any[]): any; } - interface Mock extends Function, MockInstance { + interface Mock extends Function, MockInstance { new (...args: Y): T; (...args: Y): T; } - interface SpyInstance extends MockInstance {} + interface SpyInstance extends MockInstance {} /** * Wrap module with mock definitions diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index b616daaeba..9054bddfa4 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -267,15 +267,20 @@ const mock5 = jest.fn((...args: string[]) => args.join("")); const mock6 = jest.fn((arg: {}) => arg); // $ExpectType Mock const mock7 = jest.fn((arg: number) => arg); +// $ExpectType Mock +const mock8: jest.Mock = jest.fn((arg: number) => arg); // $ExpectError mock7('abc'); // $ExpectError mock7.mockImplementation((arg: string) => 1); +// compiles because mock8 is declared as jest.Mock<{}, any> +mock8('abc'); +mock8.mockImplementation((arg: string) => 1); -const mock8 = jest.fn((a: number, _b: string, _c: {}, _iReallyDontCare: [], _makeItStop: boolean) => Promise.resolve(_makeItStop)); +const mock9 = jest.fn((a: number, _b: string, _c: {}, _iReallyDontCare: [], _makeItStop: boolean) => Promise.resolve(_makeItStop)); // mockImplementation not required to declare all arguments -mock8.mockImplementation((a: number) => Promise.resolve(a === 0)); +mock9.mockImplementation((a: number) => Promise.resolve(a === 0)); const genMockModule1: {} = jest.genMockFromModule("moduleName"); const genMockModule2: { a: "b" } = jest.genMockFromModule<{ a: "b" }>("moduleName"); @@ -342,8 +347,10 @@ const spy3Mock = spy3 .mockRejectedValue("value") .mockRejectedValueOnce("value"); -let spy4; +let spy4: jest.SpyInstance; spy4 = jest.spyOn(spiedTarget, "returnsString"); +// compiles because spy4 is declared as jest.SpyInstance<{}, any> +spy4.mockImplementation(() => 1); spy4.mockRestore(); // $ExpectType SpyInstance