Jest mock localStorage methods

前端 未结 4 980
猫巷女王i
猫巷女王i 2021-01-06 11:15

I would like to mock localStorage methods in jest for error simulation. I have localstorage getter and setter methods defined in utility.js. I would like to mock local

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-06 12:05

    jest.spyOn(window.localStorage.__proto__, 'setItem'); works with nothing else at all needed, as noted here: https://github.com/facebook/jest/issues/6798#issuecomment-440988627

提交回复
热议问题