diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts index f7df56a507..de12be417e 100644 --- a/types/jest-when/index.d.ts +++ b/types/jest-when/index.d.ts @@ -2,24 +2,25 @@ // Project: https://github.com/timkindberg/jest-when#readme // Definitions by: Alden Taylor // Trung Dang +// Gregor Stamać // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.0 /// -export interface WhenMock extends jest.Mock { - calledWith(...matchers: Y): WhenMock; - expectCalledWith(...matchers: Y): WhenMock; - mockReturnValue(value: T): WhenMock; - mockReturnValueOnce(value: T): WhenMock; - mockResolvedValue(value: jest.ResolvedValue): WhenMock; - mockResolvedValueOnce(value: jest.ResolvedValue): WhenMock; - mockRejectedValue(value: jest.RejectedValue): WhenMock; - mockRejectedValueOnce(value: jest.RejectedValue): WhenMock; - mockImplementation(fn: (...args: Y) => T): WhenMock; - mockImplementationOnce(fn?: (...args: Y) => T): WhenMock; +export interface WhenMock extends jest.MockInstance { + calledWith(...matchers: Y): this; + expectCalledWith(...matchers: Y): this; + mockReturnValue(value: T): this; + mockReturnValueOnce(value: T): this; + mockResolvedValue(value: jest.ResolvedValue): this; + mockResolvedValueOnce(value: jest.ResolvedValue): this; + mockRejectedValue(value: jest.RejectedValue): this; + mockRejectedValueOnce(value: jest.RejectedValue): this; + mockImplementation(fn: (...args: Y) => T): this; + mockImplementationOnce(fn?: (...args: Y) => T): this; } -export type When = (fn: jest.Mock) => WhenMock; +export type When = (fn: jest.MockInstance) => WhenMock; export const when: When; diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 61aca7b397..7d1a7d70ca 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -19,6 +19,7 @@ // Andy // Antoine Brault // Jeroen Claassens +// Gregor Stamać // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.0 @@ -904,7 +905,7 @@ declare namespace jest { * * Note: `jest.fn(implementation)` is a shorthand for `jest.fn().mockImplementation(implementation)`. */ - mockImplementation(fn?: (...args: Y) => T): Mock; + mockImplementation(fn?: (...args: Y) => T): this; /** * Accepts a function that will be used as an implementation of the mock for one call to the mocked function. * Can be chained so that multiple function calls produce different results. @@ -920,9 +921,9 @@ declare namespace jest { * * myMockFn((err, val) => console.log(val)); // false */ - mockImplementationOnce(fn: (...args: Y) => T): Mock; + mockImplementationOnce(fn: (...args: Y) => T): this; /** Sets the name of the mock`. */ - mockName(name: string): Mock; + mockName(name: string): this; /** * Just a simple sugar function for: * @@ -932,7 +933,7 @@ declare namespace jest { * return this; * }); */ - mockReturnThis(): Mock; + mockReturnThis(): this; /** * Accepts a value that will be returned whenever the mock function is called. * @@ -944,7 +945,7 @@ declare namespace jest { * mock.mockReturnValue(43); * mock(); // 43 */ - mockReturnValue(value: T): Mock; + mockReturnValue(value: T): this; /** * Accepts a value that will be returned for one call to the mock function. Can be chained so that * successive calls to the mock function return different values. When there are no more @@ -961,11 +962,11 @@ declare namespace jest { * console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn()); * */ - mockReturnValueOnce(value: T): Mock; + mockReturnValueOnce(value: T): this; /** * Simple sugar function for: `jest.fn().mockImplementation(() => Promise.resolve(value));` */ - mockResolvedValue(value: ResolvedValue): Mock; + mockResolvedValue(value: ResolvedValue): this; /** * Simple sugar function for: `jest.fn().mockImplementationOnce(() => Promise.resolve(value));` * @@ -985,7 +986,7 @@ declare namespace jest { * }); * */ - mockResolvedValueOnce(value: ResolvedValue): Mock; + mockResolvedValueOnce(value: ResolvedValue): this; /** * Simple sugar function for: `jest.fn().mockImplementation(() => Promise.reject(value));` * @@ -997,7 +998,7 @@ declare namespace jest { * await asyncMock(); // throws "Async error" * }); */ - mockRejectedValue(value: RejectedValue): Mock; + mockRejectedValue(value: RejectedValue): this; /** * Simple sugar function for: `jest.fn().mockImplementationOnce(() => Promise.reject(value));` @@ -1015,7 +1016,7 @@ declare namespace jest { * }); * */ - mockRejectedValueOnce(value: RejectedValue): Mock; + mockRejectedValueOnce(value: RejectedValue): this; } /** diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 04d25ce5e9..b2c271ba2d 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -463,22 +463,22 @@ class TestMocked { const mocked: jest.Mocked = new TestMocked() as any; mocked.test1.mockImplementation(() => Promise.resolve({ a: 1 })); mocked.test1.mockReturnValue(Promise.resolve({ a: 1 })); -// $ExpectType Mock, [Type1]> +// $ExpectType MockInstance, [Type1]> & ((x: Type1) => Promise) mocked.test1.mockResolvedValue({ a: 1 }); mocked.test1.mockResolvedValueOnce({ a: 1 }); -// $ExpectType Mock, [Type1]> +// $ExpectType MockInstance, [Type1]> & ((x: Type1) => Promise) mocked.test1.mockResolvedValue(Promise.resolve({ a: 1 })); mocked.test1.mockResolvedValueOnce(Promise.resolve({ a: 1 })); -// $ExpectType Mock, [Promise]> +// $ExpectType MockInstance, [Promise]> & ((x: Promise) => Promise) mocked.test2.mockResolvedValue({ a: 1 }); mocked.test2.mockResolvedValueOnce({ a: 1 }); -// $ExpectType Mock, [Promise]> +// $ExpectType MockInstance, [Promise]> & ((x: Promise) => Promise) mocked.test2.mockResolvedValue(Promise.resolve({ a: 1 })); mocked.test2.mockResolvedValueOnce(Promise.resolve({ a: 1 })); -// $ExpectType Mock, [Promise]> +// $ExpectType MockInstance, [Promise]> & ((x: Promise) => Promise) mocked.test3.mockResolvedValue({ b: 1 }); mocked.test3.mockResolvedValueOnce({ b: 1 }); -// $ExpectType Mock, [Promise]> +// $ExpectType MockInstance, [Promise]> & ((x: Promise) => Promise) mocked.test3.mockResolvedValue(Promise.resolve({ b: 1 })); mocked.test3.mockResolvedValueOnce(Promise.resolve({ b: 1 })); mocked.test3.mockRejectedValue(new Error());