Skip to content

test({react,preact,solid}-query): move 'queryClient' and 'queryCache' to 'beforeEach' for per-test isolation#10356

Merged
sukvvon merged 7 commits intomainfrom
test/react-preact-query-move-queryclient-to-beforeeach
Mar 31, 2026
Merged

test({react,preact,solid}-query): move 'queryClient' and 'queryCache' to 'beforeEach' for per-test isolation#10356
sukvvon merged 7 commits intomainfrom
test/react-preact-query-move-queryclient-to-beforeeach

Conversation

@sukvvon
Copy link
Copy Markdown
Collaborator

@sukvvon sukvvon commented Mar 30, 2026

🎯 Changes

Move queryClient and queryCache from describe-level constants to beforeEach/afterEach for per-test isolation across react-query, preact-query, and solid-query test files.

Files changed:

  • {react,preact}-query: QueryResetErrorBoundary, fine-grained-persister, ssr, suspense, useInfiniteQuery, useMutation, usePrefetchInfiniteQuery, usePrefetchQuery, useQueries, useQuery, useSuspenseInfiniteQuery, useSuspenseQuery
  • solid-query: suspense, transition, useInfiniteQuery, useMutation, useQueries, useQuery

Each test now gets a fresh QueryClient and QueryCache instance, with queryClient.clear() called in afterEach for explicit cleanup.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Tests
    • Improved isolation by creating fresh query cache/client per test and moving initialization into beforeEach across react-query, preact-query, and solid-query suites.
    • Centralized teardown: afterEach now restores timers then clears client state to prevent cross-test leakage and improve reliability.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 93e6a9d2-eae6-4fc9-8473-e1b0d62c47af

📥 Commits

Reviewing files that changed from the base of the PR and between 8fbe92e and 57ec1bc.

📒 Files selected for processing (2)
  • packages/preact-query/src/__tests__/ssr.test.tsx
  • packages/react-query/src/__tests__/ssr.test.tsx

📝 Walkthrough

Walkthrough

Moved test-level QueryCache/QueryClient initialization into beforeEach across preact/react/solid query packages and centralized cleanup by calling queryClient.clear() in afterEach (after restoring real timers) to ensure per-test client/cache isolation.

Changes

Cohort / File(s) Summary
Preact — Per-test setup & cleanup
packages/preact-query/src/__tests__/QueryResetErrorBoundary.test.tsx, packages/preact-query/src/__tests__/fine-grained-persister.test.tsx, packages/preact-query/src/__tests__/useInfiniteQuery.test.tsx, packages/preact-query/src/__tests__/useQueries.test.tsx, packages/preact-query/src/__tests__/useSuspenseInfiniteQuery.test.tsx, packages/preact-query/src/__tests__/useSuspenseQuery.test.tsx
Replaced module/describe-scope const queryCache/queryClient with let variables initialized in beforeEach; added queryClient.clear() in afterEach.
Preact — Cleanup additions
packages/preact-query/src/__tests__/ssr.test.tsx, packages/preact-query/src/__tests__/suspense.test.tsx, packages/preact-query/src/__tests__/useMutation.test.tsx, packages/preact-query/src/__tests__/usePrefetchInfiniteQuery.test.tsx, packages/preact-query/src/__tests__/usePrefetchQuery.test.tsx, packages/preact-query/src/__tests__/useQuery.test.tsx
Added queryClient.clear() to afterEach (executed after vi.useRealTimers()), centralizing client/cache cleanup.
React — Per-test setup & cleanup
packages/react-query/src/__tests__/QueryResetErrorBoundary.test.tsx, packages/react-query/src/__tests__/fine-grained-persister.test.tsx, packages/react-query/src/__tests__/useInfiniteQuery.test.tsx, packages/react-query/src/__tests__/useQueries.test.tsx, packages/react-query/src/__tests__/useSuspenseInfiniteQuery.test.tsx, packages/react-query/src/__tests__/useSuspenseQuery.test.tsx
Moved queryCache/queryClient to let variables and instantiate in beforeEach; added queryClient.clear() in afterEach.
React — Cleanup additions
packages/react-query/src/__tests__/ssr.test.tsx, packages/react-query/src/__tests__/suspense.test.tsx, packages/react-query/src/__tests__/useMutation.test.tsx, packages/react-query/src/__tests__/usePrefetchInfiniteQuery.test.tsx, packages/react-query/src/__tests__/usePrefetchQuery.test.tsx, packages/react-query/src/__tests__/useQuery.test.tsx
Centralized cache/client clearing by calling queryClient.clear() in afterEach (after restoring timers); removed per-test queryCache.clear() calls.
Solid — Per-test setup & cleanup
packages/solid-query/src/__tests__/suspense.test.tsx, packages/solid-query/src/__tests__/transition.test.tsx, packages/solid-query/src/__tests__/useInfiniteQuery.test.tsx, packages/solid-query/src/__tests__/useQueries.test.tsx, packages/solid-query/src/__tests__/useQuery.test.tsx
Switched from describe-scope const queryCache/queryClient to let variables created in beforeEach; added queryClient.clear() in afterEach.
Solid — Mutation caches per-test
packages/solid-query/src/__tests__/useMutation.test.tsx
Refactored QueryCache, MutationCache, and QueryClient to per-test let variables initialized in beforeEach and added queryClient.clear() in afterEach.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I hopped through tests at break of day,

