I know this question has been asked multiple times.
But I could not find the correct one for my case.
I would like to mock the moment() to return a specific date.
First, I mock by
jest.mock("moment", () => {
return (date: string) =>
jest.requireActual("moment")(date || "2021-01-01T00:00:00.000Z");
});
But I use some properties of moment, (moment.duration()... for example) So when mock like this, it does not work.
Next I tried to mock Date.now by several ways:
jest.spyOn(Date, "now").mockReturnValue(+new Date("2021-01-01T00:00:00.000Z"));
Date.now = jest.fn(() => +new Date("2021-01-01T00:00:00.000Z")
But when doing this, when calling moment() it returns an invalid date.
I'm not sure what I am doing wrong.