Skip to content

Instantly share code, notes, and snippets.

@rachidelaid
Forked from johno/jest.config.js
Created January 19, 2022 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rachidelaid/ec233df09613c4ff7146e3901e72b2fd to your computer and use it in GitHub Desktop.
Save rachidelaid/ec233df09613c4ff7146e3901e72b2fd 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