Fresh caches planted, old crumbs brushed away.
Each test a burrow, tidy and neat,
No lingering crumbs beneath my feet.
Hooray for clean runs—more carrots to eat! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: moving queryClient and queryCache to beforeEach for per-test isolation across multiple query libraries.
Description check ✅ Passed The description covers the key aspects: what changed, which files were affected, the technical approach, and release impact. However, contributing guide and local testing checklist items remain unchecked.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/react-preact-query-move-queryclient-to-beforeeach

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Mar 30, 2026

View your CI Pipeline Execution ↗ for commit 57ec1bc

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 2m 24s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-31 01:41:36 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 30, 2026

🚀 Changeset Version Preview

No changeset entries found. Merging this PR will not cause a version bump for any packages.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 30, 2026

More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10356

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10356

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@10356

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10356

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10356

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10356

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10356

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@10356

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@10356

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10356

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10356

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@10356

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@10356

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10356

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10356

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@10356

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10356

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10356

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@10356

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10356

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10356

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@10356

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10356

commit: 57ec1bc

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 30, 2026

size-limit report 📦

Path Size
react full 11.98 KB (0%)
react minimal 9.01 KB (0%)

@sukvvon sukvvon changed the title test({react,preact}-query): move 'queryClient' and 'queryCache' to 'beforeEach' for per-test isolation test({react,preact,solid}-query): move 'queryClient' and 'queryCache' to 'beforeEach' for per-test isolation Mar 30, 2026
@sukvvon sukvvon self-assigned this Mar 30, 2026
@sukvvon sukvvon marked this pull request as ready for review March 31, 2026 01:24
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
packages/react-query/src/__tests__/ssr.test.tsx (1)

30-33: Consider removing redundant queryCache.clear() calls within individual tests.

