Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!