From 1978489d1fb5993a24a07de5d033b97d7c8f8de2 Mon Sep 17 00:00:00 2001 From: Valentin STERN Date: Thu, 5 Sep 2019 00:18:27 +0200 Subject: [PATCH] [@types/jest-when] add missing functions resetAllWhenMocks and verifyAllWhenMocksCalled (#38129) --- types/jest-when/index.d.ts | 8 +- types/jest-when/jest-when-tests.ts | 260 ++++++++++++++++++----------- 2 files changed, 169 insertions(+), 99 deletions(-) diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts index de12be417e..bfb0576c2d 100644 --- a/types/jest-when/index.d.ts +++ b/types/jest-when/index.d.ts @@ -1,14 +1,16 @@ -// Type definitions for jest-when 2.4 +// Type definitions for jest-when 2.7 // Project: https://github.com/timkindberg/jest-when#readme // Definitions by: Alden Taylor // Trung Dang // Gregor Stamać +// Valentin Stern // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.0 /// -export interface WhenMock extends jest.MockInstance { +export interface WhenMock + extends jest.MockInstance { calledWith(...matchers: Y): this; expectCalledWith(...matchers: Y): this; mockReturnValue(value: T): this; @@ -24,3 +26,5 @@ export interface WhenMock extends jest.MockInsta export type When = (fn: jest.MockInstance) => WhenMock; export const when: When; +export function resetAllWhenMocks(): void; +export function verifyAllWhenMocksCalled(): void; diff --git a/types/jest-when/jest-when-tests.ts b/types/jest-when/jest-when-tests.ts index e37021e983..c36efde258 100644 --- a/types/jest-when/jest-when-tests.ts +++ b/types/jest-when/jest-when-tests.ts @@ -1,116 +1,182 @@ -import { when } from 'jest-when'; +import { when, resetAllWhenMocks, verifyAllWhenMocksCalled } from 'jest-when'; describe('mock-when test', () => { - it('basic usage:', () => { - const fn = jest.fn(); - when(fn).calledWith(1).mockReturnValue('yay!'); + it('basic usage:', () => { + const fn = jest.fn(); + when(fn) + .calledWith(1) + .mockReturnValue('yay!'); - const result = fn(1); - expect(result).toEqual('yay!'); - }); + const result = fn(1); + expect(result).toEqual('yay!'); + }); - it('Supports multiple args:', () => { - const fn = jest.fn(); - when(fn).calledWith(1, true, 'foo').mockReturnValue('yay!'); + it('Supports multiple args:', () => { + const fn = jest.fn(); + when(fn) + .calledWith(1, true, 'foo') + .mockReturnValue('yay!'); - const result = fn(1, true, 'foo'); - expect(result).toEqual('yay!'); - }); + const result = fn(1, true, 'foo'); + expect(result).toEqual('yay!'); + }); - it('Supports jest matchers:', () => { - const fn = jest.fn(); - when(fn).calledWith( - expect.anything(), - expect.any(Number), - expect.arrayContaining([false]) - ).mockReturnValue('yay!'); + it('Supports jest matchers:', () => { + const fn = jest.fn(); + when(fn) + .calledWith( + expect.anything(), + expect.any(Number), + expect.arrayContaining([false]), + ) + .mockReturnValue('yay!'); - const result = fn('whatever', 100, [true, false]); - expect(result).toEqual('yay!'); - }); + const result = fn('whatever', 100, [true, false]); + expect(result).toEqual('yay!'); + }); - it('Supports compound declarations:', () => { - const fn = jest.fn(); - when(fn).calledWith(1).mockReturnValueOnce('no').mockReturnValue('yes'); - when(fn).calledWith(2).mockReturnValue('way?'); - when(fn).calledWith(3).mockResolvedValueOnce('no'); - when(fn).calledWith(3).mockResolvedValue('yes'); - when(fn).calledWith(4).mockRejectedValueOnce('no'); - when(fn).calledWith(4).mockRejectedValue('yes'); + it('Supports compound declarations:', () => { + const fn = jest.fn(); + when(fn) + .calledWith(1) + .mockReturnValueOnce('no') + .mockReturnValue('yes'); + when(fn) + .calledWith(2) + .mockReturnValue('way?'); + when(fn) + .calledWith(3) + .mockResolvedValueOnce('no'); + when(fn) + .calledWith(3) + .mockResolvedValue('yes'); + when(fn) + .calledWith(4) + .mockRejectedValueOnce('no'); + when(fn) + .calledWith(4) + .mockRejectedValue('yes'); - expect(fn(1)).toEqual('no'); - expect(fn(1)).toEqual('yes'); - expect(fn(2)).toEqual('way?'); - expect(fn(3)).resolves.toEqual('no'); - expect(fn(3)).resolves.toEqual('yes'); - expect(fn(4)).rejects.toEqual('no'); - expect(fn(4)).rejects.toEqual('yes'); - }); + expect(fn(1)).toEqual('no'); + expect(fn(1)).toEqual('yes'); + expect(fn(2)).toEqual('way?'); + expect(fn(3)).resolves.toEqual('no'); + expect(fn(3)).resolves.toEqual('yes'); + expect(fn(4)).rejects.toEqual('no'); + expect(fn(4)).rejects.toEqual('yes'); + }); - it('Supports chained calls:', () => { - const fn = jest.fn(); - when(fn) - .calledWith(1).mockReturnValue('no') - .calledWith(2).mockReturnValue('way?') - .calledWith(3).mockReturnValue('yes') - .calledWith(4).mockReturnValue('way!'); + it('Supports chained calls:', () => { + const fn = jest.fn(); + when(fn) + .calledWith(1) + .mockReturnValue('no') + .calledWith(2) + .mockReturnValue('way?') + .calledWith(3) + .mockReturnValue('yes') + .calledWith(4) + .mockReturnValue('way!'); - expect(fn(1)).toEqual('no'); - expect(fn(2)).toEqual('way?'); - expect(fn(3)).toEqual('yes'); - expect(fn(4)).toEqual('way!'); - expect(fn(5)).toEqual(undefined); - }); + expect(fn(1)).toEqual('no'); + expect(fn(2)).toEqual('way?'); + expect(fn(3)).toEqual('yes'); + expect(fn(4)).toEqual('way!'); + expect(fn(5)).toEqual(undefined); + }); - it('Supports chained calls with defaults:', () => { - const fn = jest.fn(); - when(fn) - .mockReturnValue('nice') - .calledWith(1).mockReturnValue('no') - .calledWith(2).mockReturnValue('way?') - .calledWith(3).mockReturnValue('yes') - .calledWith(4).mockReturnValue('way!'); + it('Supports chained calls with defaults:', () => { + const fn = jest.fn(); + when(fn) + .mockReturnValue('nice') + .calledWith(1) + .mockReturnValue('no') + .calledWith(2) + .mockReturnValue('way?') + .calledWith(3) + .mockReturnValue('yes') + .calledWith(4) + .mockReturnValue('way!'); - expect(fn(1)).toEqual('no'); - expect(fn(2)).toEqual('way?'); - expect(fn(3)).toEqual('yes'); - expect(fn(4)).toEqual('way!'); - expect(fn(5)).toEqual('nice'); - }); + expect(fn(1)).toEqual('no'); + expect(fn(2)).toEqual('way?'); + expect(fn(3)).toEqual('yes'); + expect(fn(4)).toEqual('way!'); + expect(fn(5)).toEqual('nice'); + }); - it('Assert the args:', () => { - const fn = jest.fn(); - when(fn).expectCalledWith(1).mockReturnValue('x'); - let testPassed = false; - try { - fn(2); // Will throw a helpful jest assertion error with args diff - } catch (e) { - testPassed = true; - } - expect(testPassed).toBeTruthy(); - }); + it('Assert the args:', () => { + const fn = jest.fn(); + when(fn) + .expectCalledWith(1) + .mockReturnValue('x'); + let testPassed = false; + try { + fn(2); // Will throw a helpful jest assertion error with args diff + } catch (e) { + testPassed = true; + } + 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)); + 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); - }); + 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)); + 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); - }); + const result = fn('whatever'); + const result2 = fn('whatever'); + expect(result).toMatchObject(expectValue); + expect(result2).toEqual(undefined); + }); + + it('should support resetAllWhenMocks', () => { + const fn = jest.fn(_ => 'initial'); + + when(fn) + .expectCalledWith(1) + .mockReturnValueOnce('x'); + + resetAllWhenMocks(); + + expect(fn(1)).toEqual('initial'); + }); + + it('should supper verifyAllWhenMocksCalled', () => { + const fn1 = jest.fn(); + const fn2 = jest.fn(); + + when(fn1) + .expectCalledWith(1) + .mockReturnValue('z'); + when(fn2) + .expectCalledWith(1) + .mockReturnValueOnce('x'); + when(fn2) + .expectCalledWith(1) + .mockReturnValueOnce('y'); + when(fn2) + .expectCalledWith(1) + .mockReturnValue('z'); + + fn1(1); + fn2(1); + fn2(1); + fn2(1); + + expect(verifyAllWhenMocksCalled).not.toThrow(); + }); });