Saturday, 8 May 2021

Jest snapshot failing test on computer timestamp (AM/PM)

I have a jest test failing on the date and time, which I think is because the computer settings are different for the user running it. It is a snapshot test, and it's not an accurate failure because we want to show whatever time zone the user has.

-             11:59:42 AM 7/26/2019
+             11:59:42 26/07/2019

In my code there’s a getTimeValue which is used to format date to time with point.timestamp.toDate().toLocaleTimeString().

I tried this approach and this one too but it didn't change the output at all:

let mockDate;

beforeAll(() => {
  mockDate = jest.spyOn(Date.prototype, 'toLocaleTimeString').mockReturnValue('2020-04-15');
});

afterAll(() => {
  mockDate.mockRestore();
});

EDIT: I realised there's already a globalsetup.ts file in this project that is handling the timezone, so the problem is actually with the timestamp (AM, PM) and date format.



from Jest snapshot failing test on computer timestamp (AM/PM)

No comments:

Post a Comment