My test includes a step where the date is set based on the current date (using dayjs()
). I need to always get the same, pre-defined date.
dayjs() calls new Date()
, so my approach was to mock the global Date()
constructor. I've tried it like this:
await t.eval( () => {
const fixedDate = new Date(2010, 0, 1);
Date = class extends Date {
constructor() {
super();
return fixedDate;
}
};
});
Like this, testcafe can't finish to eval
(works in my Chrome though). So far, I only managed to overwrite Date.now()
directly, but not the constructor.
I wonder if the approach to modifying Date
with eval
is the right approach or if there's any better solution how to fixate the current Date
.