'How to write snapshot tests for react apps using redux toolkit?

I switched my app from context API to redux toolkit and of course some tests are failing now :)

How can I test apps (e.g. write a snapshot test) that use the redux toolkit?

My (simplified) test currently looks like this:

import { store } from '../redux/store';
import { render } from '@testing-library/react';
import { Provider } from 'react-redux';

import { MyComponent } from './MyComponent';

import '@testing-library/jest-dom';

describe('MyComponent', () => {
  test('should match snapshot', () => {
    const { baseElement } = render(
      <Provider store={store}>
        <MyComponent />
      </Provider>,
    );
    expect(baseElement).toMatchSnapshot();
  });
});

For this I get the following error message:

TypeError: Cannot read properties of undefined (reading 'reducer')

  4 | export const store = configureStore({
  5 |   reducer: {
> 6 |     result: mySlice.reducer,
    |                                         ^
  7 |   },
  8 | });
  9 |

What am I doing wrong or whats missing here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source