What is @types/jest-when?
@types/jest-when is a TypeScript type definition package for the jest-when library, which provides a way to mock functions in Jest based on specific argument conditions. It enhances Jest's mocking capabilities by allowing more granular control over mock behavior.
What are @types/jest-when's main functionalities?
Mocking based on specific arguments
This feature allows you to mock a function to return different values based on the arguments it is called with. The code sample demonstrates how to set up a mock function that returns 'one' when called with 1, 'two' when called with 2, and undefined for any other arguments.
const when = require('jest-when');
const mockFn = jest.fn();
when(mockFn).calledWith(1).mockReturnValue('one');
when(mockFn).calledWith(2).mockReturnValue('two');
console.log(mockFn(1)); // 'one'
console.log(mockFn(2)); // 'two'
console.log(mockFn(3)); // undefined
Mocking based on multiple arguments
This feature allows you to mock a function based on multiple arguments. The code sample shows how to set up a mock function that returns 'one-a' when called with (1, 'a') and 'two-b' when called with (2, 'b').
const when = require('jest-when');
const mockFn = jest.fn();
when(mockFn).calledWith(1, 'a').mockReturnValue('one-a');
when(mockFn).calledWith(2, 'b').mockReturnValue('two-b');
console.log(mockFn(1, 'a')); // 'one-a'
console.log(mockFn(2, 'b')); // 'two-b'
console.log(mockFn(1, 'b')); // undefined
Mocking with default behavior
This feature allows you to set a default return value for a mock function, which will be used if none of the specific argument conditions are met. The code sample demonstrates a mock function that returns 'one' when called with 1 and 'default' for any other arguments.
const when = require('jest-when');
const mockFn = jest.fn().mockReturnValue('default');
when(mockFn).calledWith(1).mockReturnValue('one');
console.log(mockFn(1)); // 'one'
console.log(mockFn(2)); // 'default'
Other packages similar to @types/jest-when
jest-mock-extended
jest-mock-extended is a package that provides extended mocking capabilities for Jest, including type-safe mocks and deep mocks. It is similar to jest-when in that it enhances Jest's mocking capabilities, but it focuses more on type safety and deep mocking.
ts-mockito
ts-mockito is a mocking library for TypeScript inspired by the Java Mockito library. It provides a fluent API for creating mocks, stubs, and spies. Compared to jest-when, ts-mockito offers a more comprehensive and type-safe approach to mocking in TypeScript.
sinon
sinon is a standalone test spies, stubs, and mocks library for JavaScript. It can be used with any testing framework, including Jest. While sinon is not specific to Jest, it provides similar functionalities for mocking and stubbing functions, making it a versatile alternative.
Installation
npm install --save @types/jest-when
Summary
This package contains type definitions for jest-when (https://github.com/timkindberg/jest-when#readme).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/jest-when.
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;
mockReturnValueOnce(value: T): this;
mockResolvedValue(value: jest.ResolvedValue<T>): this;
mockResolvedValueOnce(value: jest.ResolvedValue<T>): this;
mockRejectedValue(value: jest.RejectedValue<T>): this;
mockRejectedValueOnce(value: jest.RejectedValue<T>): this;
mockImplementation(fn: (...args: Y) => T): this;
mockImplementationOnce(fn?: (...args: Y) => T): this;
}
export type When = <T, Y extends any[]>(fn: ((...args: Y) => T) | jest.MockInstance<T, Y>) => WhenMock<T, Y>;
export const when: When;
export function resetAllWhenMocks(): void;
export function verifyAllWhenMocksCalled(): void;
Additional Details
- Last updated: Thu, 23 Dec 2021 23:34:54 GMT
- Dependencies: @types/jest
- Global values: none
Credits
These definitions were written by Alden Taylor, Trung Dang, Gregor Stamać, and Nicholas Hehr.