Description
openedon Feb 14, 2020
Note: This is not a bug with react-query, but I wanted to leave this here if anyone is experiencing the same problems.
Problem
When trying to write react-native tests for Components which use react-query, jest seemingly gets stuck without printing anything.
Solution
Put this inside your jest setup file:
global.window = global
What's happening?
As stated in the docs, react-query's hooks only return initialData (or null
) if it's running on the server. If the hook mounts, it will re-execute, the queries will be cached and data will be fetched.
How does this affect React Native? react-query is checking if it's running on the server with typeof window === 'undefined'
. window
is defined in the React Native runtime, but it's undefined in the react-native jest preset!
This causes the useEffect
hooks of react-query to get stuck in an infinite loop, because it correctly assumes that when useEffect
is executed, it is running inside the client. But window
is still undefined, thus react-query thinks it's still on the server, and the whole thing repeats forever.
Setting window
to something other than undefined will resolve this. Since window
seems to have the same attributes as global
, I just cross-referenced them, to reduce the difference between both environments.
I opened a PR inside react-native (facebook/react-native#28067).