Skip to content
Source
Sign inSign up
TanStack/query

test(react-query/HydrationBoundary): add tests for 'enabled: false' and 'staleTime: Infinity' #9969

Mergedsukvvon(GitHub profile, opens in a new tab)merged 4 commits intomain fromrefs/pull/9969/headmerged 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 false
  • should 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.
✏️ Tip: You can customize this high-level summary in your review settings.
    1. ⚠️ 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

    2. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query

    3. Walkthrough

      Added two tests to HydrationBoundary.test.tsx verifying hydration does not trigger query refetches for queries with enabled: false and with staleTime: Infinity, and that hydrated cached data is rendered.

      Changes

      Cohort / File(s)Summary
      Hydration boundary tests
      packages/react-query/src/__tests__/HydrationBoundary.test.tsx
      Added two test cases: one ensuring a query with enabled: false does not call the query function during hydration, and another ensuring a query with staleTime: Infinity is 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.
      • 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 nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.You can run @coderabbitai generate docstrings to improve docstring coverage.
      ✅ Passed checks (2 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and specifically describes the main change: adding tests for two edge cases in HydrationBoundary.
      Description check✅ PassedThe 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.
    4. View your CI Pipeline Execution ↗ for commit 9a2d30a2da873a6d1999d6a0f2b32ed4680fce5c

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded3m 54sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded28sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-12-14 19:18:26 UTC

    5. More templates

      @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@9969

      commit: 9a2d30a

    6. Actionable 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 having queryFn throw (and disable retries). Right now vi.fn() returns undefined if 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 making queryFn throw (and retry: 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
    7. 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

      Impacted file tree graph

      @@             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      -440     
      ComponentsCoverage Δ
      @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-query96.01% <ø> (ø)
      @tanstack/react-query-devtools9.25% <ø> (ø)
      @tanstack/react-query-next-experimental∅ <ø> (∅)
      @tanstack/react-query-persist-client100.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 now const queryFn = vi.fn() returns undefined; 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 for enabled: false vs staleTime: Infinity. A tiny helper like renderHydratedPage({ 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

  • Sign in to join this conversation.