3

I used to have a global file with all my selector mocks. It was a list of all my store selectors and looked like below:

jest.mock('redux/mainData/selectors', () => ({
  selectLoading: jest.fn(() => false),
  selectData: jest.fn(() => [{ id: '1' }]),
}));

It used to work like a charm, but when I tried upgrading Jest to v29 it doesn't any more.

In jest.setup.ts there was (and is):

jest.mock('react-redux', () => ({
  useSelector: jest.fn(fn => fn()),
}));

When I'm testing a component, that has:

const data = useSelector(selectData);

and then below in the component

data?.map(...)

I'm getting:

Error: Uncaught [TypeError: Cannot read properties of undefined (reading 'data')]

My file with mocked selectors has about 300 mocked selectors so now basically every test fails.

I tried doing:

jest.mock('redux/mainData/selectors', () => ({
  selectData: () => ([{ id: '1' }]),
}));

or

jest.mock('redux/mainData/selectors', () => ({
  selectData: jest.fn().mockReturnValueOnce([{}]);
}));

but both didn't work.

My example selector:

export const selectData = createSelector(
  selectMainData,
  ({data}) => data,
);

I'm using Jest v29.5.0 and Reselect v4.1.8

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
jake-ferguson
  • 315
  • 3
  • 11
  • 32
  • Looks good to me. Can you create a https://stackoverflow.com/help/minimal-reproducible-example, Try debugging by calling `selectData()`, what result do you get? – Lin Du Jun 29 '23 at 07:58
  • I'm getting `undefined` in the component not matter in which way I try to mock the data. I've also tried ` mocked(selectData).mockImplementation(() =>[]);` – jake-ferguson Jul 18 '23 at 09:52
  • which version of nodejs are you using? – Ahmed Sbai Jul 18 '23 at 21:19
  • Node version 18 – jake-ferguson Jul 19 '23 at 11:44
  • @jake-ferguson I see you put a bounty on this 2 days ago so I assume you still have quite a predicament. Does the [latest version](https://www.npmjs.com/package/jest/v/29.6.1) still have this issue? As Lin Du suggested; is it possible for you to create a project on github that reproduces this problem? – HMR Jul 20 '23 at 10:14
  • Hey, yes the lates 29.6.1 Jest still producing the same issue with mock selectors not working for me. GH would be a bit hard since this is a big, old project unfortunately that cannot be shared. – jake-ferguson Jul 20 '23 at 11:42

0 Answers0