Last active
June 13, 2023 21:20
Mock localStorage with Jest for React testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'], | |
setupFiles: ['./test-setup.js'] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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