marchaos / jest-mock-extended

Type safe mocking extensions for Jest https://www.npmjs.com/package/jest-mock-extended
MIT License
828 stars 57 forks source link

Bug: mocking Date with calling date.getHours() causes Error #59

Closed BruTyler closed 2 years ago

BruTyler commented 3 years ago

I've got a bug:

interface d { date: Date } let m = mock ({ date: new Date() })

m.date.getHours() //leads to TypeError: this is not a Date object at Proxy.getHours ()

Reason: Current get method of Proxy's handler loses its context: return obj[property]

Solving: Bind a context for the functions: return obj[property].bind(obj)

As a more specific solving: bind context only for Date's function

BruTyler commented 3 years ago

Did an example of solution: https://github.com/marchaos/jest-mock-extended/pull/60

marchaos commented 2 years ago

Fixed in 2.0.4