Skip to content

Comments

test(svelte-query/HydrationBoundary): add test for hydrating queries to the cache on context#10179

Open
sukvvon wants to merge 2 commits intoTanStack:mainfrom
sukvvon:test/svelte-query-HydrationBoundary
Open

test(svelte-query/HydrationBoundary): add test for hydrating queries to the cache on context#10179
sukvvon wants to merge 2 commits intoTanStack:mainfrom
sukvvon:test/svelte-query-HydrationBoundary

Conversation

@sukvvon
Copy link
Contributor

@sukvvon sukvvon commented Feb 24, 2026

🎯 Changes

Add a test for HydrationBoundary that verifies hydrated queries are available in the cache on context, improving coverage for HydrationBoundary.svelte (0% → 100%) and useHydrate.ts (0% → 100%).

The test follows the same pattern as the react-query HydrationBoundary.test.tsx 'should hydrate queries to the cache on context' test case.

✅ 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 tests covering hydration/dehydration flows for query state, verifying initial hydrated values and subsequent updates.
  • Documentation

    • Added an example Svelte component demonstrating how to use a hydration boundary with dehydrated query state and client initialization.

@changeset-bot
Copy link

changeset-bot bot commented Feb 24, 2026

⚠️ No Changeset found

Latest commit: 1afcfd1

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2026

📝 Walkthrough

Walkthrough

Adds a new Svelte test component and a test verifying hydration: a prefetched query is dehydrated, passed into a HydrationBoundary, initially renders cached data, then updates when the fresh query resolves.

Changes

Cohort / File(s) Summary
HydrationBoundary example & tests
packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte, packages/svelte-query/tests/HydrationBoundary/HydrationBoundary.svelte.test.ts
New Svelte component that wires a QueryClient and renders query data inside HydrationBoundary; new vitest test that prefills a query, dehydrates state, hydrates the component with that state, asserts initial cached render and subsequent update after timers advance.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant QueryClient
    participant DehydratedState
    participant BaseExample
    participant HydrationBoundary
    participant QueryFn

    Test->>QueryClient: prefetch(['string']) (resolves after 10ms)
    QueryClient-->>Test: dehydrate() -> DehydratedState
    Test->>BaseExample: render(props: {dehydratedState, queryFn})
    BaseExample->>QueryClient: create client & set context
    BaseExample->>HydrationBoundary: render(state=DehydratedState)
    HydrationBoundary->>QueryClient: restore cached data
    BaseExample->>QueryFn: fallback fetch (resolves after 20ms)
    Note over BaseExample,HydrationBoundary: initial render shows cached value
    Test->>Test: advance timers (21ms)
    QueryFn-->>BaseExample: fresh result
    BaseExample->>HydrationBoundary: update render with fresh data
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • manudeli
  • TkDodo

Poem

🐰
I hopped through queries, light and spry,
Carried caches in a blink of eye,
Hydration hummed, the old state shone,
Then fresh data hopped in—home sweet home!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a test for HydrationBoundary that verifies hydrated queries are available in the cache on context.
Description check ✅ Passed The description follows the required template with all key sections completed: Changes section explains the test and coverage improvements, Checklist section has items marked as completed, and Release Impact section correctly identifies it as dev-only.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 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.

@nx-cloud
Copy link

nx-cloud bot commented Feb 24, 2026

View your CI Pipeline Execution ↗ for commit 1afcfd1

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

☁️ Nx Cloud last updated this comment at 2026-02-24 05:07:19 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 24, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/preact-query

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: 1afcfd1

Copy link
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.

Actionable comments posted: 1

🧹 Nitpick comments (2)
packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte (2)

2-2: QueryClient is used only as a type — prefer import type.

Since QueryClient is only referenced in the type annotation, this should be a type-only import.

