Pattern: Property mock assignment
Issue: -
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.
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);