Pre-populate QueryClient cache with Zod schema-valid data for tests. No fetch mocking needed #10871
Unanswered
gonll
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone, I wanted to share a small testing utility I built that pairs well
with TanStack Query projects already using Zod for schema validation.
The problem
Testing components that depend on queries usually means one of:
QueryClientProviderand mockingfetch/ MSWIf you're already using Zod to validate API responses, your schemas are the
source of truth, so why not generate test data directly from them?
What I built
zodmint/tanstack-querypre-populates aQueryClientcache synchronouslyusing your Zod schemas. No network, no fetch mock, no MSW required.
The client comes with test-friendly defaults (
retry: false,staleTime: Infinity,gcTime: Infinity) and every generated value is guaranteed to passschema.safeParse(v).success === true.There's also
mockQueryFnfor dropping intouseQuerydirectly, andmockInfiniteQueryClientfor infinite query testing with the v5{ pages, pageParams }shape.Install
npm install zodmint # @tanstack/query-core is the only peer dep (you already have it)Full docs: github.com/gonll/zodmint
Would love feedback, especially from anyone doing complex query testing patterns
I might have missed.
Beta Was this translation helpful? Give feedback.
All reactions