♻️ Suggested change
-  import { QueryClient } from '@tanstack/query-core'
+  import type { QueryClient } from '@tanstack/query-core'
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte` at line 2,
The import of QueryClient is used only as a type and should be a type-only
import; replace the value import "import { QueryClient } from
'@tanstack/query-core'" with a type import "import type { QueryClient } from
'@tanstack/query-core'" wherever QueryClient is used in type annotations (e.g.,
in the HydrationBoundary/BaseExample.svelte component), ensuring no runtime
import remains.

28-34: options={undefined} and queryClient={undefined} are redundant since both props have explicit undefined defaults on HydrationBoundary.

In Svelte 5, omitting optional props with undefined defaults is equivalent to explicitly passing undefined. These bindings add noise to the test fixture without adding clarity.

♻️ Suggested change
 <HydrationBoundary
   state={dehydratedState}
-  options={undefined}
-  queryClient={undefined}
 >
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte` around
lines 28 - 34, The HydrationBoundary usage redundantly passes
options={undefined} and queryClient={undefined} even though HydrationBoundary
already defaults those props to undefined; remove the explicit options and
queryClient bindings from the HydrationBoundary component in BaseExample.svelte
so it only passes state={dehydratedState} (and the children) to simplify the
test fixture while preserving behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@packages/svelte-query/tests/HydrationBoundary/HydrationBoundary.svelte.test.ts`:
- Around line 27-43: The test calls queryClient.clear() at the end of the test
body which will be skipped if an assertion throws; either wrap the test body in
a try/finally and call queryClient.clear() in finally, or refactor so
queryClient is declared in the surrounding scope (e.g. let queryClient;) and
assign it inside the test, then add an afterEach that calls queryClient.clear()
and vi.useRealTimers() to guarantee cleanup for the test "should hydrate queries
to the cache on context" (referencing queryClient.clear(), vi.useRealTimers(),
and the test block).

---

Nitpick comments:
In `@packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte`:
- Line 2: The import of QueryClient is used only as a type and should be a
type-only import; replace the value import "import { QueryClient } from
'@tanstack/query-core'" with a type import "import type { QueryClient } from
'@tanstack/query-core'" wherever QueryClient is used in type annotations (e.g.,
in the HydrationBoundary/BaseExample.svelte component), ensuring no runtime
import remains.
- Around line 28-34: The HydrationBoundary usage redundantly passes
options={undefined} and queryClient={undefined} even though HydrationBoundary
already defaults those props to undefined; remove the explicit options and
queryClient bindings from the HydrationBoundary component in BaseExample.svelte
so it only passes state={dehydratedState} (and the children) to simplify the
test fixture while preserving behavior.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb7dca5 and 8bc1d99.

📒 Files selected for processing (2)
  • packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte
  • packages/svelte-query/tests/HydrationBoundary/HydrationBoundary.svelte.test.ts

Comment on lines 27 to 43
it('should hydrate queries to the cache on context', async () => {
const dehydratedState = JSON.parse(stringifiedState)
const queryClient = new QueryClient()

const rendered = render(BaseExample, {
props: {
queryClient,
dehydratedState,
queryFn: () => sleep(20).then(() => 'string'),
},
})

expect(rendered.getByText('data: stringCached')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('data: string')).toBeInTheDocument()
queryClient.clear()
})
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

queryClient.clear() inside the test body won't run if an assertion fails.

If getByText('data: stringCached') or getByText('data: string') throws, cleanup is skipped. Move it to afterEach (alongside vi.useRealTimers()) or use a try/finally block.

🛡️ Proposed fix — move cleanup to afterEach
+  let queryClient: QueryClient
+
   afterEach(() => {
     vi.useRealTimers()
+    queryClient.clear()
   })

   it('should hydrate queries to the cache on context', async () => {
     const dehydratedState = JSON.parse(stringifiedState)
-    const queryClient = new QueryClient()
+    queryClient = new QueryClient()

     const rendered = render(BaseExample, {
       props: {
         queryClient,
         dehydratedState,
         queryFn: () => sleep(20).then(() => 'string'),
       },
     })

     expect(rendered.getByText('data: stringCached')).toBeInTheDocument()
     await vi.advanceTimersByTimeAsync(21)
     expect(rendered.getByText('data: string')).toBeInTheDocument()
-    queryClient.clear()
   })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/svelte-query/tests/HydrationBoundary/HydrationBoundary.svelte.test.ts`
