test(react-query/HydrationBoundary): add tests for 'enabled: false' and 'staleTime: Infinity' #9969
main fromrefs/pull/9969/head·merged by TkDodo 🎯 Changes#
Add test cases for HydrationBoundary to verify that queries with enabled: false or staleTime: Infinity do not trigger unnecessary refetches during hydration.
Tests Added#
should not refetch when query has enabled set to falseshould not refetch when query has staleTime set to Infinity
✅ 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
- Added two tests verifying hydration does not trigger refetches for queries that are disabled or have infinite stale time; confirms cached pre-hydrated data is shown and the query function is not invoked.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
Walkthrough#
Added two tests to HydrationBoundary.test.tsx verifying hydration does not trigger query refetches for queries with
enabled: falseand withstaleTime: Infinity, and that hydrated cached data is rendered.Changes#
Cohort / File(s) Summary Hydration boundary tests
packages/react-query/src/__tests__/HydrationBoundary.test.tsxAdded two test cases: one ensuring a query with enabled: falsedoes not call the query function during hydration, and another ensuring a query withstaleTime: Infinityis not refetched and displays hydrated cached data.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10–15 minutes
- Verify timers are advanced and restored correctly.
- Confirm mocks/spies assert zero invocations reliably.
- Ensure tests follow existing suite conventions and clean up state between tests.
Possibly related PRs#
- TanStack/query#9605 — Adds/adjusts tests in HydrationBoundary.test.tsx targeting hydration behavior and preventing unnecessary refetches.
Suggested reviewers#
- manudeli
Poem#
🐰 Hydrated cache, snug and still, No fetch woke it from the hill. Disabled flag and endless stale, Keep the data on the trail. Tests hop in — all calm, no quail. 🥕
Pre-merge checks and finishing touches#
❌ 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%. You can run @coderabbitai generate docstringsto improve docstring coverage.✅ Passed checks (2 passed)
Check name Status Explanation Title check ✅ Passed The title clearly and specifically describes the main change: adding tests for two edge cases in HydrationBoundary. Description check ✅ Passed The description follows the template with all required sections completed, including detailed changes, checklist items marked, and release impact specified. ✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
Comment `@coderabbitai help` to get the list of available commands and usage tips.View your CI Pipeline Execution ↗ for commit 9a2d30a2da873a6d1999d6a0f2b32ed4680fce5c
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 3m 54s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 28s View ↗
☁️ Nx Cloud last updated this comment at
2025-12-14 19:18:26UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9969@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9969@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9969@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9969@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9969@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9969@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9969@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9969@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9969@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9969@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9969@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9969@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9969@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9969@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9969@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9969@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9969@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9969@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9969@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9969commit:
9a2d30aActionable comments posted: 0
🧹 Nitpick comments (2)
packages/react-query/src/__tests__/HydrationBoundary.test.tsx (2)
484-511: Make the “must not refetch” assertion fail-fast by havingqueryFnthrow (and disable retries). Right nowvi.fn()returnsundefinedif invoked; a throw makes unintended calls immediately obvious (and avoids any retry noise).- const queryFn = vi.fn() + const queryFn = vi.fn(() => { + throw new Error('queryFn should not be called during hydration') + }) ... const { data } = useQuery({ queryKey: ['string'], queryFn, enabled: false, + retry: false, })
513-540: Same: consider makingqueryFnthrow (andretry: false) to harden the test.- const queryFn = vi.fn() + const queryFn = vi.fn(() => { + throw new Error('queryFn should not be called during hydration') + }) ... const { data } = useQuery({ queryKey: ['string'], queryFn, staleTime: Infinity, + retry: false, })📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between f15b7fcc01e995ab8835f1b1cc82ebb472c1ff64 and 690603b70531acd565c645182e21cf7437a6340d.
📒 Files selected for processing (1)
packages/react-query/src/__tests__/HydrationBoundary.test.tsx(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-22T09:06:05.219Z
Learnt from: sukvvon Repo: TanStack/query PR: 9892 File: packages/solid-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx:331-335 Timestamp: 2025-11-22T09:06:05.219Z Learning: In TanStack/query test files, when a queryFn contains side effects (e.g., setting flags for test verification), prefer async/await syntax for clarity; when there are no side effects, prefer the .then() pattern for conciseness.Applied to files:
packages/react-query/src/__tests__/HydrationBoundary.test.tsx
🧬 Code graph analysis (1)
packages/react-query/src/__tests__/HydrationBoundary.test.tsx (1)
packages/react-query/src/HydrationBoundary.tsx (1)
HydrationBoundary(25-111)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Test
Codecov Report#
:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 83.64%. Comparing base (
f15b7fc) to head (9a2d30a). :warning: Report is 10 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9969 +/- ## =========================================== + Coverage 45.89% 83.64% +37.75% =========================================== Files 200 26 -174 Lines 8437 373 -8064 Branches 1943 108 -1835 =========================================== - Hits 3872 312 -3560 + Misses 4116 52 -4064 + Partials 449 9 -440Components Coverage Δ @tanstack/angular-query-experimental ∅ <ø> (∅)@tanstack/eslint-plugin-query ∅ <ø> (∅)@tanstack/query-async-storage-persister ∅ <ø> (∅)@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)@tanstack/query-codemods ∅ <ø> (∅)@tanstack/query-core ∅ <ø> (∅)@tanstack/query-devtools ∅ <ø> (∅)@tanstack/query-persist-client-core ∅ <ø> (∅)@tanstack/query-sync-storage-persister ∅ <ø> (∅)@tanstack/query-test-utils ∅ <ø> (∅)@tanstack/react-query 96.01% <ø> (ø)@tanstack/react-query-devtools 9.25% <ø> (ø)@tanstack/react-query-next-experimental ∅ <ø> (∅)@tanstack/react-query-persist-client 100.00% <ø> (ø)@tanstack/solid-query ∅ <ø> (∅)@tanstack/solid-query-devtools ∅ <ø> (∅)@tanstack/solid-query-persist-client ∅ <ø> (∅)@tanstack/svelte-query ∅ <ø> (∅)@tanstack/svelte-query-devtools ∅ <ø> (∅)@tanstack/svelte-query-persist-client ∅ <ø> (∅)@tanstack/vue-query ∅ <ø> (∅)@tanstack/vue-query-devtools ∅ <ø> (∅):rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/react-query/src/__tests__/HydrationBoundary.test.tsx (2)
484-512: Make the “should not refetch” failure mode more explicit by using a queryFn that would clearly break if invoked. Right nowconst queryFn = vi.fn()returnsundefined; if a regression calls it, the test will still likely fail, but the failure can be noisier/less diagnostic. Consider making the mock throw (or return a promise) so any unexpected invocation is unambiguous.Example tweak:
- const queryFn = vi.fn() + const queryFn = vi.fn(() => { + throw new Error('queryFn should not be called during hydration') + })(Also matches the repo preference of keeping queryFns concise when they have no intended side effects. Based on learnings, prefer
.then()when/if you do introduce async behavior.)
514-542: Consider DRYing the two new tests (same setup + assertions) to reduce future drift. These blocks are nearly identical except forenabled: falsevsstaleTime: Infinity. A tiny helper likerenderHydratedPage({ enabled, staleTime })(or a table-driven test) would keep intent clear and cut duplication.📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 0a3e7e2933d0176cae7c5f6f5f078c2c6dc545ec and 9a2d30a2da873a6d1999d6a0f2b32ed4680fce5c.
📒 Files selected for processing (1)
packages/react-query/src/__tests__/HydrationBoundary.test.tsx(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-11-22T09:06:05.219Z
Learnt from: sukvvon Repo: TanStack/query PR: 9892 File: packages/solid-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx:331-335 Timestamp: 2025-11-22T09:06:05.219Z Learning: In TanStack/query test files, when a queryFn contains side effects (e.g., setting flags for test verification), prefer async/await syntax for clarity; when there are no side effects, prefer the .then() pattern for conciseness.Applied to files:
packages/react-query/src/__tests__/HydrationBoundary.test.tsx
🧬 Code graph analysis (1)
packages/react-query/src/__tests__/HydrationBoundary.test.tsx (1)
packages/react-query/src/HydrationBoundary.tsx (1)
HydrationBoundary(25-111)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
TkDodo merged this2be25ca
TkDodo closed this
sukvvon(GitHub profile, opens in a new tab) assignedsukvvon
Sign in to join this conversation.
⚠️ No Changeset found#
Latest commit: 9a2d30a2da873a6d1999d6a0f2b32ed4680fce5c
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR