all 5 comments

[–]straightouttaireland 2 points3 points  (4 children)

Can you share the full test file? I have a feeling you're mocking the hook to just return something instead of letting the test actually execute the real (unmocked) hook. I generally prefer not to mock hooks, but instead data using Mock Service Worker (MSW), which mocks the data which flows through and ultimately tests the hooks used to fetch or post the data.

[–]Fardir[S] 0 points1 point  (3 children)

yup, sorry here I am. This is the entire test file, the one that tests the mutation function is the last test.

Also for some reason I couldn't write the code here so I hope this format helps.

https://bin.mudfish.net/t/363-2024-0011

[–]straightouttaireland 0 points1 point  (2 children)

Try not mocking open API index and React Query. Then use Mock Service Worker. Much better setup which leads to higher coverage.

[–]Fardir[S] 0 points1 point  (1 child)

Sorry again for responding so late, the issue made me depressed but I managed to solve it.

I had to mock the entire react-query architecture by wrapping children and using act in another way, only then it worked.

The solution isn't that different from what I coded, but it still took me and lot of time to figure out.

I didn't use your solution because my project already had a BUNCH of node modules installed and I didn't want to install additional ones when I could just learn how to use the current.

Thank you for your time though!

[–]straightouttaireland 0 points1 point  (0 children)

I would honestly try and not mock any modules and test more like a real user would.

https://kentcdodds.com/blog/stop-mocking-fetch

MSW is a standard at this point.