Skip to content

Files

Latest commit

 

History

History
21 lines (15 loc) · 554 Bytes

prefer-mock-promise-shorthand.md

File metadata and controls

21 lines (15 loc) · 554 Bytes

Pattern: Verbose promise mock

Issue: -

Description

Jest provides shorthand methods for mocking promise-returning functions. Using mockResolvedValue() or mockRejectedValue() is clearer and more concise than mockImplementation() with Promise construction.

Examples

Example of incorrect code:

jest.fn().mockImplementation(() => Promise.resolve(42));
mock.mockReturnValue(Promise.reject(new Error()));

Example of correct code:

jest.fn().mockResolvedValue(42);
mock.mockRejectedValue(new Error());