/* istanbul ignore file */ /** * @param {Date} expected The date to which we want to freeze time * @returns {Function} Call to remove Date mocking */ export const mockDate = (expected: Date): (() => void) => { const InternalDate = Date; // If any Date or number is passed to the constructor // use that instead of our mocked date function MockDate(mockOverride?: Date | number) { return new InternalDate(mockOverride || expected); } MockDate.UTC = InternalDate.UTC; MockDate.parse = InternalDate.parse; MockDate.now = () => expected.getTime(); // Give our mock Date has the same prototype as Date // Some libraries rely on this to identify Date objects MockDate.prototype = InternalDate.prototype; // Our mock is not a full implementation of Date // Hack types so it'll look good enough globalThis.Date = MockDate as unknown as typeof InternalDate; // Callback function to remove the Date mock return () => { globalThis.Date = InternalDate; }; };