Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 41 ยท Testing and Tooling

Testing Reducers, Selectors and Thunks

Learn how to test Redux logic: pure reducers and selectors with simple assertions, thunks with fake dispatch and APIs, and whole-store integration tests. Runnable examples.

Redux was designed to be easy to test. Reducers and selectors are pure functions, actions are plain objects, and the store is an ordinary value you can create in a test. You rarely need heavy mocking. This lesson shows what to test, how, and at which level, with runnable examples that use a tiny test helper so the editor output is easy to read.

On a real project you would use Vitest or Jest (test, expect). The editors here cannot load them, so the examples use a minimal test/expect stand-in. The test bodies are the same as you would write with the real tools.

What to test

The Redux team's advice: test behaviour, not implementation. Prefer tests that go through the store's public surface (dispatch an action, read via a selector) over tests that look at reducer internals.

Level Tests Speed
Unit A reducer, a selector, a utility Instant
Integration A real store with real slices; dispatch, assert on selectors Fast
Component Rendered UI with a real store (next lesson) Moderate

Most value comes from integration tests with a real store: they exercise reducers, selectors and middleware together, and survive refactors.

Reducers and selectors

A reducer test is: given this state and this action, expect this state. No setup, no mocks.

JS Testing a reducer and a selector

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Five tests pass and one fails on purpose so you can see what a failure message looks like. Notice the "no mutation" test: it protects you from the most common reducer bug.

With Redux Toolkit slices the same applies; you import todosSlice.reducer and the action creators (todosSlice.actions.todoAdded('Milk')) and assert the same way. Prefer using the action creators over hand-writing action objects, so renaming an action does not break tests.

Thunks

A thunk is a function that takes dispatch and getState. Test it by passing fakes: a dispatch that records calls and a getState that returns a chosen state. The network is replaced by an injected function.

JS Testing a thunk with a fake dispatch

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The injected { api } third argument is the extraArgument option of the thunk middleware (configureStore({ middleware: gdm => gdm({ thunk: { extraArgument: { api } } }) })). It is the cleanest way to swap a real API client for a fake in tests, with no module mocking.

For createAsyncThunk you can do the same in an integration test (read-only):

JavaScript
// Real Redux Toolkit + Vitest code (read-only)
test('loads todos', async () => {
  const store = configureStore({ reducer: { todos: todosReducer } });
  vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(JSON.stringify([{ id: 1, text: 'a' }])));
  await store.dispatch(fetchTodos());
  expect(selectAllTodos(store.getState())).toHaveLength(1);
  expect(store.getState().todos.status).toBe('succeeded');
});

Integration tests with a real store

Create the store with the real reducers and drive it through actions. A helper that builds a fresh store per test, optionally with preloadedState, keeps tests isolated:

JavaScript
// Real Redux Toolkit code (read-only)
export function makeStore(preloadedState) {
  return configureStore({ reducer: rootReducer, preloadedState });
}
JS An integration test through the store

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The test never mentions reducers: it only dispatches and reads through a selector. If you later reshape the state or rename an internal action, tests like this keep passing as long as behaviour is unchanged.

Testing middleware

Create a fake store ({ getState, dispatch }), a fake next, call middleware(store)(next)(action), and assert on the calls. This pattern, "invoke the triple function by hand," works for any middleware, and a recording next lets you check that it passes, blocks or transforms the action.

Common mistakes

  • Asserting on the exact action list or the internal state shape when you care about behaviour.
  • Sharing one store between tests (state leaks): create a fresh store per test.
  • Mocking selectors or reducers, which tests the mock.
  • Forgetting await on a thunk so the assertion runs before it finishes.
  • Testing Immer or Redux Toolkit themselves.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the simplest way to test a reducer?

    Reducers are pure functions, so input and output are enough.
  2. 2. How do you fake the network when testing a thunk?

    Injecting the API client keeps thunks easy to test without module mocking.
  3. 3. Why create a new store for each test?

    A shared store makes tests depend on each other's order.
  4. 4. Which test survives a refactor of the state shape best?

    Selectors are the public read API; tests that use them test behaviour.

Summary

  • Test behaviour through dispatch and selectors; avoid testing internals.
  • Reducers and selectors need only plain inputs; include a no-mutation test.
  • Thunks are tested with a recording dispatch, a fake getState and an injected API.
  • Prefer integration tests with a fresh real store per test.
  • Middleware is tested by calling middleware(store)(next)(action) with fakes.

Next: Testing React components with Redux