Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/svelte-query/tests/HydrationBoundary/BaseExample.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script lang="ts">
import { QueryClient } from '@tanstack/query-core'
import {
HydrationBoundary,
createQuery,
setQueryClientContext,
} from '../../src/index.js'
import type { DehydratedState } from '@tanstack/query-core'

let {
dehydratedState,
queryFn,
}: {
dehydratedState: DehydratedState
queryFn: () => Promise<string>
} = $props()

const queryClient = new QueryClient()
setQueryClientContext(queryClient)

const query = createQuery(() => ({
queryKey: ['string'],
queryFn,
}))
</script>

<HydrationBoundary
state={dehydratedState}
options={undefined}
queryClient={undefined}
>
<div>data: {query.data ?? 'undefined'}</div>
</HydrationBoundary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { render } from '@testing-library/svelte'
import { QueryClient, dehydrate } from '@tanstack/query-core'
import { sleep } from '@tanstack/query-test-utils'
import BaseExample from './BaseExample.svelte'

describe('HydrationBoundary', () => {
let stringifiedState: string

beforeEach(async () => {
vi.useFakeTimers()
const queryClient = new QueryClient()
queryClient.prefetchQuery({
queryKey: ['string'],
queryFn: () => sleep(10).then(() => 'stringCached'),
})
await vi.advanceTimersByTimeAsync(10)
const dehydrated = dehydrate(queryClient)
stringifiedState = JSON.stringify(dehydrated)
queryClient.clear()
})

afterEach(() => {
vi.useRealTimers()
})

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

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

expect(rendered.getByText('data: stringCached')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(21)
expect(rendered.getByText('data: string')).toBeInTheDocument()
})
Comment on lines 27 to 40
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).

})
Loading