I keep getting \"localStorage is not defined\" in Jest tests which makes sense but what are my options? Hitting brick walls.
You need to mock local storage with this snippets
// localStorage.js
var localStorageMock = (function() {
var store = {};
return {
getItem: function(key) {
return store[key] || null;
},
setItem: function(key, value) {
store[key] = value.toString();
},
clear: function() {
store = {};
}
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock
});
And in the jest config:
"setupFiles":["localStorage.js"]
Feel free to ask anything .