jest-date
jest-date copied to clipboard
Can we add an `expect(date).toBeCloseTo(date)` method?
An assertion like
const expectedDate = ...;
const realDate = getDate();
expect(realDate).toBeSameHourAs(expectedDate); // Usually pass
can fail in this circumstance:
const expectedDate = new Date('2022-12-29 8:59 AM');
const realDate = new Date('2022-12-29 9:00 AM');
expect(realDate).toBeSameHourAs(expectedDate); // Fails
To avoid unstable test assertions, I think an API like the following would be helpful:
expect(new Date('2022-12-29 8:59 AM')).toBeCloseTo(new Date('2022-12-29 9:00 AM')); // Pass
expect(new Date('2022-12-29 8:59 AM')).toBeCloseTo(new Date('2022-12-29 9:00 AM'), {
precision: 60_000, // Precision window in miliseconds
}); // Pass
expect(new Date('2022-12-29 8:59 AM')).toBeCloseTo(new Date('2022-12-29 9:00 AM'), {
precision: 1_000, // Precision window in miliseconds
}); // Fail