What is the best place to instantiate new QueryClient()? #2007
-
The quick start guide shows this, where the const queryClient = new QueryClient()
// used to render app and tests
function App() {
return (
// Provide the client to your App
<QueryClientProvider client={queryClient}>
<Todos />
</QueryClientProvider>
)
}
it('one thing', () => {
render(<App />)
...
})
it('next thing', () => {
// cached queries from previous test are still here
render(<App />)
...
}) However, what if your tests should we instead instantiate in a hook that only runs once? function App() {
const queryClient = useMemo(() => new QueryClient(), [])
return (
// Provide the client to your App
<QueryClientProvider client={queryClient}>
<Todos />
</QueryClientProvider>
)
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I test my hooks with if you want to test a component, you can also make a thin wrapper: in your example, don't test the
side note: |
Beta Was this translation helpful? Give feedback.
-
We render the full app instead of a component most of the time because it is closer to how it's used, and will ultimately have less false negatives where tests pass but it's actually broken when used in a browser.
I've settled on this pattern, thanks everyone! @julionav ty. I considered this situation, but since the client will still sorta be shared between tests, ultimately decided against it although it should work fine. also @TkDodo that blog post was super useful. I saw that warning in the docs for |
Beta Was this translation helpful? Give feedback.
We render the full app instead of a component most of the time because it is closer to how it's used, and will ultimately have less false negatives where tests pass but it's actually broken when used in a browser.
I've settled on this pattern, thanks everyone!
@julionav ty. I considered this situation, but since the client will still sorta be shared between tests, ultimately decided against it although it should work fine.
also