mobx: Mobx 6.1 breaks spying on actions in unit test
Intended outcome:
In our unit tests we are using the jest.spyOn() function to ensure that certain actions are called under certain conditions (with expected values) in our components.
Actual outcome:
With the 6.1 release changes all of our spys are failing in the same manner as below
setUsernameSpy = jest.spyOn(authStore, 'setUsername');
results in:
TypeError: Cannot assign to read only property 'setUsername' of object '[object Object]'
How to reproduce the issue: I’m pretty sure it’s obvious how to reproduce the issue. My question is HOW can I verify that an action on an observable is called with the proper arguments now that 6.1 is available? Is there an option I can set to turn off making actions read-only for tests?
Versions 6.0.4 - working 6.1 - not working
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 16 (3 by maintainers)
Depends if it’s plain object or not. All annotated fields of non-plain objects (classes) are non-configurable as well. We will probably end up with
configure({ safeDescriptors: false })or something like that. Actually already working on it, but still considering alternatives… EDIT: Note that in case of plain objects, changing property configuration viadefinePropertyis also potentially dangerous, but not forbidden because we have to supportdelete key/remove(key)and also it’s not something that can happen just by mistake (as opposed to classes).@urugator I think being able to mock/spy actions is good enough. I cannot think about a use case where mocking an observable or computed is necessary (or even good practice?).
I’ve being writing MobX specs in large code bases for over a year and never had the need to mock an observable or computed.
Unfortunately version 6.1 broke many specs so upgrading is not an option for us till this is fixed. Thanks for your work!
@ljani #2766 should fix this
@urugator
It might be helpful:
https://github.com/facebook/jest/issues/8656