Skip to content

test(react-query/useSuspenseQueries): add test for suspending only pending queries when some already have cached data#10126

Open
sukvvon wants to merge 8 commits intoTanStack:mainfrom
sukvvon:test/react-query-useSuspenseQueries-partial-cached-suspend
Open

test(react-query/useSuspenseQueries): add test for suspending only pending queries when some already have cached data#10126
sukvvon wants to merge 8 commits intoTanStack:mainfrom
sukvvon:test/react-query-useSuspenseQueries-partial-cached-suspend

Conversation

@sukvvon
Copy link
Contributor

@sukvvon sukvvon commented Feb 13, 2026

🎯 Changes

Add a test to verify that useSuspenseQueries only suspends queries that are still pending, skipping queries that already have cached data via setQueryData.

✅ 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 coverage verifying that when multiple concurrent queries run and some already have cached results, only the pending queries trigger suspense/loading while cached queries return immediately.
    • Asserts that cached results are shown promptly and that background refetches complete to update all queries with fresh data.
    • Confirms no changes to public APIs.

…nding queries when some already have cached data
@changeset-bot
Copy link

changeset-bot bot commented Feb 13, 2026

⚠️ No Changeset found

Latest commit: f61c23d

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 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f61c23d and 4a85919.

📒 Files selected for processing (1)
  • packages/react-query/src/__tests__/useSuspenseQueries.test.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/react-query/src/tests/useSuspenseQueries.test.tsx

📝 Walkthrough

Walkthrough

Adds a test for useSuspenseQueries that verifies when multiple queries run with mixed cache states (one cached, one pending), only the pending query suspends while the cached query returns immediately and both update after a background refetch.

Changes

Cohort / File(s) Summary
Test Update
packages/react-query/src/__tests__/useSuspenseQueries.test.tsx
Adds a test that defines two queries (one seeded with cached data, one pending), renders under Suspense, asserts suspension for pending query only, then verifies cached result appears immediately and both queries update after refetch.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • TkDodo
  • manudeli

Poem

🐰 I nibbled code beneath the moonlight bright,

One key snoozed warm, the other held its breath,
Suspense did wait while a cache hopped light,
Then both refreshed and danced away from death. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title is fully related to the main change in the changeset, clearly summarizing that the PR adds a test for useSuspenseQueries with partial cached data.
Description check ✅ Passed The description follows the repository template with all required sections completed: Changes section explains the test addition, Checklist shows both contributing guide and testing verification completed, and Release Impact correctly marks it as dev-only with no changeset.

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

✨ Finishing Touches
🧪 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 13, 2026

View your CI Pipeline Execution ↗ for commit f61c23d

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

☁️ Nx Cloud last updated this comment at 2026-02-26 01:54:07 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 13, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/preact-query

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: f61c23d

Copy link
Collaborator

@TkDodo TkDodo left a comment

Choose a reason for hiding this comment

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

i don’t think this test tests what the description says it tests. In order to see that we only suspend until key2 is “done”, they would both need to have a different sleep time (likely shorter for key2 than for key1). Since key1 already has data but triggers a background refetch, we would only suspend until key2 is done. Then, data should be visible, but it should be cached data from key1 and fresh data from key2. At that time, the background refetch for key1 ist still ongoing, and eventually it should settle with data1: data1. Does that make sense?

… verify background refetch settles with fresh data
@sukvvon sukvvon requested a review from TkDodo February 26, 2026 00:59
@sukvvon
Copy link
Contributor Author

sukvvon commented Feb 26, 2026

@TkDodo Updated in 22e7972 — used different sleep times (key1: 2000ms, key2: 1000ms) so we only suspend until key2 is done, then verify cached data for key1 and fresh data for key2. Also added assertions for key1's background refetch settling with data1.

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 (1)
packages/react-query/src/__tests__/useSuspenseQueries.test.tsx (1)

880-888: Timing in the test appears to be longer than necessary.

The test advances time by a total of 4000ms, but based on the query configuration:

  • key1's queryFn takes 2000ms and its background refetch should start at mount (since staleTime defaults to 0, cached data is immediately stale)
  • key2's queryFn takes 1000ms

If key1's background refetch starts at mount, it should complete at t=2000ms, not t=4000ms. The current timing will still pass but doesn't precisely verify the expected behavior.

Additionally, the comment "key1 stale timer fires, triggering background refetch" at line 880 may be misleading—with default staleTime: 0, the background refetch should trigger immediately when the component mounts, not after a separate stale timer.

Consider simplifying the timing
     expect(rendered.getByText('data1: cached')).toBeInTheDocument()
     expect(rendered.getByText('data2: data2')).toBeInTheDocument()
 
-    // key1 stale timer fires, triggering background refetch
-    await act(() => vi.advanceTimersByTimeAsync(1000))
-
-    // key1 background refetch completes: key1 updates to fresh data
-    await act(() => vi.advanceTimersByTimeAsync(2000))
+    // key1 background refetch completes (started at mount, takes 2000ms total)
+    await act(() => vi.advanceTimersByTimeAsync(1000))
 
     expect(rendered.getByText('data1: data1')).toBeInTheDocument()
     expect(rendered.getByText('data2: data2')).toBeInTheDocument()

Please verify whether the background refetch for key1 starts immediately at mount or after a delay. If there's specific behavior that justifies the 4000ms total time, consider adding a clarifying comment.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-query/src/__tests__/useSuspenseQueries.test.tsx` around lines
880 - 888, The test advances timers longer than necessary and the comment is
misleading: because queries default to staleTime: 0, key1's background refetch
starts immediately at mount and should complete at t=2000ms (queryFn takes
2000ms), so replace the two vi.advanceTimersByTimeAsync calls that total 4000ms
with a single advance to 2000ms (using act(() =>
vi.advanceTimersByTimeAsync(2000))) and update the comment near the advances to
say "key1 background refetch started at mount (staleTime: 0) and completes at
2000ms" to accurately reflect behavior; locate and change the lines referencing
key1/key2, the act+/vi.advanceTimersByTimeAsync calls, and the comment in
useSuspenseQueries.test.tsx.
🤖 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/react-query/src/__tests__/useSuspenseQueries.test.tsx`:
- Around line 880-888: The test advances timers longer than necessary and the
comment is misleading: because queries default to staleTime: 0, key1's
background refetch starts immediately at mount and should complete at t=2000ms
(queryFn takes 2000ms), so replace the two vi.advanceTimersByTimeAsync calls
that total 4000ms with a single advance to 2000ms (using act(() =>
vi.advanceTimersByTimeAsync(2000))) and update the comment near the advances to
say "key1 background refetch started at mount (staleTime: 0) and completes at
2000ms" to accurately reflect behavior; locate and change the lines referencing
key1/key2, the act+/vi.advanceTimersByTimeAsync calls, and the comment in
useSuspenseQueries.test.tsx.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a089053 and f61c23d.

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

@sukvvon sukvvon force-pushed the test/react-query-useSuspenseQueries-partial-cached-suspend branch from 4a85919 to f61c23d Compare February 26, 2026 01:51
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