-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocking state broken in 4.2.0 (with AsyncStorage in React Native) #1505
Comments
Does it work with the deprecated |
We get the same issue but we are not using we get the following error when our vanilla.js file has:
if we use:
|
Isn't it the problem only in 4.2.0? The error message seems different from OP. How do you define |
Yes, with |
Thanks for confirming. Not sure what's happening, but there must be something somewhere. |
Problem seems to be here: https://github.com/pmndrs/zustand/blob/main/src/middleware/persist.ts#L38. |
Possible solution. Please check if this seems right for you: if (str === null || Object.keys(str).length === 0) {
return null;
}
return JSON.parse(str); |
Thanks @MHC03 for the investigation! @MHC03 If you have chance, please keep investigating. @swemail Can you confirm if 4.2.0 works with RN's AsyncStorage without mocking? |
For mocking the AsyncStorage I use the mock implementation of RN's AsyncStorage. After further investigation I found out, that const str = (storage as StateStorage).getItem(name) ?? null
return Promise.resolve(str).then(parse) Edit: @swemail How have you mocked your AsyncStorage? I followed this documentation: https://react-native-async-storage.github.io/async-storage/docs/advanced/jest/ |
It isn't very nice for sync storage. That's what we put most effort in the old impl. We could check if
On another look, |
Yes it works perfectly without mocking |
Yes, same way, using mocks directory approach |
Digged in further again and found out that somehow AsyncStorageMock.getItem = jest.fn(async (key, callback) => await AsyncStorageMock.getItem(key, callback)); |
Nice finding though. Can you try |
So I came here to report that some part of my app functionality breaks when upgrading 4.1.5 to 4.2.0 and using but on 4.2.0, using the deprecated useEffect(() => {
const unsub = settings.subscribe((state, prevState) => {
if (state.SELECTED_LOCALE !== prevState.SELECTED_LOCALE) {
console.log(prevState.SELECTED_LOCALE, state.SELECTED_LOCALE);
loadLocale(state.SELECTED_LOCALE);
I18nManager.forceRTL(isRTL);
// allow some time for forceRTL to work
setTimeout(restart, 200);
}
});
return unsub;
}); I have a component with snippet above, where it has a drop down component that changes SELECTED_LOCALE I thought this issue is probably related so decided to add my own info here |
@meypod Is your issue only with mocking? If it's an issue for non-mocking context, you might want to open a new discussion? Thanks for chiming in anyway. It might be a hint for someone. |
hmm, sorry I thought it's clear from my comment that it's not in mocking, but in actual app that its causing problem. I just thought maybe this issue and mine have the same root cause |
@dai-shi Your fix solves the problem, but this makes all of this more complicated than I thought. The reason, why all of this works with Changing this behaviour with |
@swemail |
Thanks, still get the same issue in mocking with that fix and using |
Oh, okay. Thanks for confirming. I think I misinterpreted @MHC03 's comment. |
No, it works for me, but I think @swemail Your error message is a bit strange after looking at it again. Maybe we both had different problems. Could you try putting following code in the same file with the zustand mock at the top before jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock')
); |
With that addition it works :) Aaand with that addition the |
Would anyone open a PR to improve docs? @MHC03 About |
Here is our directory setup for mocks If I add this to our jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock'),
); |
I would export it exactly like the docs describe: import AsyncStorageMock from '@react-native-async-storage/async-storage/jest/async-storage-mock';
export default AsyncStorageMock; |
Now it works with the last one, doing it two steps! Sorry for causing confusion, it worked with our old way of mocking async-storage even if it was not exactly as prescribed so I looked in the wrong place 🙈 |
Wanted to add, that I found following issue, which fits to the problem described in this issue: react-native-async-storage/async-storage#379 |
So, can close this issue, right? |
Yes! Thanks for the effort! |
Same with react-native-encrypted-storage |
Summary
Hello and thank you for a great state library!
When upgrading to 4.2.0 and using persist, the suggested mock approach throws error
TypeError: Cannot read properties of undefined (reading 'getItem')
Suggested mock code that breaks:
Our persist setup:
The text was updated successfully, but these errors were encountered: