Skip to content

Instantly share code, notes, and snippets.

@jogilvyt
Created January 19, 2021 17:14
Show Gist options
  • Save jogilvyt/23648644abfb1cde24851a0eb6016ab8 to your computer and use it in GitHub Desktop.
Save jogilvyt/23648644abfb1cde24851a0eb6016ab8 to your computer and use it in GitHub Desktop.
LocalStorageMock to enable testing of localStorage on older versions of jest
class LocalStorageMock {
constructor() {
this.store = {};
}
clear() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
setItem(key, value) {
this.store[key] = value;
}
removeItem(key) {
delete this.store[key];
}
}
global.localStorage = new LocalStorageMock();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment