Last active
June 13, 2023 21:20
-
-
Save johno/9b11719c57d14fb4fb144d6c1a84208f to your computer and use it in GitHub Desktop.
Mock localStorage with Jest for React testing
This file contains 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 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 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