I need run some testing using jest and moment. Some of the imported functions work with moment current date (moment() ). But I can't seem to find a way of always run the same date for testing or mock moment constructor, like pin moment date at moment('2020-07-05'), even if current day is 2020-07-10, so the test should always run under day 5.
My ./UtilsModule file:
export const getIntervalDates = groupOfDates => {
const CURRENT_DATE = moment().format('YYYY-MM-DD');
return getDates(CURRENT_DATE, groupOfDates );
};
export const nextDates = (date, group) => {
let newDate= getIntervalDates(group);
}
My test.js file, and what i tried for:
it('testing function', () => {
const PAYLOAD = {...};
const DATE= '2020-07-20';
const SpyGetIntervalDates = jest.spyOn(UtilsModule, 'getIntervalDates');
SpyGetIntervalDates.mockImplementation(() => Promise.resolve({ minAge: '2020-08-04' }));
const nextDate = UtilsModule.nextDates (DATE, PAYLOAD);
expect(nextDate).toEqual({ minDate: '2020-11-04' });
});
I also tried, but i couldn't make it to work :
jest.mock('moment', () => {
return () => jest.requireActual('moment')('2020-07-04');
});
and
global.moment = jest.fn(moment('2021-07-04'));
Aucun commentaire:
Enregistrer un commentaire