Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unit test TanStack's queryClient

I'm trying to unit test Tanstack's queryClient and I am able to test queries and components, but not sure how I would go about testing the queryCache and mutationCache inside of the queryClient and to be quite honest, I don't have a clue as to where to start. My test suite is pinging the lines that have comments below.

`const queryClient = new QueryClient({
   defaultOptions: {},
   queryCache: new QueryCache({
     onError: (error) => handleError(error) //line to test
   }),
   mutationCache: new mutationCache({
     onError: (error) => handleError(error) //line to test 
   }),
 })`

Nothing, I don't even know where to start and I'm getting paralysis. Would I need to create a nock query and run an error there?

like image 383
beeboop Avatar asked May 01 '26 01:05

beeboop


1 Answers

Don't test the library. It has its own tests. Instead you should test your code to ensure that your handleError function behaves the way you expect, not that it's called correctly by tanstack-query

like image 79
Chad S. Avatar answered May 04 '26 20:05

Chad S.