Skip to content

Instantly share code, notes, and snippets.

@johno
Last active June 13, 2023 21:20
Show Gist options
  • Save johno/9b11719c57d14fb4fb144d6c1a84208f to your computer and use it in GitHub Desktop.
Save johno/9b11719c57d14fb4fb144d6c1a84208f to your computer and use it in GitHub Desktop.
Mock localStorage with Jest for React testing
module.exports = {
testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'],
setupFiles: ['./test-setup.js']
}
module.exports = class LocalStorage {
constructor() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
setItem(key, value) {
this.store[key] = value.toString();
}
removeItem(key) {
delete this.store[key];
}
reset() {
this.store = {};
}
};
const LocalStorage = require('./local-storage-mock');
global.localStorage = new LocalStorage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment