I'm trying to mock new Date()
to return a specific date. The following code:
const now = new Date()
jest.spyOn(global, 'Date').mockImplementation(() => now)
gives a compilation error: Argument of type '() => Date' is not assignable to parameter of type '() => string'. Type 'Date' is not assignable to type 'string'
.
I think the reason is that jest thinks I'm trying to mock Date()
instead of new Date()
. Indeed, Date()
returns a string. How can I solve this issue?
new Date(Date.now())
in my code and nevernew Date()
. This way I can mockDate.now()
. – Cockaigne