I’m using date-fns, a JavaScript date utility library. I’m trying to write a Vitest for fromUnixTime.
My Vitest keeps failing:
describe("fromUnixTime function", () => {
test("should return the correct date from a Unix timestamp in UK time", () => {
const unixTimestamp = 1612345678 // Unix timestamp 2021-02-03 04:56:18 UTC
// Convert Unix timestamp to Date object
const date = fromUnixTime(unixTimestamp)
// Verify if the Unix timestamp obtained from the date object matches the original Unix timestamp
expect(getUnixTime(date)).toBe(unixTimestamp)
// Verify the components of the date object
expect(date.getFullYear()).toBe(2021)
expect(date.getMonth()).toBe(1) // Note: Month is zero-based index
expect(date.getDate()).toBe(3)
expect(date.getHours()).toBe(4) // The Unix timestamp is in UTC, so it should be adjusted for UK time zone
expect(date.getMinutes()).toBe(56)
expect(date.getSeconds()).toBe(18)
})
})
In the terminal I see:
FAIL src/utils/dates.test.tsx > fromUnixTime function > should return the correct date from a Unix timestamp in UK time
AssertionError: expected 9 to be 4 // Object.is equality
- Expected
- Received
- 4
- 9
expect(date.getHours()).toBe(4)