Cannot assign to read only property 'props' of object '#<Object>' with React
See original GitHub issue🐛 Bug Report
After upgrading to jest 25.x.x, when comparing 2 different React Components with
expect(<...>).toEqual(<...>)
there’s an TypeError instead of a report of the differences between the components.
To Reproduce
expect(<div prop1="" />).toEqual(<div/>)
TypeError: Cannot assign to read only property 'props' of object '#<Object>'
3 | test('renders learn react link', () => {
4 | expect(<div/>).toEqual(<div/>)
> 5 | expect(<div prop1="" />).toEqual(<div/>)
| ^
6 | });
7 |
at Object.<anonymous> (src/App.test.js:5:28)
Expected behavior
It works fine in jest 24.9
expect(received).toEqual(expected) // deep equality
- Expected
+ Received
- <div />
+ <div
+ prop1=""
+ />
Link to repl or repo (highly encouraged)
git clone git@github.com:dpinol/jest-react-toEqual-bug.git
cd jest-react-toEqual-bug
npm i -D
npm run test
envinfo
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Cannot assign to read only property 'props' of #<Object> in ...
You cannot push to props this.props.nav.push({id: 'Applist', index: 2}); since component properties are read-only, as the error states.
Read more >TypeError: Cannot assign to read only property of ... - GitHub
The issue arises when you are passing in an immutable object as your default values. In my case I was directly passing in...
Read more >Cannot assign to read only property of Object in JavaScript
The error "Cannot assign to read only property of object" occurs when we try to change a property of an object that has...
Read more >Cannot assign to read only property 'props' of #<Object> in ...
[Solved]-Cannot assign to read only property 'props' of #<Object> in react native-Reactjs ... You cannot push to props this.props.nav.push({id: 'Applist', index: ...
Read more >Fix useRef() “Cannot Assign to … Read Only Property" Error
Now, as you can see from the example above, the type of the reference object is a mutable object (i.e. React.MutableRefObject ) which...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I am running into this problem as well. It seems quite easy to reproduce:
The expect statement produces the error
TypeError: Cannot assign to read only property 'foo' of object '#<Object>'.This issue occurs on Jest >=
25.0.0.This isn’t a sufficient fix. I filed https://github.com/facebook/jest/issues/9745.