Created
August 12, 2020 07:23
-
-
Save PauloLuan/4a965a558a60cbfba35e525c448d3a20 to your computer and use it in GitHub Desktop.
jest-mock-date.ts
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
describe('jest mock date example', () => { | |
let realDateInstance = Date | |
beforeAll(() => { | |
const mockedDate = new Date('2016-12-14T02:00:00.000Z') | |
global.Date = class extends Date { | |
constructor (date) { | |
if (date) { | |
return super(date) | |
} | |
return mockedDate | |
} | |
} | |
console.log(new Date()) | |
}) | |
afterAll(() => { | |
global.Date = realDateInstance | |
}) | |
it('should mock a date', () => { | |
console.log(new Date()) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment