Comments (6)
It seems that upgrading to Jest 22.0.0 or beyond causes the issue
from jest-localstorage-mock.
jest.spyOn(Storage.prototype, 'setItem');
jest.spyOn(Storage.prototype, 'getItem');
jest.spyOn(Storage.prototype, 'removeItem');
Fixes it for me per
jestjs/jest#6858
and jestjs/jest#6798
from jest-localstorage-mock.
Seems like this is related
#80
from jest-localstorage-mock.
Hey, guys, I guess this issue could be closed due merge of #80. Btw, I don't see any release with this changes. Any comments on this?
from jest-localstorage-mock.
The following can be used as a work around:
class LocalStorageMock {
constructor() {
this.__STORE__ = {};
}
clear = jest
.fn()
.mockName('localStorage.clear')
.mockImplementation(() => (this.__STORE__ = {}));
getItem = jest
.fn()
.mockName('localStorage.getItem')
.mockImplementation(key => this.__STORE__[key] || null);
setItem = jest
.fn()
.mockName('localStorage.setItem')
.mockImplementation((key, value) => (this.__STORE__[key] = value.toString()));
removeItem = jest
.fn()
.mockName('localStorage.removeItem')
.mockImplementation(key => delete this.__STORE__[key]);
}
Object.defineProperty(window, 'localStorage', {
value: new LocalStorageMock()
});
beforeEach(() => {
window.localStorage.clear();
window.localStorage.clear.mockClear();
window.localStorage.getItem.mockClear();
window.localStorage.setItem.mockClear();
window.localStorage.removeItem.mockClear();
});
Just add this code in your setup file 👍
Edit: This workaround only seems to work in a Windows environment
from jest-localstorage-mock.
I'll give it a try. But the we still can't access the mocked __STORE__
variable unfortunately.
from jest-localstorage-mock.
Related Issues (20)
- localStorage key "key" is reserved? HOT 3
- Include warning that this package is potentially duplicative for anyone using Jest 24 and above HOT 2
- empty values are coerced to null
- An in-range update of rollup is breaking the build 🚨 HOT 15
- getItem and setItem value implementation for localstorage HOT 3
- How to extend localStorage.getItem? HOT 2
- Where do the dispatch and action calls come from? HOT 5
- docs: usage with create-react-app now requires explicitly setting resetMocks: false HOT 1
- `setItem` does not set the value into storage HOT 2
- The `undefined` value is inconsistent with the browser HOT 1
- Using import 'jest-localstorage-mock' and have this error about my token in test HOT 2
- How do I make API methods throw an exception HOT 4
- getItem() bug HOT 1
- Cannot use it after update Jest HOT 2
- "jest.resetAllMocks();" does not work HOT 6
- cannot set localStorage.__STORE__ to an empty object HOT 2
- Version 2.30 breaks [email protected] HOT 3
- An in-range update of prettier is breaking the build 🚨 HOT 6
- An in-range update of rollup-plugin-babel is breaking the build 🚨 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-localstorage-mock.