diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts index 9a9fb9a55f..f7df56a507 100644 --- a/types/jest-when/index.d.ts +++ b/types/jest-when/index.d.ts @@ -1,6 +1,7 @@ -// Type definitions for jest-when 1.1 +// Type definitions for jest-when 2.4 // Project: https://github.com/timkindberg/jest-when#readme // Definitions by: Alden Taylor +// Trung Dang // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.0 @@ -15,6 +16,8 @@ export interface WhenMock extends jest.Mock): WhenMock; mockRejectedValue(value: jest.RejectedValue): WhenMock; mockRejectedValueOnce(value: jest.RejectedValue): WhenMock; + mockImplementation(fn: (...args: Y) => T): WhenMock; + mockImplementationOnce(fn?: (...args: Y) => T): WhenMock; } export type When = (fn: jest.Mock) => WhenMock; diff --git a/types/jest-when/jest-when-tests.ts b/types/jest-when/jest-when-tests.ts index 55744218ce..e37021e983 100644 --- a/types/jest-when/jest-when-tests.ts +++ b/types/jest-when/jest-when-tests.ts @@ -89,4 +89,28 @@ describe('mock-when test', () => { } expect(testPassed).toBeTruthy(); }); + + it('should support for mockImplementation', () => { + const fn = jest.fn(); + const expectValue = { a: 1, b: 2 }; + when(fn).calledWith( + expect.anything() + ).mockImplementation(() => (expectValue)); + + const result = fn('whatever'); + expect(result).toMatchObject(expectValue); + }); + + it('should support for mockImplementationOnce', () => { + const fn = jest.fn(); + const expectValue = { a: 1, b: 2 }; + when(fn).calledWith( + expect.anything() + ).mockImplementationOnce(() => (expectValue)); + + const result = fn('whatever'); + const result2 = fn('whatever'); + expect(result).toMatchObject(expectValue); + expect(result2).toEqual(undefined); + }); });