Skip to content
Source
Sign inSign up
TanStack/query

test(react-query/HydrationBoundary): add tests for 'non-object' state and missing 'queries' property handling #9605

Mergedsukvvon(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/9605/headmerged by manudeli(GitHub profile, opens in a new tab)

AS-IS

image

TO-BE

image

Summary by CodeRabbit

  • Tests
    • Added test coverage to ensure hydration is skipped when the provided state is not an object.
    • Added test coverage to confirm graceful handling when hydration state lacks a queries property.
    • Verifies no unintended hydration calls occur with invalid or incomplete state, improving reliability and preventing edge-case regressions.
    1. Walkthrough

      Adds two tests to HydrationBoundary.test.tsx to ensure no hydration occurs when provided state is non-object or lacks a queries property, using a mocked hydrate call and timer advancement within a QueryClientProvider setup.

      Changes

      Cohort / File(s)Summary of edits
      Hydration invalid-state tests
      packages/react-query/src/__tests__/HydrationBoundary.test.tsx
      Added two tests: (1) prevents hydration when state is a non-object, (2) prevents hydration when state is an empty object; spies on core hydrate, advances timers, asserts no invocation.

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~10 minutes

      Poem

      I thump my paws at midnight’s gate, Two tests hop in to validate— If state’s askew or queries thin, No hydrate call shall scurry in. I nibble greens, the suite is tight, Burrow secured, all checks alight. 🐇✨

      Tip

      🔌 Remote MCP (Model Context Protocol) integration is now available!

      Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

      ✨ Finishing Touches
      • 📝 Generate Docstrings
        🧪 Generate unit tests
      • Create PR with unit tests
      • Post copyable unit tests in a comment

      🪧 Tips

      Chat

      There are 3 ways to chat with CodeRabbit:

      • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
        • I pushed a fix in commit <commit_id>, please review it.
        • Open a follow-up GitHub issue for this discussion.
      • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
      • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
        • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
        • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

      Support

      Need help? Create a ticket on our support page for assistance with any issues or questions.

      CodeRabbit Commands (Invoked using PR/Issue comments)

      Type @coderabbitai help to get the list of available commands.

      Other keywords and placeholders

      • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
      • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
      • Add @coderabbitai anywhere in the PR title to generate the title automatically.

      CodeRabbit Configuration File (.coderabbit.yaml)

      • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
      • Please see the configuration documentation for more information.
      • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

      Status, Documentation and Community

      • Visit our Status Page to check the current availability of CodeRabbit.
      • Visit our Documentation for detailed information on how to use CodeRabbit.
      • Join our Discord Community to get help, request features, and share feedback.
      • Follow us on X/Twitter for updates and announcements.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query

    3. View your CI Pipeline Execution ↗ for commit 1fe93b6c5bea85ba017adc629c0e1b09a1661630

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

      ☁️ Nx Cloud last updated this comment at 2025-09-02 08:24:17 UTC

    4. More templates

      @tanstack/angular-query-devtools-experimental

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

      @tanstack/angular-query-experimental

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

      @tanstack/eslint-plugin-query

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

      @tanstack/query-async-storage-persister

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

      @tanstack/query-broadcast-client-experimental

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

      @tanstack/query-core

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

      @tanstack/query-devtools

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

      @tanstack/query-persist-client-core

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

      @tanstack/query-sync-storage-persister

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

      @tanstack/react-query

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

      @tanstack/react-query-devtools

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

      @tanstack/react-query-next-experimental

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

      @tanstack/react-query-persist-client

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

      @tanstack/solid-query

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

      @tanstack/solid-query-devtools

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

      @tanstack/solid-query-persist-client

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

      @tanstack/svelte-query

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

      @tanstack/svelte-query-devtools

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

      @tanstack/svelte-query-persist-client

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

      @tanstack/vue-query

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

      @tanstack/vue-query-devtools

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

      commit: 1fe93b6

    5. Actionable comments posted: 0

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

      365-387: Minor cleanups for clarity and resilience

      • Prefer expect(hydrateSpy).not.toHaveBeenCalled() over toHaveBeenCalledTimes(0).
      • vi.advanceTimersByTimeAsync(0) isn’t needed; hydration would be synchronous if it happened.
      • Use try/finally to always restore the spy and clear the client even if an assertion fails.
      • Optional: rename the second test to be more explicit, e.g., “should not hydrate queries if state.queries is missing”.

      Apply diffs:

      @@
      -  test('should not hydrate queries if state is not an object', async () => {
      +  test('should not hydrate queries if state is not an object', async () => {
           const queryClient = new QueryClient()
       
           const hydrateSpy = vi.spyOn(coreModule, 'hydrate')
       
      -    function Page() {
      -      return null
      -    }
      -
      -    render(
      -      <QueryClientProvider client={queryClient}>
      -        <HydrationBoundary state={'invalid-state' as any}>
      -          <Page />
      -        </HydrationBoundary>
      -      </QueryClientProvider>,
      -    )
      -
      -    await vi.advanceTimersByTimeAsync(0)
      -    expect(hydrateSpy).toHaveBeenCalledTimes(0)
      -
      -    hydrateSpy.mockRestore()
      -    queryClient.clear()
      +    try {
      +      function Page() {
      +        return null
      +      }
      +      render(
      +        <QueryClientProvider client={queryClient}>
      +          <HydrationBoundary state={'invalid-state' as any}>
      +            <Page />
      +          </HydrationBoundary>
      +        </QueryClientProvider>,
      +      )
      +      expect(hydrateSpy).not.toHaveBeenCalled()
      +    } finally {
      +      hydrateSpy.mockRestore()
      +      queryClient.clear()
      +    }
         })
      @@
      -  test('should handle state without queries property gracefully', async () => {
      +  test('should not hydrate queries if state.queries is missing', async () => {
           const queryClient = new QueryClient()
       
           const hydrateSpy = vi.spyOn(coreModule, 'hydrate')
       
      -    function Page() {
      -      return null
      -    }
      -
      -    render(
      -      <QueryClientProvider client={queryClient}>
      -        <HydrationBoundary state={{} as any}>
      -          <Page />
      -        </HydrationBoundary>
      -      </QueryClientProvider>,
      -    )
      -
      -    await vi.advanceTimersByTimeAsync(0)
      -    expect(hydrateSpy).toHaveBeenCalledTimes(0)
      -
      -    hydrateSpy.mockRestore()
      -    queryClient.clear()
      +    try {
      +      function Page() {
      +        return null
      +      }
      +      render(
      +        <QueryClientProvider client={queryClient}>
      +          <HydrationBoundary state={{} as any}>
      +            <Page />
      +          </HydrationBoundary>
      +        </QueryClientProvider>,
      +      )
      +      expect(hydrateSpy).not.toHaveBeenCalled()
      +    } finally {
      +      hydrateSpy.mockRestore()
      +      queryClient.clear()
      +    }
         })

      Also applies to: 389-411

      📜 Review details

      Configuration used: CodeRabbit UI

      Review profile: CHILL

      Plan: Pro

      💡 Knowledge Base configuration:

      • MCP integration is disabled by default for public repositories
      • Jira integration is disabled by default for public repositories
      • Linear integration is disabled by default for public repositories

      You can enable these sources in your CodeRabbit configuration.

      📥 Commits

      Reviewing files that changed from the base of the PR and between b998f68a57e4cefbb3b353e72f118c8607f4f77d and 4604258f3f5fddd7240589e0191e5d3e16193f6a.

      📒 Files selected for processing (1)
      • packages/react-query/src/__tests__/HydrationBoundary.test.tsx (1 hunks)
      🧰 Additional context used
      🧬 Code graph analysis (1)
      packages/react-query/src/__tests__/HydrationBoundary.test.tsx (1)
      packages/react-query/src/HydrationBoundary.tsx (1)
      • HydrationBoundary (25-108)
      ⏰ 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). (3)
      • GitHub Check: autofix
      • GitHub Check: Preview
      • GitHub Check: Test
      🔇 Additional comments (2)
      packages/react-query/src/__tests__/HydrationBoundary.test.tsx (2)

      365-387: LGTM: Correctly asserts no hydration for non-object state

      This matches HydrationBoundary’s guard (typeof state !== 'object' → early return), so hydrate should never be called.


      389-411: LGTM: Covers missing queries property path

      State fallback to queries || [] means no hydration; this test validates that path.

    6. Codecov Report

      :white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 84.51%. Comparing base (c44ca47) to head (1fe93b6). :warning: Report is 2 commits behind head on main.

      Additional details and impacted files

      Impacted file tree graph

      @@             Coverage Diff             @@
      ##             main    #9605       +/-   ##
      ===========================================
      + Coverage   45.21%   84.51%   +39.29%     
      ===========================================
        Files         208       26      -182     
        Lines        8335      368     -7967     
        Branches     1889      108     -1781     
      ===========================================
      - Hits         3769      311     -3458     
      + Misses       4119       48     -4071     
      + Partials      447        9      -438     
      ComponentsCoverage Δ
      @tanstack/angular-query-devtools-experimental∅ <ø> (∅)
      @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.00% <ø> (+0.33%):arrow_up:
      @tanstack/react-query-devtools10.00% <ø> (ø)
      @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.
  • manudeli(GitHub profile, opens in a new tab) approved these changes

  • Sign in to join this conversation.