Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
How can i test redux .subscribe()

In my code, that i want to test, has store.subscribe() function, and i dont know, how can i test it.

test('should call console.log inside subscribe fn', async () => {
  const store = configureStore(initialState);
  store.subscribe(() => console.log(store.getState())); // console.log was not called

  await wait(100);
})

And in my code

// index.ts

export const store = createStore();
store.subscribe(() => console.log(store.getState())); // how can i test it in jest?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You want to test the subscriber function, then you can trigger it through dispatch action. Assert the state after each action is dispatched.

import { configureStore } from '@reduxjs/toolkit';

test('should call console.log inside subscribe fn', async () => {
  expect.assertions(1);
  const store = configureStore({ reducer: (state = 'test state') => state });
  store.subscribe(() => {
    expect(store.getState()).toEqual('test state');
  });
  store.dispatch({ type: '' });
});

test result:

 PASS   redux-toolkit-example  packages/redux-toolkit-example/stackoverflow/69845750/index.test.ts
  ✓ should call console.log inside subscribe fn (4 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.869 s
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda