Last active
November 21, 2018 19:11
-
-
Save dklassen/fb9a54eadcc7b248bd1f114b5693abd2 to your computer and use it in GitHub Desktop.
Mocking global 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
| // source: https://github.com/facebook/jest/issues/2234 | |
| let timeNow; | |
| const realDate = Date; | |
| describe("Stubbed Date", () => { | |
| beforeAll(() => { | |
| timeNow = Date.now(); | |
| const _GLOBAL: any = global; | |
| _GLOBAL.Date = class { | |
| public static now() { | |
| return timeNow; | |
| } | |
| constructor() { | |
| return timeNow; | |
| } | |
| public valueOf() { | |
| return timeNow; | |
| } | |
| }; | |
| }); | |
| afterAll(() => { | |
| global.Date = realDate; | |
| }); | |
| it("provides constant timestamps", () => { | |
| const ts1 = Date.now(); | |
| const ts2 = +new Date(); | |
| expect(ts1).toEqual(ts2); | |
| expect(ts2).toEqual(timeNow); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment