fix(core): observing "promise" needs to implicitly observe "data" #9772
main fromrefs/pull/9772/head·merged by TkDodo fixes #9727
Summary by CodeRabbit#
Bug Fixes
- Promise observation now implicitly observes associated data, fixing tracking and ensuring correct query results (including infinite queries).
New Features
- Exported infinite-query hook made available from the public API.
Tests
- Added tests verifying promise-driven observation correctly tracks data and renders pagination/loading states.
Chores
- Added a changeset entry for a patch bump noting the observation fix.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
Walkthrough#
Accessing a proxied query result's
promisenow also explicitly tracksdata. A changeset was added documenting a patch bump for@tanstack/query-core. Tests were updated/added to assert implicit data observation when using the promise (including infinite-queryhasNextPagescenarios).Changes#
Cohort / File(s) Summary Changeset metadata
\.changeset/good-windows-tell.md``New changeset entry documenting a patch bump for @tanstack/query-coreand a fix note: "observing 'promise' needs to implicitly observe 'data'."Query observer proxy tracking
packages/query-core/src/queryObserver.tsReorganized proxy gethandler forpromise: whenpromiseis accessed it now explicitly tracksdatain that branch and preserves the existing conditional rejection of a pending thenable whenexperimental_prefetchInRenderis disabled.Tests & public export usage
packages/react-query/src/__tests__/useQuery.promise.test.tsxAdded imports and tests verifying "should implicitly observe data when promise is used"; updated expectations and flows (including infinite-query hasNextPage) to reflect implicitdataobservation. Tests referenceuseInfiniteQuery(export surface used in tests).Sequence Diagram(s)#
sequenceDiagram autonumber participant Component as React component participant Proxy as QueryObserver Proxy participant Observer as QueryObserver internals participant Thenable as Query.thenable Component->>Proxy: access `query.promise` (e.g. use(query.promise)) note right of Proxy #D6EAF8: proxy.get('promise') updated Proxy->>Observer: track('data') ## explicit tracking added alt thenable pending and !experimental_prefetchInRender Observer->>Thenable: reject/throw pending thenable note right of Thenable #FDEBD0: triggers Suspense / error path else Observer->>Thenable: return thenable (normal path) note right of Thenable #E8F8F5: component can await/use data end Thenable-->>Component: resolved/rejected resultEstimated code review effort#
🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs#
- #9576: Modifies
queryObserver.tshandling of tracked accesses to thepromiseproperty—closely related refactor/fix to the same logic.
Poem#
🐰 I tapped the promise, data stirred from its bed, Now both wake together when
useis said. hasNextPage hums, no longer astray, Suspense tips its hat and hops away. ✨Pre-merge checks and finishing touches#
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution Description Check ⚠️ Warning The description only contains “fixes #9727” and omits the required sections for “Changes”, “Checklist”, and “Release Impact” from the repository template, so it does not follow the prescribed structure or provide context about the change. Please expand the PR description to include the “🎯 Changes” section with details on what was modified and why, complete the “✅ Checklist” confirming contribution and testing steps, and fill out “🚀 Release Impact” to indicate changeset generation or dev-only impact. Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstringsto improve docstring coverage.✅ Passed checks (3 passed)
Check name Status Explanation Title Check ✅ Passed The title clearly summarizes the primary change by indicating that observing the promiseproperty will now implicitly observedata, matching the core fix implemented in the changeset and code. It is concise, specific to the main change, and follows conventional commit style without extraneous information.Linked Issues Check ✅ Passed The code updates ensure that accessing promisenow also tracksdataand related properties, directly addressing the core issue of stalehasNextPageafter suspension described in issue #9727. Tests were added and updated to verify implicit data observation via promise.Out of Scope Changes Check ✅ Passed All modifications—including the changeset metadata, core logic in queryObserver.ts, and related tests—are focused on ensuring that observingpromiseimplicitly observesdata, which aligns with the linked issue objectives. No unrelated functionality or files appear to have been altered.✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
- Commit unit tests in branch `feature/fix/use-data`
Comment `@coderabbitai help` to get the list of available commands and usage tips.- #9576: Modifies
View your CI Pipeline Execution ↗ for commit e6fce973a626250ee27f6d694b5f71ed68215446
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 3m 13s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 28s View ↗
☁️ Nx Cloud last updated this comment at
2025-10-16 14:23:13UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9772@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9772@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9772@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9772@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9772@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9772@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9772@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9772@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9772@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9772@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9772@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9772@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9772@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9772@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9772@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9772@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9772@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9772@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9772@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9772commit:
e6fce97github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/react-query/src/__tests__/useQuery.promise.test.tsx (1)
1388-1433: Well-crafted test that validates the fix for issue #9727.The test correctly verifies that accessing
query.promiseviaReact.use()now implicitly tracksdata, ensuring thathasNextPage(which depends on the tracked data) reflects the correct value after suspending. The query setup, suspense handling, and assertions are all appropriate.Optional suggestions for enhanced clarity:
- Consider adding a brief inline comment before line 1405 explaining that this test specifically validates implicit data observation when using the promise (e.g.,
// Accessing promise should implicitly track data, ensuring hasNextPage is correctly computed). - For completeness, you could add a complementary test case that verifies
hasNextPage: falsewhengetNextPageParamreturnsnullorundefined.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 0a72df88a832946de2e4e85b9e01a22d9c771283 and a7e66035a55b6f2c6c3ca4aef2e06d54f9f67e9b.
📒 Files selected for processing (1)
packages/react-query/src/__tests__/useQuery.promise.test.tsx(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/react-query/src/__tests__/useQuery.promise.test.tsx (2)
packages/query-core/src/queryObserver.ts (1)
query(704-721)
packages/query-core/src/infiniteQueryBehavior.ts (1)
hasNextPage(162-168)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
- Consider adding a brief inline comment before line 1405 explaining that this test specifically validates implicit data observation when using the promise (e.g.,
Codecov Report#
:x: Patch coverage is
16.66667%with5 linesin your changes missing coverage. Please review. :white_check_mark: Project coverage is 59.77%. Comparing base (64507ba) to head (e6fce97). :warning: Report is 1 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9772 +/- ## =========================================== + Coverage 45.75% 59.77% +14.02% =========================================== Files 200 129 -71 Lines 8403 5619 -2784 Branches 1930 1537 -393 =========================================== - Hits 3845 3359 -486 + Misses 4111 1955 -2156 + Partials 447 305 -142- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/react-query/src/__tests__/useQuery.promise.test.tsx (2)
420-423: Brittle renderedComponents ordering assertionsAdjustments to expect(renderedComponents).toEqual([Page, MyComponent]) look correct functionally, but strict array equality on render order can be flaky across React versions/runtimes. Consider relaxing:
- Assert presence instead of exact order: expect(renderedComponents).toEqual(expect.arrayContaining([Page, MyComponent])) and assert length
- Or assert DOM output only (already done in most tests)
Also applies to: 488-491, 682-685, 1097-1100, 1111-1114
1287-1294: Make background-update assertion deterministic under fake timersThe new waitFor is reasonable, but with vi.useFakeTimers shouldAdvanceTime: true, polling can still be timing-sensitive. Prefer explicit timer advancement + stream take to reduce flakiness.
Apply this diff:
- { - const { snapshot } = await renderStream.takeRender() - expect(snapshot).toMatchObject({ data: 'test3' }) - } + { + const { snapshot } = await renderStream.takeRender() + expect(snapshot).toMatchObject({ data: 'test3' }) + } - await waitFor(() => rendered.getByText('test0new')) + await vi.advanceTimersByTimeAsync(10) + { + const { withinDOM } = await renderStream.takeRender() + withinDOM().getByText('test0new') + }If you prefer keeping waitFor, consider jest/rtl timer config to ensure it integrates with vitest fake timers in CI.
Also applies to: 1302-1303
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between a7e66035a55b6f2c6c3ca4aef2e06d54f9f67e9b and e6fce973a626250ee27f6d694b5f71ed68215446.
📒 Files selected for processing (1)
packages/react-query/src/__tests__/useQuery.promise.test.tsx(9 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/react-query/src/__tests__/useQuery.promise.test.tsx (2)
packages/query-core/src/queryObserver.ts (1)
query(704-721)
packages/query-core/src/infiniteQueryBehavior.ts (1)
hasNextPage(162-168)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
🔇 Additional comments (3)
packages/react-query/src/__tests__/useQuery.promise.test.tsx (3)
9-9: Confirm peer dependency for waitForUsing waitFor from @testing-library/react 16.x requires @testing-library/dom as a peer. Ensure it’s installed to avoid runtime/typings issues in CI.
Based on learnings
15-17: Public export of useInfiniteQueryImporting useInfiniteQuery from the package barrel implies it’s exported publicly. Please confirm the export is present in the public module to keep tests (and consumers) aligned.
1385-1430: Great coverage: implicit data observation via promiseThis precisely captures the regression for infinite queries (hasNextPage becomes true after suspension). LGTM.
TkDodo merged thise42ddfe
TkDodo closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: e6fce973a626250ee27f6d694b5f71ed68215446
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 19 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR