T
TanStack•14mo ago
manual-pink

testing a query cache

Hey guys, I'm using a query with a stale time (I mean the cache is activated) and in my test I would like to check that the data from the cache is well retrieved. I have access to my queryClient in my test but I don't know what to use to get this assertion. For exemple I have my keys: ['tests', 1] and ['tests', 2] I start with ['tests', 1] and I expect the queryFn to be called then I swap to ['tests', 2] and I expect the queryFn to be called to finish I swap back to ['tests', 1] and expect get the data from the cache <-- that's what I need do you have any ideas?
3 Replies
vicious-gold
vicious-gold•14mo ago
Hi. It's not the answer to your question, but you shouldn't test a code that's not yours. RQ is a library. When you use a library you rely on its maintainers to test it and remove any bugs. The same way, you don't do all sorts of tests to test a newly bought smartphone. It's the responsibility of the manufacturer.
adverse-sapphire
adverse-sapphire•14mo ago
Agreed. This seems like something you should not be writing tests for. With that said, I would probably spy the queryFn and see that it was called the first time and was not called the second time (and data exists).
vicious-gold
vicious-gold•14mo ago
Sounds like a plan 🙂

Did you find this page helpful?