This isn't a duplicate question. This is to do with mocking two different dates within the same function. I have tried the following approach to mock a date but that sets both dates to be the same.
beforeAll(() => {
Date.now = jest.fn(() => new Date('2020-04-07T10:20:30Z'));
})
I have a function which compares two dates and return the difference in days as following. The parameter collectionDate is the DD e.g.12 - which is then converted into the date e.g. 12/01/2022.
export const getDateDifference = (collectionDate) => { // collectionDate is the DD - e.g. 12
const today = new Date();
const nextcollectionDate = new Date();
nextcollectionDate.setDate(collectionDate);
const differenceInTime = nextcollectionDate.getTime() - today.getTime();
return (differenceInTime / (1000 * 3600 * 24)).toFixed(0); // returns the differnce in date e.g 10
};
The issue I am having here is because I have two dates being initialised within the same function, when I mock Date, they both get set to the same. Is there a way I can give them both a different date? Or maybe my approach is incorrect and I should instead pass them in from out?