Skip to content

Conversation

@sukvvon
Copy link
Contributor

@sukvvon sukvvon commented Nov 7, 2025

🎯 Changes

βœ… 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
    • Enhanced test suite reliability and timing control for the persisted queries functionality through improved test utilities and synchronization mechanisms.

@changeset-bot
Copy link

changeset-bot bot commented Nov 7, 2025

⚠️ No Changeset found

Latest commit: 56b9380

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 Nov 7, 2025

Walkthrough

A test file is refactored to use controlled fake timers and explicit timer advances instead of relying on real-time delays and polling. A sleep utility is introduced, replacing direct setTimeout calls and waitFor patterns with deterministic vi.advanceTimersByTimeAsync invocations.

Changes

Cohort / File(s) Change Summary
Test timing refactoring
packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx
Introduces fake timer setup/teardown via vitest; adds sleep utility function; replaces setTimeout-based delays with sleep(10) and sleep(100); converts waitFor polling to manual timer advances using vi.advanceTimersByTimeAsync; updates import statements and test assertions

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10–15 minutes

  • Review the sleep utility implementation for correctness
  • Verify fake timer setup/teardown doesn't affect other tests
  • Confirm all vi.advanceTimersByTimeAsync calls align with intended delay durations
  • Ensure test assertions still validate the expected behavior after timing changes

Suggested reviewers

  • manudeli

Poem

🐰 Timers tick now under command,
No more waiting, sleepy and bland,
Fake clocks advance at rabbit's pace,
Tests run swift through time and space!
⏰✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
βœ… Passed checks (2 passed)
Check name Status Explanation
Title check βœ… Passed The title precisely describes the three main changes: switching to fake timers, replacing waitFor with vi.advanceTimersByTimeAsync, and replacing setTimeout with sleep in the test file.
Description check βœ… Passed The description follows the template structure with all required sections present, including a checked checklist confirming contribution steps and local testing, and correct Release Impact selection for a dev-only change.
✨ Finishing touches
  • πŸ“ Generate docstrings
πŸ§ͺ 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 Nov 7, 2025

