mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 14:20:12 +00:00
[@types/jest-when] add missing functions resetAllWhenMocks and verifyAllWhenMocksCalled (#38129)
This commit is contained in:
committed by
Andrew Casey
parent
252a177545
commit
1978489d1f
8
types/jest-when/index.d.ts
vendored
8
types/jest-when/index.d.ts
vendored
@@ -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 <https://github.com/aldentaylor>
|
||||
// Trung Dang <https://github.com/immanuel192>
|
||||
// Gregor Stamać <https://github.com/gstamac>
|
||||
// Valentin Stern <https://github.com/sehsyha>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 3.0
|
||||
|
||||
/// <reference types="jest" />
|
||||
|
||||
export interface WhenMock<T = any, Y extends any[] = any> extends jest.MockInstance<T, Y> {
|
||||
export interface WhenMock<T = any, Y extends any[] = any>
|
||||
extends jest.MockInstance<T, Y> {
|
||||
calledWith(...matchers: Y): this;
|
||||
expectCalledWith(...matchers: Y): this;
|
||||
mockReturnValue(value: T): this;
|
||||
@@ -24,3 +26,5 @@ export interface WhenMock<T = any, Y extends any[] = any> extends jest.MockInsta
|
||||
export type When = <T, Y extends any[]>(fn: jest.MockInstance<T, Y>) => WhenMock<T, Y>;
|
||||
|
||||
export const when: When;
|
||||
export function resetAllWhenMocks(): void;
|
||||
export function verifyAllWhenMocksCalled(): void;
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user