Skip to content

Instantly share code, notes, and snippets.

@lakshmankashyap
Forked from sanusart/localStorage.js
Created September 28, 2018 07:49
Show Gist options
  • Save lakshmankashyap/8d1bf18ae145e4a1d673e992a8e5cf3c to your computer and use it in GitHub Desktop.
Save lakshmankashyap/8d1bf18ae145e4a1d673e992a8e5cf3c to your computer and use it in GitHub Desktop.
Jest mock localStorage #test #jest
// ./__mocks__/localStorage.js
let mockStorage = {};
module.exports = window.localStorage = {
setItem: (key, val) => Object.assign(mockStorage, {[key]: val}),
getItem: (key) => mockStorage[key],
clear: () => mockStorage = {}
};
// Then import it in the reducer test file
import '../__mocks__/localStorage';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment