-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Cannot test reducer: Cannot assign to read only property #424
Comments
I've found the solution. It was related to In your
Create a new file
Then you're good to go |
The question is why autofreeze would cause any problems here. |
I suggest you create a small runnable reproduction for the problem and take it up with either immer or jest - this seems like a problem that should be resolved. |
Found it. Looks like you were running into this jest bug: jestjs/jest#9531 |
@jon301 thx, your solution worked for our app. import { configureStore } from '@reduxjs/toolkit';
import { setAutoFreeze } from 'immer';
// Fixes "Cannot assign to read only property" error message
// when modifying objects from Redux state directly.
// https://github.com/reduxjs/redux-toolkit/issues/424
setAutoFreeze(false);
... |
@catamphetamine you should never modify any objects from the Redux state directly though. What are you trying to do there? |
@phryneas In our case, we can and we do. We have implemented "optimistic" updates like when you perform a server-side action on an object and you set the new |
@catamphetamine you have to do that through an action & reducer nonetheless. Blindly editing the redux state from the outside is 100% forbidden in every case. It will cause bugs like your UI not updating. |
We prefer not to.
It won't. |
This is stupidly dangerous. If you want to do that, please stop using Redux and start using a library that supports that pattern. |
Fiiine, fine. Geez. |
@catamphetamine : I'm going to echo what @phryneas said. That is a very bad idea and you should never have been doing that. The number one rule of Redux is DO NOT MUTATE STATE. This is so critical that we repeatedly emphasize the importance of immutability in our tutorials and throughout the docs. We also emphasize that it's never correct to modify state outside of reducers. I'm not sure what gave you the impression that this was possibly a good idea. |
The simplicity of the bare implementation? |
Hi
Given the following reducer:
When I try to test it
Then I get the following error
TypeError: Cannot assign to read only property 'data' of object '#<Object>'
The text was updated successfully, but these errors were encountered: