Skip to content

Instantly share code, notes, and snippets.

@mostlylikeable
Created December 1, 2022 15:52
Show Gist options
  • Save mostlylikeable/5761c4a49f6e361acc267e9f3fd875fa to your computer and use it in GitHub Desktop.
Save mostlylikeable/5761c4a49f6e361acc267e9f3fd875fa to your computer and use it in GitHub Desktop.
Jest Freeze Time/Date
const freezeTime = (): Date => {
const now = new Date();
jest.useFakeTimers();
jest.setSystemTime(now);
return now;
};
const unfreezeTime = () => {
jest.useRealTimers();
};
describe('context', () => {
let frozenNow: Date;
beforeAll(() => {
frozenNow = freezeTime();
});
afterAll(() => {
unfreezeTime();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment