'How to mock functions in jest using REACT?

I have the following code in jest that I want to mock:

  let now = spacetime.now(timezone);
  now = now.time("12:00am");

How can I mock this?

I tried

jest.mock('spacetime', () => ({
    now: jest.fn(() => {
        return {
            time: jest.fn().mockReturnThis(),
            endOf: jest.fn()
        }
    })
}));

but I get the error

TypeError: Cannot read property 'time' of undefined

Can someone please help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source