To mock localStorage methods with Jest, we call jest.spyOn
.
For instance, we write
jest.spyOn(window.localStorage.__proto__, "setItem");
to call jest.spyOn
with window.localStorage.__proto__
, which is the local storage prototype.
We mock the localStorage.setItem
method by calling spyOn
with 'setItem'
as the 2nd argument.