How do I deal with localStorage in jest tests?

后端 未结 17 1788
长情又很酷
长情又很酷 2020-11-28 02:01

I keep getting \"localStorage is not defined\" in Jest tests which makes sense but what are my options? Hitting brick walls.

17条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 02:51

    Unfortunately, the solutions that I've found here didn't work for me.

    So I was looking at Jest GitHub issues and found this thread

    The most upvoted solutions were these ones:

    const spy = jest.spyOn(Storage.prototype, 'setItem');
    
    // or
    
    Storage.prototype.getItem = jest.fn(() => 'bla');
    

提交回复
热议问题