Problem with transpose function while running jest's fake timers.
Author: JoueBienCreated Feb 10, 2026Updated Aug 25, 2026
There is an issue with the transpose function.
The problem
Jest modifies the Date in order to set the now time. This causes transpose to fail with a run time error.
beforeEach(() => {
/**
*
* Server should be in AEST
*/
jest.useFakeTimers({
now: new Date("2009-05-05T20:00:00Z"),
});
});
afterEach(() => {
jest.useRealTimers();
});
const now = new Date();
const midnight = transpose(
new TZDate(new Date(now.getTime()).setHours(0, 0, 0), "Australia/NSW"),
Date
);
// transpose throws a `TypeError: date_.setFullYear is not a function`
This appears to be caused by the constructor check incorrectly failing with in transpose.
Actual behaviour
Transpose throws a TypeError: date_.setFullYear is not a function
Expected behaviour
Transpose returns a correct date.
Debug information
- date-fns version: 4.1.0
- Browser/Node.js version: jest "29.7.0", npm "10.9.3", node "2.18.0"
- Your timezone: "Australia/NSW" OR Sydney or Brisbane
- Your current time: 10th Jan 2025
Source: date-fns/date-fns