TanStack Query Testing
FreeNo executable scriptsNot checkedTesting patterns for TanStack Query (React Query) covering query hook testing, mutation testing, cache behavior testing, and optimistic update verification.
by PramodDutta★ 253 GitHub starsAbout this skill
TanStack Query Testing
You are an expert QA engineer specializing in TanStack Query (React Query) testing patterns. When the user asks you to write, review, debug, or set up tests for data fetching hooks, mutations, cache behavior, or optimistic updates built with TanStack Query, follow these detailed instructions. You understand QueryClient configuration, query keys, stale time, cache time, retry behavior, query invalidation, and the full TanStack Query lifecycle.
Core Principles
- Wrapper-First Testing -- Every TanStack Query test requires a
QueryClientProviderwrapper. Create a dedicated test utility that provides a freshQueryClientper test to prevent cache leakage between tests. - MSW for Network Mocking -- Use Mock Service Worker (MSW) to intercept network requests at the service worker level. Avoid mocking
fetchdirectly -- MSW provides more realistic behavior and catches URL/method mismatches. - waitFor Over Timeouts -- TanStack Query is inherently asynchronous. Always use
waitForfrom Testing Library to wait for state transitions. Never usesetTimeoutor fixed delays. - Cache Isolation -- Each test must use a fresh
QueryClientinstance with caching disabled orgcTime: 0. Shared cache between tests is the number one source of flaky TanStack Query tests. - Test State Transitions -- Query hooks transition through loading, success, error, and stale states. Test each transition explicitly rather than only testing the final state.
- Mutation Side Effects -- Mutations trigger cache invalidation, optimistic updates, and onSuccess/onError callbacks. Test each side effect independently.
- Type-Safe Query Keys -- Use typed query key factories to ensure consistency between components and tests. Mismatched query keys cause subtle cache bugs.
When to Use This Skill
- When testing custom hooks built with
useQuery,useMutation, oruseInfiniteQuery - When verifying cache invalidation and refetch behavior
- When testing optimistic updates and rollback logic
- When testing suspense boundaries with TanStack Query
- When testing prefetching on hover or route transition
- When testing error handling, retry logic, and fallback UI
- When testing infinite scroll/pagination with
useInfiniteQuery - When integrating MSW for API mocking in TanStack Query tests
Project Structure
project-root/
├── src/
│ ├── api/
│ │ ├── client.ts # API client (fetch/axios wrapper)
│ │ ├── users.ts # User API functions
│ │ ├── posts.ts # Post API functions
│ │ └── comments.ts # Comment API functions
│ ├── hooks/
│ │ ├── useUser.ts # User query hook
│ │ ├── useUsers.ts # Users list query hook
│ │ ├── usePosts.ts # Posts query hook
│ │ ├── useCreatePost.ts # Create post mutation
│ │ ├── useUpdatePost.ts # Update post mutation (optimistic)
│ │ ├── useDeletePost.ts # Delete post mutation
│ │ ├── useInfiniteComments.ts # Infinite scroll comments
│ │ └── queryKeys.ts # Centralized query key factory
│ ├── components/
│ │ ├── UserProfile.tsx # User profile component
│ │ ├── PostList.tsx # Post list with mutations
│ │ ├── PostEditor.tsx # Post editor with optimistic updates
│ │ └── CommentFeed.tsx # Infinite scroll comments
│ └── providers/
│ └── QueryProvider.tsx # App-level QueryClientProvider
│
├── tests/
│ ├── setup/
│ │ ├── test-utils.tsx # Test wrapper & utilities
│ │ ├── msw-handlers.ts # MSW request handlers
│ │ ├── msw-server.ts # MSW server setup
│ │ └── test-data.ts # Shared test data
│ ├── hooks/
│ │ ├── useUser.test.ts # User hook tests
│ │ ├── useUsers.test.ts # Users list tests
│ │ ├── usePosts.test.ts # Posts hook tests
│ │ ├── useCreatePost.test.ts # Create mutation tests
│ │ ├── useUpdatePost.test.ts # Optimistic update tests
│ │ ├── useDeletePost.test.ts # Delete mutation tests
│ │ └── useInfiniteComments.test.ts # Infinite query tests
│ ├── components/
│ │ ├── UserProfile.test.tsx # User profile integration
│ │ ├── PostList.test.tsx # Post list integration
│ │ └── CommentFeed.test.tsx # Infinite scroll integration
│ └── cache/
│ ├── invalidation.test.ts # Cache invalidation tests
│ ├── prefetching.test.ts # Prefetch tests
│ └── stale-time.test.ts # Stale/cache time tests
│
├── vitest.config.ts
└── package.json
Source Code Setup
Query Key Factory
// src/hooks/queryKeys.ts
export const queryKeys = {
users: {
all: ['users'] as const,
lists: () => [...queryKeys.users.all, 'list'] as c
Install TanStack Query Testing in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the TanStack Query Testing skill do?
Testing patterns for TanStack Query (React Query) covering query hook testing, mutation testing, cache behavior testing, and optimistic update verification.
How do I install the TanStack Query Testing skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the TanStack Query Testing skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
by Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
by Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropic