Jest and useSWRInfinite causes act(() =>) problem. #711
Answered
by
nathanmrtns
nathanmrtns
asked this question in
Q&A
-
So, I have a component that runs useSWRInfinite to fetch some data, once this data is fetched, it updates the element. const Servers = ({ title, session, initialData }) => {
const router = useRouter();
const team = session?.team;
const projectId = router.query.project_id;
const { data, error, mutate, size, setSize, isValidating } = useSWRInfinite(
index => `/api/url`); //tried with initialData but the problem still occurs
return <Component that updates when data is fetched/>
} The problem is that in tests, it causes the act problem:
I tried encapsulating the component creation inside the act, but it doesn't work. it.only('should show the BlankSate component', async () => {
wrapper = mount(
<RouterContext.Provider value={router}>
<Servers title="Servers" />
</RouterContext.Provider>);
expect(wrapper.find(BlankState)).toHaveLength(1); //it breaks here with the act message
}); Do you guys know what should I do? |
Beta Was this translation helpful? Give feedback.
Answered by
nathanmrtns
Oct 23, 2020
Replies: 1 comment 7 replies
-
In case anybody else finds the same problem, here is my solution. jest.mock("swr");
useSWRInfinite.mockReturnValue({ "data": [{ data: [] }], "error": null });
it.only('should show the BlankSate component', async () => {
useSWRInfinite.mockReturnValue({ "data": [{ data: [] }], "error": null }); //set mock value per test
wrapper = mount(
<RouterContext.Provider value={router}>
<Servers title="Servers" />
</RouterContext.Provider>);
expect(wrapper.find(BlankState)).toHaveLength(1); //it breaks here with the act message
}); |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
nathanmrtns
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case anybody else finds the same problem, here is my solution.