Skip to content

test(query-core/hydration): replace 'vi.waitFor' with 'vi.advanceTimersByTimeAsync' and use 'queryKey' helper for infinite query tests#10944

Open
sukvvon wants to merge 1 commit into
mainfrom
test/query-core-hydration-advance-timers
Open

test(query-core/hydration): replace 'vi.waitFor' with 'vi.advanceTimersByTimeAsync' and use 'queryKey' helper for infinite query tests#10944
sukvvon wants to merge 1 commit into
mainfrom
test/query-core-hydration-advance-timers

Conversation

@sukvvon

@sukvvon sukvvon commented Jun 14, 2026

Copy link
Copy Markdown
Member

🎯 Changes

The infinite query tests in hydration.test.tsx wrapped prefetchInfiniteQuery/fetchInfiniteQuery calls in vi.waitFor(...) while the suite runs under fake timers (vi.useFakeTimers()). vi.waitFor polls a callback that returns a fresh prefetch promise on every tick, which is the wrong tool under fake timers and made these tests slow.

This replaces that pattern with the standard fake-timer flow already used throughout the file.

Changes:

  • Replace 9 vi.waitFor(() => client.prefetch/fetchInfiniteQuery(...)) calls with const promise = client.prefetch/fetchInfiniteQuery(...) + await vi.advanceTimersByTimeAsync(...) + await promise.
  • Use sleep(10) in the converted queryFns and advance by 10 (single page) / 20 (the pages: 2 test) so the timer control is deterministic.
  • Drop the redundant async from the converted queryFn: ({ pageParam }) => sleep(...).then(...) arrows (they only return a promise).
  • Replace hard-coded string query keys with the queryKey() helper in these tests for uniqueness and consistency with the rest of the file.

No assertions were changed. The whole hydration.test.tsx suite (42 tests) passes with no type errors, and the run is noticeably faster after removing the vi.waitFor polling.

✅ 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

Release Notes

  • Tests
    • Enhanced test coverage for infinite query hydration and dehydration cycles, including multi-page scenarios and state preservation across cache hydration.

…rsByTimeAsync' and use 'queryKey' helper for infinite query tests
@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a4bbb980-400b-4c57-9a03-518067ef39db

📥 Commits

Reviewing files that changed from the base of the PR and between 47e539a and f398a4d.

📒 Files selected for processing (1)
  • packages/query-core/src/__tests__/hydration.test.tsx

📝 Walkthrough

Walkthrough

The hydration test suite for infinite queries is refactored: all six test scenarios replace vi.waitFor-based timing with prefetchInfiniteQuery combined with vi.advanceTimersByTimeAsync, adopt dynamically generated query keys, and update assertions on queryType, pages, and pageParams across dehydrate/hydrate cycles, refetch, setOptions, and multi-page scenarios.

Changes

Infinite query hydration test refactor

Layer / File(s) Summary
Basic hydration and infiniteQueryBehavior attachment
packages/query-core/src/__tests__/hydration.test.tsx
Rewrites the first two tests: prefetches with fake timers, dehydrates, asserts queryType: 'infinite' survives the cycle, then calls fetchInfiniteQuery after hydration and asserts the resulting pages/pageParams shape.
Dehydrate/hydrate cycle type restoration and refetch setup
packages/query-core/src/__tests__/hydration.test.tsx
Updates the restore-type test to match the dehydrated query by generated key and asserts queryType: 'infinite' both pre- and post-hydration; adds setup for the refetch-after-hydration scenario using prefetchInfiniteQuery and timer advancement.
Refetch-after-hydration assertions
packages/query-core/src/__tests__/hydration.test.tsx
Replaces vi.waitFor-wrapped fetchInfiniteQuery with direct awaiting; asserts pre-refetch pages/pageParams cache state, then captures the refetch result for further assertions.
setOptions type persistence and multi-page hydration
packages/query-core/src/__tests__/hydration.test.tsx
Rewrites the setOptions test with a generated key to assert the query stays 'infinite' after setOptions; refactors the multi-page test to use prefetchInfiniteQuery with pages: 2, advances timers, and asserts all two pages are preserved after hydration and a subsequent fetchInfiniteQuery.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Hop, hop, hydrate! The pages align,
Infinite queries preserved, working fine.
No more waitFor — just timers we advance,
Dynamic keys lead this dehydration dance.
queryType: 'infinite' survives every round,
In the cache of TanStack, good data is found! 🌟

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: replacing vi.waitFor with vi.advanceTimersByTimeAsync and using queryKey helper for infinite query tests in the hydration test file.
Description check ✅ Passed The description covers all required template sections with detailed explanations of changes, testing, and release impact. Both checklist items are properly completed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/query-core-hydration-advance-timers

Comment @coderabbitai help to get the list of available commands and usage tips.

@sukvvon sukvvon self-assigned this Jun 14, 2026
@nx-cloud

nx-cloud Bot commented Jun 14, 2026

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix Eligible

An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.

To disable these notifications, a workspace admin can disable them in workspace settings.


View your CI Pipeline Execution ↗ for commit f398a4d

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ❌ Failed 1m 22s View ↗

☁️ Nx Cloud last updated this comment at 2026-06-14 12:31:41 UTC

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

No changeset entries found. Merging this PR will not cause a version bump for any packages.

@pkg-pr-new

pkg-pr-new Bot commented Jun 14, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/lit-query

npm i https://pkg.pr.new/@tanstack/lit-query@10944

@tanstack/preact-query

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

@tanstack/preact-query-devtools

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

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10944

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: f398a4d

@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
react full 12.11 KB (0%)
react minimal 9.08 KB (0%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant