-
-
Save rachidelaid/ec233df09613c4ff7146e3901e72b2fd to your computer and use it in GitHub Desktop.
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