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
This goes inline with what Andreas suggested in the answer, but i was able to mock it using Storage interface. I did something like this,
In jest,
test('throw error', () => {
Storage.prototype.setItem = jest.fn(() => {
console.log(" called "); // <-- was called
throw new Error('ERROR');
});
utility.setItem('123', 'value');
});
Also this PR discussion was helpful.