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

1.2K
Views
How to test RTK Query with react testing library?

I'm trying to work with RTK Query, but can't find a good example of how to write unit tests with react testing library for a component that uses requests with RTK Query. For example, we have a component that gets a list of something from server. How do mock data for requests? I found a solution to use mswjs for mocking API for tests. But even with it, I have a problem - I need to add await new Promise((r) => setTimeout(r, 1000)); before I'll check that something from the collection exists. Maybe, somebody knows how to test components with RTK Query?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can take a look at the tests of RTK Query itself.

Some things:

  • use msw to mock an api
  • wrap your component in a real Redux store with your api (storeRef.wrapper in the example)
  • use something to wait for UI changes like
      await waitFor(() =>
        expect(screen.getByTestId('isFetching').textContent).toBe('false')
      )
about 4 years ago · Juan Pablo Isaza Report

0

An alternative I have found is mocking the rtk query hooks themselves. Then you can set anything you want for the data object.

jest.mock('pathToHook', () => ({
  __esModule: true,
  default: () => () => { data: 'whatever you want' },
}))

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!