vendredi 4 décembre 2020

Jest: Spy on object method?

I need to spy on Date.toISOString() and return a value.

const spy = jest.spyOn(global, Date); spies on Date global

const spy = jest.spyOn(global, 'get', Date); spies on Date global get

But how do you spy on the object methods? And return a value?

const spy = jest.spyOn(global.Date, 'toISOString').mockImplementation(() => {
        return new Date().now()
      })

Cannot spy the toISOString property because it is not a function; undefined given instead

const spy = jest.spyOn(global.Date.toISOString).mockImplementation(() => {
        return new Date().now()
      })

Cannot spyOn on a primitive value; undefined given

Aucun commentaire:

Enregistrer un commentaire