With queryClient.clear() now called in afterEach, the individual queryCache.clear() calls at the end of each test case (lines 60, 77, 108, 140, 179, 210, 252, 272, 290) are redundant since QueryClient.clear() already invokes queryCache.clear() internally. Removing these would improve code clarity and consistency with other test files in this PR.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-query/src/__tests__/ssr.test.tsx` around lines 30 - 33, Remove
the redundant queryCache.clear() calls at the end of individual tests because
afterEach already calls queryClient.clear(), which internally clears the cache;
search for and delete calls to queryCache.clear() in the ssr.test.tsx tests
(they appear after individual it/test blocks) and rely on the existing afterEach
that calls queryClient.clear() and vi.useRealTimers().
packages/preact-query/src/__tests__/ssr.test.tsx (1)

28-31: Consider removing redundant queryCache.clear() calls within individual tests.

Since queryClient.clear() in afterEach already invokes queryCache.clear() internally (as shown in QueryClient.clear() implementation at packages/query-core/src/queryClient.ts:644-648), the individual queryCache.clear() calls at the end of each test (lines 58, 75, 106, 138, 156, 195) are now redundant and can be removed for cleaner test code.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/preact-query/src/__tests__/ssr.test.tsx` around lines 28 - 31, The
test suite already calls queryClient.clear() in the afterEach hook (via
afterEach(() => { vi.useRealTimers(); queryClient.clear(); })), which internally
calls queryCache.clear(); remove the redundant explicit queryCache.clear() calls
found at the end of individual tests (the trailing queryCache.clear()
invocations within the tests) so the cleanup is handled centrally by
queryClient.clear(); keep the afterEach hook as-is and delete only the per-test
queryCache.clear() calls.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/preact-query/src/__tests__/ssr.test.tsx`:
- Around line 28-31: The test suite already calls queryClient.clear() in the
afterEach hook (via afterEach(() => { vi.useRealTimers(); queryClient.clear();
})), which internally calls queryCache.clear(); remove the redundant explicit
queryCache.clear() calls found at the end of individual tests (the trailing
queryCache.clear() invocations within the tests) so the cleanup is handled
centrally by queryClient.clear(); keep the afterEach hook as-is and delete only
the per-test queryCache.clear() calls.

In `@packages/react-query/src/__tests__/ssr.test.tsx`:
- Around line 30-33: Remove the redundant queryCache.clear() calls at the end of
individual tests because afterEach already calls queryClient.clear(), which
internally clears the cache; search for and delete calls to queryCache.clear()
in the ssr.test.tsx tests (they appear after individual it/test blocks) and rely
on the existing afterEach that calls queryClient.clear() and vi.useRealTimers().

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f27c74bf-54fa-46c1-8561-44958aa30bbe

📥 Commits

Reviewing files that changed from the base of the PR and between 7e5fe21 and 8fbe92e.

📒 Files selected for processing (30)
  • packages/preact-query/src/__tests__/QueryResetErrorBoundary.test.tsx
  • packages/preact-query/src/__tests__/fine-grained-persister.test.tsx
  • packages/preact-query/src/__tests__/ssr.test.tsx
  • packages/preact-query/src/__tests__/suspense.test.tsx
  • packages/preact-query/src/__tests__/useInfiniteQuery.test.tsx
  • packages/preact-query/src/__tests__/useMutation.test.tsx
  • packages/preact-query/src/__tests__/usePrefetchInfiniteQuery.test.tsx
  • packages/preact-query/src/__tests__/usePrefetchQuery.test.tsx
  • packages/preact-query/src/__tests__/useQueries.test.tsx
  • packages/preact-query/src/__tests__/useQuery.test.tsx
  • packages/preact-query/src/__tests__/useSuspenseInfiniteQuery.test.tsx
  • packages/preact-query/src/__tests__/useSuspenseQuery.test.tsx
  • packages/react-query/src/__tests__/QueryResetErrorBoundary.test.tsx
  • packages/react-query/src/__tests__/fine-grained-persister.test.tsx
  • packages/react-query/src/__tests__/ssr.test.tsx
  • packages/react-query/src/__tests__/suspense.test.tsx
  • packages/react-query/src/__tests__/useInfiniteQuery.test.tsx
  • packages/react-query/src/__tests__/useMutation.test.tsx
  • packages/react-query/src/__tests__/usePrefetchInfiniteQuery.test.tsx
  • packages/react-query/src/__tests__/usePrefetchQuery.test.tsx
  • packages/react-query/src/__tests__/useQueries.test.tsx
  • packages/react-query/src/__tests__/useQuery.test.tsx
  • packages/react-query/src/__tests__/useSuspenseInfiniteQuery.test.tsx
  • packages/react-query/src/__tests__/useSuspenseQuery.test.tsx
  • packages/solid-query/src/__tests__/suspense.test.tsx
  • packages/solid-query/src/__tests__/transition.test.tsx
  • packages/solid-query/src/__tests__/useInfiniteQuery.test.tsx
  • packages/solid-query/src/__tests__/useMutation.test.tsx
  • packages/solid-query/src/__tests__/useQueries.test.tsx
  • packages/solid-query/src/__tests__/useQuery.test.tsx

@sukvvon sukvvon merged commit f168555 into main Mar 31, 2026
8 checks passed
@sukvvon sukvvon deleted the test/react-preact-query-move-queryclient-to-beforeeach branch March 31, 2026 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant