TypeError: Cannot redefine property: useSelector

See original GitHub issue

What 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:closed
  • Created a year ago
  • Reactions:2
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

9reactions
cafesanucommented, Jun 6, 2022

This solved our issue:

const mockDispatch = jest.fn();
const mockSelector = jest.fn();

jest.mock("react-redux", () => ({
 ...jest.requireActual("react-redux"),
 useDispatch: () => mockDispatch,
 useSelector: () => mockSelector,
}));
3reactions
phryneascommented, May 24, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found