Skip to content

Instantly share code, notes, and snippets.

@PauloLuan
Created August 12, 2020 07:23
Show Gist options
  • Save PauloLuan/4a965a558a60cbfba35e525c448d3a20 to your computer and use it in GitHub Desktop.
Save PauloLuan/4a965a558a60cbfba35e525c448d3a20 to your computer and use it in GitHub Desktop.
jest-mock-date.ts
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