Skip to content

Files

Latest commit

 

History

History
21 lines (15 loc) · 528 Bytes

prefer-spy-on.md

File metadata and controls

21 lines (15 loc) · 528 Bytes

Pattern: Property mock assignment

Issue: -

Description

Assigning mock functions directly to properties makes cleanup harder as the original implementation must be manually restored. Using jest.spyOn() allows for automatic cleanup through Jest's built-in restoration methods.

Examples

Example of incorrect code:

Date.now = jest.fn();
obj.method = jest.fn(() => 42);

Example of correct code:

jest.spyOn(Date, "now");
jest.spyOn(obj, "method").mockImplementation(() => 42);