TypeError: Cannot redefine property: useSelector
See original GitHub issueWhat version of React, ReactDOM/React Native, Redux, and React Redux are you using?
- React: “17.0.2”
- React Native: “0.68.2”
- Redux: “4.2.0”
- React Redux: “8.0.1”
What is the current behavior?
In some test cases I used spyOn for mock useSelector hook -
import * as ReactRedux from 'react-redux';
jest.spyOn(ReactRedux, 'useSelector').mockReturnValueOnce({value: mockValue});
after updating to “8.0.1”, I’m getting error -
TypeError: Cannot redefine property: useSelector
at Function.defineProperty (<anonymous>)
What is the expected behavior?
everything was okay on
"react-redux": "7.2.8",
"redux": "4.1.2",
Which browser and OS are affected by this issue?
No response
Did this work in previous versions of React Redux?
- Yes
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:13 (9 by maintainers)
Top Results From Across the Web
Unable to mock react-redux hooks after migrating to 8
TypeError: Cannot redefine property: useSelector at Function. ... I had the same problem. My solution ... useSelector.
Read more >spyOn: Cannot redefine property #26 - aelbore/esbuild-jest
While mocks are working great for me, I'm having some trouble with spies. I created this code. import { moduleDependency } from "....
Read more >TypeError: can't redefine non-configurable property "x"
The JavaScript exception "can't redefine non-configurable property" occurs when it was attempted to redefine a property, but that property is non-configurable.
Read more >Mocking redux useSelector-hook - DEV Community
I am getting this error. TypeError: _reactReedux.useSelector.mockImplementation is not a function. Kindly someone help.
Read more >Cannot redefine property: uniquelize while integrating ...
I am working integrating zoom meeting web SDK with existing react app which uses webpack. Browser Console Error Getting Uncaught TypeError: ...
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 FreeTop 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
Top GitHub Comments
This solved our issue:
I will add to that that this is caused by a babel default setting. Mocking imports is extremely flimsy and cannot really be relied on. While we can make this work for now, I want to 💯 stress what @markerikson already said a few times: this is not how we recommend testing components. Testing components in isolation with directly mocked methods will not give you a lot of confidence at all - you are essentially testing implementation details, not actual functionality.