4

I'm getting this error when testing a component that uses react query custom hook: " No QueryClient set, use QueryClientProvider to set one "

this is my hook:

export default () => {
  const loginMutation = useMutation(
    async (payload: LoginDto) => {
      return await MembershipService.login(payload);
    },
    {
      onSuccess: ({ data }: AxiosResponse) => alert(data),
      onError: (error: AxiosError) => {
        console.log(error);
      },
    }
  );

  const signupMutation = useMutation(
    async (payload: SignupDto) => {
      return await MembershipService.signup(payload);
    },
    {
      onSuccess: ({ data }: AxiosResponse) => alert(data),
      onError: (error: AxiosError) => {
        console.log(error);
      },
    }
  );

  return {
    loginMutation,
    signupMutation,
  };
};

and here is my test:

test("should display required error", async () => {
    render(
      <QueryClientProvider client={queryClient}>
        <Signup />
      </QueryClientProvider>
    );
    fireEvent.submit(screen.getByRole("button"));
    expect(await screen.findAllByRole("alert")).toHaveLength(5);
  });

As you can see, I already passed an instance of QueryClient class to wrapper component. And also I'm using axios and axios-mock-adaptor to mocking requests.

Erfan Chegini
  • 61
  • 1
  • 3
  • similar question, maybe you could find your answer here : https://stackoverflow.com/questions/65590195/no-queryclient-set-use-queryclientprovider-to-set-one – Vahid Mar 02 '22 at 09:25
  • 1
    I got it while I was testing with jest and react-testing-library, and I did not have any issues during development or building – Erfan Chegini Mar 02 '22 at 09:39
  • duplicate of this one? https://stackoverflow.com/questions/65694015/integrate-testing-for-react-query-with-testing-library/ you're also not showing where the queryClient is created, and how the Signup component uses it – TkDodo Mar 03 '22 at 19:28

0 Answers0