Replies: 1 comment
-
I guess when you write tests, due to time changes, this can lead to incorrect snapshot matches. You can start by mocking the system time. import { setSystemTime, beforeAll, test, expect } from "bun:test";
beforeAll(() => {
setSystemTime(new Date("2020-01-01T00:00:00.000Z"));
});
test("it is 2020", () => {
expect(new Date().getFullYear()).toBe(2020);
}); in vitest test('mocking the system time', () => {
vi.useFakeTimers();
// Set the current date to a specific point in time
const now = new Date('2023-04-20');
vi.setSystemTime(now);
// Now `Date.now()` and `new Date()` will use the time set above
expect(Date.now()).toBe(now.getTime());
// When you want to restore the original timers
vi.useRealTimers();
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It messes with inline snapshot tests.
Would like the response not to contain
date
.Beta Was this translation helpful? Give feedback.
All reactions