How can i conform nock with react-query? #6074
Replies: 2 comments 6 replies
-
I'd say because that's not how
I also have a working version here:
|
Beta Was this translation helpful? Give feedback.
-
I have successfully implemented msw, I made adjustments based on your reply, but the results I printed out are still not as expected. source code: import { createQueryProviderWrapper } from "@/test/index";
import { useQuery } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import nock from "nock";
const fetchUserProfile = async (tokenKey: string) => {
const response = await fetch("http://api.example.com/v1/me", {
headers: {
Authorization: `Bearer ${tokenKey}`,
},
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
};
const useGetUserProfileAPIQuery = (tokenKey: string) => {
return useQuery(["userProfile", tokenKey], () => fetchUserProfile(tokenKey));
};
describe("useGetUserProfileAPIQuery", () => {
test("should fetch user profile and have correct nickname", async () => {
nock("http://api.example.com").get("/v1/me").reply(200, {
id: 1,
firstName: "/value from the api",
});
const tokenKey = "sampleTokenKey";
const { result } = renderHook(() => useGetUserProfileAPIQuery(tokenKey), {
wrapper: createQueryProviderWrapper(),
});
await waitFor(() => expect(result.current.isSuccess).toBe(true), {
timeout: 5000,
}); // fail
expect(result.current.data).toBeDefined(); // fail
expect(result.current.data?.id).toBe(1); // fail
});
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to test a custom hook that uses react-query for data fetching in a React 18 project. The API call is mocked using nock, but I am facing issues: the result.current remains undefined, and the query stays in the loading state. I've already consulted the React Query testing documentation, but it hasn't been helpful for solving this problem.
my source code:
Specific Issues
Beta Was this translation helpful? Give feedback.
All reactions