Created
December 1, 2022 15:52
-
-
Save mostlylikeable/5761c4a49f6e361acc267e9f3fd875fa to your computer and use it in GitHub Desktop.
Jest Freeze Time/Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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