react-hooks-testing-library
version: n/a, using React 18 and @testing-library/react 13.3react
version: 18.2react-dom
version (if applicable): 18.2react-test-renderer
version (if applicable): n/anode
version: v16.16.0npm
(oryarn
) version: 8.11.0
import { QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
test("useIasQuery", async () => {
function useCustomHook() {
return useQuery(['customHook'], () => 'Hello');
}
const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);
const { result } = renderHook(() => useCustomHook(), { wrapper });
await waitFor(() => result.current.isSuccess, {interval: 100});
expect(result.current.data).toEqual("Hello");
});
I attempted to implement and run the example test for ReactQuery (https://tanstack.com/query/v4/docs/guides/testing), using React 18. Note that react-hooks-testing-library renderHooks has been rolled into @testing-library/react for React 18. Therefore, you will note one change in the above code from the example test provided: instead of using the waitFor
method returned by renderHook, I am using the waitFor
method defined in @testing-library/react.
The test fails because waitFor
times out.
git clone [email protected]:jxbaker-sep/broken-react-query-test.git
npm test
We would like to write good tests for our ReactQuery app, but are unable to determine how to correctly write those tests with React 18.
Update ReactQuery docs to demonstrate functional testing with React 18.