Conversation
…itFor' with 'vi.advanceTimersByTimeAsync', and move 'queryClient' to 'beforeEach'
📝 WalkthroughWalkthroughRefactors Svelte Query tests to use Vitest fake timers (vi.useFakeTimers()/vi.advanceTimersByTimeAsync()/vi.useRealTimers()), initialize a fresh QueryClient/QueryCache per test, replace waitFor/sleep with deterministic timer advances, adjust refetch timing, and rename a reactive Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
View your CI Pipeline Execution ↗ for commit c99fe97
☁️ Nx Cloud last updated this comment at |
🚀 Changeset Version PreviewNo changeset entries found. Merging this PR will not cause a version bump for any packages. |
More templates
@tanstack/angular-query-experimental
@tanstack/eslint-plugin-query
@tanstack/preact-query
@tanstack/preact-query-devtools
@tanstack/preact-query-persist-client
@tanstack/query-async-storage-persister
@tanstack/query-broadcast-client-experimental
@tanstack/query-core
@tanstack/query-devtools
@tanstack/query-persist-client-core
@tanstack/query-sync-storage-persister
@tanstack/react-query
@tanstack/react-query-devtools
@tanstack/react-query-next-experimental
@tanstack/react-query-persist-client
@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
commit: |
size-limit report 📦
|
…urnType' for 'queryCache'
…m 100 to 11 for staleTime test
… to match 'staleTime: 100'
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/svelte-query/tests/createQuery.svelte.test.ts (2)
86-87: Consider extracting a helper for repeated zero-time timer flushes.
await vi.advanceTimersByTimeAsync(0)appears many times; a tiny helper would reduce duplication and improve readability.✨ Optional DRY helper
describe('createQuery', () => { + const flushQueryUpdates = () => vi.advanceTimersByTimeAsync(0)Then replace repeated calls, e.g.:
- await vi.advanceTimersByTimeAsync(0) + await flushQueryUpdates()🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/svelte-query/tests/createQuery.svelte.test.ts` around lines 86 - 87, The test repeats await vi.advanceTimersByTimeAsync(0) in createQuery.svelte.test.ts; extract a small helper (e.g., flushTimers or advanceZero) and replace each direct call with that helper to reduce duplication and clarify intent—add the helper near the top of the test file (or test utils) and update all occurrences of vi.advanceTimersByTimeAsync(0) within tests to call the new helper function.
26-29: Restore real timers in afinallyblock to prevent cascading test pollution.If
queryClient.clear()ever throws, subsequent tests can stay on fake timers.♻️ Suggested hardening
afterEach(() => { - queryClient.clear() - vi.useRealTimers() + try { + queryClient.clear() + } finally { + vi.useRealTimers() + } })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/svelte-query/tests/createQuery.svelte.test.ts` around lines 26 - 29, The afterEach hook can leave fake timers enabled if queryClient.clear() throws; wrap the clear call in a try/finally so vi.useRealTimers() always runs. Update the afterEach to call queryClient.clear() (await if it can return a promise) inside a try block and call vi.useRealTimers() in the finally block to guarantee timer restoration (referencing the existing afterEach, queryClient.clear, and vi.useRealTimers symbols).
🤖 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/createQuery.svelte.test.ts`:
- Around line 86-87: The test repeats await vi.advanceTimersByTimeAsync(0) in
createQuery.svelte.test.ts; extract a small helper (e.g., flushTimers or
advanceZero) and replace each direct call with that helper to reduce duplication
and clarify intent—add the helper near the top of the test file (or test utils)
and update all occurrences of vi.advanceTimersByTimeAsync(0) within tests to
call the new helper function.
- Around line 26-29: The afterEach hook can leave fake timers enabled if
queryClient.clear() throws; wrap the clear call in a try/finally so
vi.useRealTimers() always runs. Update the afterEach to call queryClient.clear()
(await if it can return a promise) inside a try block and call
vi.useRealTimers() in the finally block to guarantee timer restoration
(referencing the existing afterEach, queryClient.clear, and vi.useRealTimers
symbols).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 4afd8ea2-bba2-4398-8978-62e2f9fd861d
📒 Files selected for processing (1)
packages/svelte-query/tests/createQuery.svelte.test.ts
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/svelte-query/tests/createQuery.svelte.test.ts (1)
1296-1305:⚠️ Potential issue | 🔴 CriticalTest will deadlock when enabled due to awaiting prefetchQuery before advancing fake timers.
Line 1296 awaits
prefetchQuerybefore advancing fake timers at line 1304, but the queryFn'sawait sleep(10)cannot resolve under fake timers until timers are advanced. This creates a circular dependency that will hang the test whenit.todois converted toit.Move the
awaitto after timer advancement:🔧 Fix
- await queryClient.prefetchQuery({ + const prefetchPromise = queryClient.prefetchQuery({ queryKey: key, queryFn: async () => { await sleep(10) return 'prefetch' }, }) await vi.advanceTimersByTimeAsync(10) + await prefetchPromise expect(queryClient.getQueryState(key)?.data).toBe('prefetch')🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/svelte-query/tests/createQuery.svelte.test.ts` around lines 1296 - 1305, The test deadlocks because it awaits queryClient.prefetchQuery (which runs queryFn that awaits sleep(10) under fake timers) before advancing timers; instead, start the prefetch without awaiting (call queryClient.prefetchQuery(...) and store the returned promise), then call vi.advanceTimersByTimeAsync(10) to let sleep(10) resolve, and only after advancing timers await the stored prefetch promise and then assert via queryClient.getQueryState(key)?.data; adjust the test to use this order using the existing symbols queryClient.prefetchQuery, vi.advanceTimersByTimeAsync, sleep, and queryClient.getQueryState.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/svelte-query/tests/createQuery.svelte.test.ts`:
- Around line 1296-1305: The test deadlocks because it awaits
queryClient.prefetchQuery (which runs queryFn that awaits sleep(10) under fake
timers) before advancing timers; instead, start the prefetch without awaiting
(call queryClient.prefetchQuery(...) and store the returned promise), then call
vi.advanceTimersByTimeAsync(10) to let sleep(10) resolve, and only after
advancing timers await the stored prefetch promise and then assert via
queryClient.getQueryState(key)?.data; adjust the test to use this order using
the existing symbols queryClient.prefetchQuery, vi.advanceTimersByTimeAsync,
sleep, and queryClient.getQueryState.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 7e55e8f1-84f1-405c-8b51-256fbbd5a271
📒 Files selected for processing (1)
packages/svelte-query/tests/createQuery.svelte.test.ts
🎯 Changes
vi.useFakeTimers()tobeforeEachandvi.useRealTimers()toafterEachqueryClientandqueryCachecreation from describe-levelconsttobeforeEachfor per-test isolationvi.waitForwithvi.advanceTimersByTimeAsyncfor deterministic timer controlawait sleep(N)in test code withawait vi.advanceTimersByTimeAsync(N)await query.refetch()withquery.refetch()+await vi.advanceTimersByTimeAsync(0)queryClientvariable tocurrentClientto fix ESLint warningasyncfromwithEffectRootcallback withoutawait✅ Checklist
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit
Tests
Chores