View your CI Pipeline Execution β†— for commit 56b9380

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... βœ… Succeeded 32s View β†—
nx run-many --target=build --exclude=examples/*... βœ… Succeeded 1s View β†—

☁️ Nx Cloud last updated this comment at 2025-11-07 04:58:11 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 7, 2025

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: 56b9380

@codecov
Copy link

codecov bot commented Nov 7, 2025

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 100.00%. Comparing base (7783a7a) to head (56b9380).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             main     #9850       +/-   ##
============================================
+ Coverage   45.76%   100.00%   +54.23%     
============================================
  Files         200         1      -199     
  Lines        8410        18     -8392     
  Branches     1930         2     -1928     
============================================
- Hits         3849        18     -3831     
+ Misses       4113         0     -4113     
+ Partials      448         0      -448     
Components Coverage Ξ”
@tanstack/angular-query-experimental βˆ… <ΓΈ> (βˆ…)
@tanstack/eslint-plugin-query βˆ… <ΓΈ> (βˆ…)
@tanstack/query-async-storage-persister βˆ… <ΓΈ> (βˆ…)
@tanstack/query-broadcast-client-experimental βˆ… <ΓΈ> (βˆ…)
@tanstack/query-codemods βˆ… <ΓΈ> (βˆ…)
@tanstack/query-core βˆ… <ΓΈ> (βˆ…)
@tanstack/query-devtools βˆ… <ΓΈ> (βˆ…)
@tanstack/query-persist-client-core βˆ… <ΓΈ> (βˆ…)
@tanstack/query-sync-storage-persister βˆ… <ΓΈ> (βˆ…)
@tanstack/query-test-utils βˆ… <ΓΈ> (βˆ…)
@tanstack/react-query βˆ… <ΓΈ> (βˆ…)
@tanstack/react-query-devtools βˆ… <ΓΈ> (βˆ…)
@tanstack/react-query-next-experimental βˆ… <ΓΈ> (βˆ…)
@tanstack/react-query-persist-client 100.00% <ΓΈ> (ΓΈ)
@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 βˆ… <ΓΈ> (βˆ…)
πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…fake timers, replace 'waitFor' with 'vi.advanceTimersByTimeAsync', and replace 'setTimeout' with 'sleep'
@sukvvon sukvvon force-pushed the test/react-query-persist-client-use-queries-with-persist-migrate-fake-timers branch from 847baa2 to 39fe259 Compare November 7, 2025 04:36
@sukvvon sukvvon marked this pull request as ready for review November 7, 2025 04:56
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: 0

🧹 Nitpick comments (1)
packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx (1)

131-134: Consider adding a clarifying comment for the two-step timer advance.

The logic is correct: the first advance completes the sleep(10) restoration, and the second flushes microtasks to ensure all React state updates complete. However, this pattern could benefit from a brief inline comment explaining the two-step approach for future maintainers.

Consider adding a comment:

+    // Advance 10ms to complete restoreClient's sleep(10), then flush microtasks
     await act(() => vi.advanceTimersByTimeAsync(10))
     await act(() => vi.advanceTimersByTimeAsync(0))
     expect(getByTestId('pending').textContent).toBe('false')
     expect(getByTestId('data').textContent).toBe('1,2,3')
πŸ“œ Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 7783a7a and 56b9380.

πŸ“’ Files selected for processing (1)
  • packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx (6 hunks)
🧰 Additional context used
🧠 Learnings (1)
πŸ““ Common learnings
Learnt from: justjake
Repo: TanStack/query PR: 9612
File: packages/query-core/src/__tests__/timeoutManager.test.tsx:37-57
Timestamp: 2025-09-04T16:36:04.575Z
Learning: In Node.js v12.19+ (and browsers), converting a Timeout object to a number (e.g., Number(timeoutId)) is valid because Timeout implements Symbol.toPrimitive; clearTimeout/clearInterval accept the object or its numeric primitive. Do not flag this pattern in tests like packages/query-core/src/__tests__/timeoutManager.test.tsx.
Learnt from: arnoud-dv
Repo: TanStack/query PR: 9669
File: docs/framework/angular/guides/testing.md:49-56
Timestamp: 2025-09-21T00:31:02.518Z
Learning: TestBed.tick() is a valid API introduced in Angular 20 for triggering effects in unit tests, similar to ApplicationRef.tick() but for testing contexts.
🧬 Code graph analysis (1)
packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx (1)
packages/query-persist-client-core/src/persist.ts (1)
  • PersistedClient (18-22)
πŸ”‡ Additional comments (5)
packages/react-query-persist-client/src/__tests__/use-queries-with-persist.test.tsx (5)

1-2: LGTM: Imports correctly updated for fake timer migration.

The addition of vi for timer control and act for wrapping async state updates aligns with the migration to fake timers.


5-5: LGTM: Correct utility for fake timer testing.

Switching from waitFor (polling-based) to sleep (timer-based) is the right choice for deterministic fake timer control.


17-17: LGTM: Proper timer lifecycle management.

Fake timers are correctly initialized before each test and restored afterward, ensuring proper isolation.

Also applies to: 36-36


57-59: LGTM: Controlled async restoration with fake timers.

The sleep(10) introduces a deterministic 10ms delay before restoration, which is properly controlled by the test's timer advances.


100-100: LGTM: Controlled async query function.

The sleep(100) delay works correctly with fake timers. Note that this query function shouldn't execute in the test since the persisted data is still fresh (staleTime: 30,000ms, data age: 1,000ms), which is why the test correctly doesn't advance the full 100ms.

@TkDodo TkDodo merged commit 7bc9543 into TanStack:main Nov 8, 2025
9 checks passed
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.

2 participants