around lines 27 - 43, The test calls queryClient.clear() at the end of the test
body which will be skipped if an assertion throws; either wrap the test body in
a try/finally and call queryClient.clear() in finally, or refactor so
queryClient is declared in the surrounding scope (e.g. let queryClient;) and
assign it inside the test, then add an afterEach that calls queryClient.clear()
and vi.useRealTimers() to guarantee cleanup for the test "should hydrate queries
to the cache on context" (referencing queryClient.clear(), vi.useRealTimers(),
and the test block).

@sukvvon sukvvon force-pushed the test/svelte-query-HydrationBoundary branch from 8bc1d99 to 1afcfd1 Compare February 24, 2026 05:05
Copy link
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 (3)
packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte (3)

29-30: options={undefined} and queryClient={undefined} are redundant — omit them.

In Svelte 5 runes mode, fallback values declared via $props() destructuring are used when the parent does not set a given prop or the value is undefined. So explicitly passing undefined is functionally identical to omitting these props, but it adds noise.

♻️ Proposed cleanup
 <HydrationBoundary
   state={dehydratedState}
-  options={undefined}
-  queryClient={undefined}
 >
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte` around
lines 29 - 30, Remove the redundant explicit undefined prop passes: delete the
attributes options={undefined} and queryClient={undefined} from the component
invocation in BaseExample.svelte so the component's fallback values (from
$props() destructuring) are used; search for the attributes "options" and
"queryClient" in that file and remove them, and run tests to confirm nothing
else relies on an explicit undefined value.

21-33: Remove redundant undefined prop assignments.

Lines 29-30 explicitly pass options={undefined} and queryClient={undefined}, but HydrationBoundary already defaults these to undefined. Remove the explicit assignments:

 <HydrationBoundary
   state={dehydratedState}
-  options={undefined}
-  queryClient={undefined}
 >
   <div>data: {query.data ?? 'undefined'}</div>
 </HydrationBoundary>
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte` around
lines 21 - 33, The HydrationBoundary JSX is passing redundant explicit undefined
props; remove the explicit options={undefined} and queryClient={undefined} from
the HydrationBoundary element so it relies on the component's defaults (leave
state={dehydratedState} and the children intact); target the HydrationBoundary
element in BaseExample.svelte to remove those two prop assignments.

29-30: Omit options={undefined} and queryClient={undefined} — they are redundant.

The HydrationBoundary component defaults both options and queryClient to undefined. In Svelte 5, explicitly passing prop={undefined} is equivalent to omitting the prop—both use the default value. Removing these lines is cleaner and reduces noise in the test.

♻️ Proposed cleanup
 <HydrationBoundary
   state={dehydratedState}
-  options={undefined}
-  queryClient={undefined}
 >
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte` around
lines 29 - 30, Remove the redundant explicit undefined props on the
HydrationBoundary usage: delete options={undefined} and queryClient={undefined}
from the HydrationBoundary component in BaseExample.svelte since
HydrationBoundary defaults those to undefined; ensure no other logic relies on
those props being explicitly passed and run tests to confirm behavior unchanged.
🤖 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/svelte-query/tests/HydrationBoundary/BaseExample.svelte`:
- Around line 29-30: Remove the redundant explicit undefined prop passes: delete
the attributes options={undefined} and queryClient={undefined} from the
component invocation in BaseExample.svelte so the component's fallback values
(from $props() destructuring) are used; search for the attributes "options" and
"queryClient" in that file and remove them, and run tests to confirm nothing
else relies on an explicit undefined value.
- Around line 21-33: The HydrationBoundary JSX is passing redundant explicit
undefined props; remove the explicit options={undefined} and
queryClient={undefined} from the HydrationBoundary element so it relies on the
component's defaults (leave state={dehydratedState} and the children intact);
target the HydrationBoundary element in BaseExample.svelte to remove those two
prop assignments.
- Around line 29-30: Remove the redundant explicit undefined props on the
HydrationBoundary usage: delete options={undefined} and queryClient={undefined}
from the HydrationBoundary component in BaseExample.svelte since
HydrationBoundary defaults those to undefined; ensure no other logic relies on
those props being explicitly passed and run tests to confirm behavior unchanged.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8bc1d99 and 1afcfd1.

📒 Files selected for processing (2)
  • packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte
  • packages/svelte-query/tests/HydrationBoundary/HydrationBoundary.svelte.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/svelte-query/tests/HydrationBoundary/HydrationBoundary.svelte.test.ts

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