test(react-query/HydrationBoundary): add tests for 'non-object' state and missing 'queries' property handling #9605
main fromrefs/pull/9605/head·merged by manudeli(GitHub profile, opens in a new tab) AS-IS#
TO-BE#
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
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
View your CI Pipeline Execution ↗ for commit 1fe93b6c5bea85ba017adc629c0e1b09a1661630
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 2m 48s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 27s View ↗
☁️ Nx Cloud last updated this comment at
2025-09-02 08:24:17UTCMore 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-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-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-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-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@9605commit:
1fe93b6Actionable 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()overtoHaveBeenCalledTimes(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 stateThis matches HydrationBoundary’s guard (typeof state !== 'object' → early return), so hydrate should never be called.
389-411: LGTM: Covers missingqueriesproperty pathState fallback to
queries || []means no hydration; this test validates that path.- Prefer
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
@@ 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- :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
manudeli(GitHub profile, opens in a new tab) closed this
sukvvon(GitHub profile, opens in a new tab) assignedsukvvon
Sign in to join this conversation.
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#
packages/react-query/src/__tests__/HydrationBoundary.test.tsxEstimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
Poem#
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 unit tests
🪧 Tips
Chat#
There are 3 ways to chat with CodeRabbit:
I pushed a fix in commit <commit_id>, please review it.Open a follow-up GitHub issue for this discussion.@coderabbitaiin a new review comment at the desired location with your query.@coderabbitaiin 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 helpto get the list of available commands.Other keywords and placeholders#
@coderabbitai ignoreor@coderabbit ignoreanywhere in the PR description to prevent this PR from being reviewed.@coderabbitai summaryto generate the high-level summary at a specific location in the PR description.@coderabbitaianywhere in the PR title to generate the title automatically.CodeRabbit Configuration File (
.coderabbit.yaml)#.coderabbit.yamlfile to the root of your repository.# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.jsonStatus, Documentation and Community#