Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

🐛Requests with RTK Query do not resolve with various jest.useFakeTimers() configs #1762

Closed
Shrugsy opened this issue Nov 23, 2021 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed rtk-query

Comments

@Shrugsy
Copy link
Collaborator

Shrugsy commented Nov 23, 2021

When using jest.useFakeTimers(), RTK Query requests resolve normally with jest.useFakeTimers('legacy'), but not with jest.useFakeTimers('modern')

A repo reproducing the issue can be found here:

Tests with jest^26.6.3

  • ✔️ passes with useFakeTimers('legacy') (note that useFakeTimers() defaults to 'legacy' on this jest version)
  • ❌ fails with useFakeTimers('modern')

Test results: https://github.com/Shrugsy/rtk-query-jest-compatibility-test/runs/4305391175?check_suite_focus=true

Test file: https://github.com/Shrugsy/rtk-query-jest-compatibility-test/blob/79d6dd98a499dbc5df9f2fb631e0fa6888ed724d/src/_tests/App.spec.tsx

The observed symptom is that the query hook always stays in the isLoading state

FAIL src/_tests/App.spec.tsx (5.103 s)
  App test
    ✓ Loads a project without fake timers (789 ms)
    ✓ Loads a project with jest fake timers (legacy) (643 ms)
    ✕ Loads a project with jest fake timers (modern) (1021 ms)

  ● App test › Loads a project with jest fake timers (modern)

    TestingLibraryElementError: Unable to find an element with the text: /Successfully fetched projects!/i. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

Tests with jest^27.0.0

  • ❌ fails with useFakeTimers('legacy')
  • ❌ fails with useFakeTimers('modern') (note that useFakeTimers() defaults to 'modern' on this jest version)

Test results: https://github.com/Shrugsy/rtk-query-jest-compatibility-test/runs/4305392974?check_suite_focus=true

FAIL src/_tests/App.spec.tsx
  App test
    ✓ Loads a project without fake timers (769 ms)
    ✕ Loads a project with jest fake timers (legacy) (1027 ms)
    ✕ Loads a project with jest fake timers (modern) (1023 ms)

  ● App test › Loads a project with jest fake timers (legacy)

    Unable to find an element with the text: /Successfully fetched projects!/i. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
@Shrugsy Shrugsy added bug Something isn't working help wanted Extra attention is needed rtk-query labels Nov 23, 2021
@Shrugsy Shrugsy changed the title 🐛Requests with RTK Query do not resolve with jest v27+ useFakeTimers 🐛Requests with RTK Query do not resolve with jest useFakeTimers('modern') Nov 23, 2021
@Shrugsy Shrugsy changed the title 🐛Requests with RTK Query do not resolve with jest useFakeTimers('modern') 🐛Requests with RTK Query do not resolve with various jest.useFakeTimers() configs Nov 23, 2021
@Shrugsy
Copy link
Collaborator Author

Shrugsy commented Nov 25, 2021

For jest v26.6.3, after swapping whatwg-fetch out for node-fetch, both legacy and modern fake timers work as expected.

https://github.com/Shrugsy/rtk-query-jest-compatibility-test/runs/4318217563?check_suite_focus=true

v27+ still needs further investigation

@markerikson
Copy link
Collaborator

Ironically I just ran into this yesterday while trying to deal with test failures from #2409 . I did indeed end up setting all of our useFakeTimers back to 'legacy':

#2460

@markerikson
Copy link
Collaborator

Well, I ended up switching us to Vitest for our 2.0 branch. No one else has complained about this in their tests, so I'm going to close this.

@ensconced
Copy link

No one else has complained about this in their tests

I would like to do so now 🙂

I'm not sure if my case is exactly the same though, as using legacy timers doesn't seem to help for me. I have opened a new issue here.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working help wanted Extra attention is needed rtk-query
Projects
None yet
Development

No branches or pull requests

3 participants