Skip to content

test(query-core/queriesObserver): add test for 'trackResult' property tracking synchronization across all observers#10185

Open
sukvvon wants to merge 3 commits intoTanStack:mainfrom
sukvvon:test/query-core-queriesObserver-trackResult
Open

test(query-core/queriesObserver): add test for 'trackResult' property tracking synchronization across all observers#10185
sukvvon wants to merge 3 commits intoTanStack:mainfrom
sukvvon:test/query-core-queriesObserver-trackResult

Conversation

@sukvvon
Copy link
Contributor

@sukvvon sukvvon commented Feb 25, 2026

🎯 Changes

Add a test to verify that trackResult synchronizes property tracking across all observers in QueriesObserver.

When a property (e.g., status) is accessed on one tracked result, trackProp is called on all observers — 1 direct call from the accessed observer's proxy + 2 synchronized calls from onPropTracked callback (one per observer).

✅ 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 test coverage to verify property-tracking behavior across multiple observers during query operations, ensuring consistent tracking when observer results are accessed. The new checks are present in multiple scenarios to improve confidence in observer synchronization and result tracking.

… tracking synchronization across all observers
@changeset-bot
Copy link

changeset-bot bot commented Feb 25, 2026

⚠️ No Changeset found

Latest commit: 9a7d894

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

@nx-cloud
Copy link

nx-cloud bot commented Feb 25, 2026

View your CI Pipeline Execution ↗ for commit 9a7d894

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

☁️ Nx Cloud last updated this comment at 2026-02-25 12:32:52 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 25, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/preact-query

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: 9a7d894

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 25, 2026

📝 Walkthrough

Walkthrough

Adds a new unit test to queriesObserver.test.tsx that spies on QueryObserver.prototype.trackProp and verifies that calling trackResult then accessing a property on one observer's result causes tracking on all observer proxies.

Changes

Cohort / File(s) Summary
Test Suite
packages/query-core/src/__tests__/queriesObserver.test.tsx
Added a test "should track properties on all observers when trackResult is called" that spies on QueryObserver.prototype.trackProp, calls QueriesObserver.trackResult and accesses a property on a tracked result, asserting trackProp invocation counts. Test appears duplicated within the file.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐇 I hop through tests with careful cheer,
Spying props so every observer hears,
A nibble here, a property there,
Trackers call out in the testing lair,
Hooray — the rabbit’s checks appear!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the test addition for trackResult property tracking synchronization across observers, which is the main change in the PR.
Description check ✅ Passed The description follows the repository template with completed sections explaining changes, a fully checked checklist, and proper release impact designation as dev-only.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 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.

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/query-core/src/__tests__/queriesObserver.test.tsx (1)

535-539: getCombinedResult(trackedResults) is unnecessary when combine is undefined; assert on trackedResults directly.

With no combine function, passing the proxy-wrapped trackedResults into getCombinedResult only to check .length is redundant: it risks triggering extra proxy get-traps that affect the spy count, and the same assertion can be made on trackedResults itself.

♻️ Proposed refactor
-    const trackedResults = trackResult()
-    const combinedResult = getCombinedResult(trackedResults)
-
-    expect(combinedResult).toHaveLength(2)
+    const trackedResults = trackResult()
+
+    expect(trackedResults).toHaveLength(2)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/query-core/src/__tests__/queriesObserver.test.tsx` around lines 535
- 539, The test unnecessarily calls getCombinedResult(trackedResults) when no
combine function is provided, which can trigger proxy get-traps and alter spy
counts; change the assertion to check trackedResults directly by removing the
getCombinedResult call and the combinedResult variable and replace
expect(combinedResult).toHaveLength(2) with
expect(trackedResults).toHaveLength(2) (references: getCombinedResult,
trackResult, trackedResults, combinedResult, combine).
🤖 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/query-core/src/__tests__/queriesObserver.test.tsx`:
- Around line 535-539: The test unnecessarily calls
getCombinedResult(trackedResults) when no combine function is provided, which
can trigger proxy get-traps and alter spy counts; change the assertion to check
trackedResults directly by removing the getCombinedResult call and the
combinedResult variable and replace expect(combinedResult).toHaveLength(2) with
expect(trackedResults).toHaveLength(2) (references: getCombinedResult,
trackResult, trackedResults, combinedResult, combine).

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 807c8a9 and 5beeeda.

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

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/query-core/src/__tests__/queriesObserver.test.tsx (1)

525-547: mockRestore() is not guaranteed to run if an assertion throws — consider afterEach or try/finally.

If any expect(...) call at lines 544–545 fails, trackPropSpy.mockRestore() is never reached, leaving QueryObserver.prototype.trackProp permanently patched for any tests that run after this one in the suite.

♻️ Proposed fix using try/finally
   const trackPropSpy = vi.spyOn(QueryObserver.prototype, 'trackProp')

+  try {
     const [, , trackResult] = observer.getOptimisticResult(
       [
         { queryKey: key1, queryFn: queryFn1 },
         { queryKey: key2, queryFn: queryFn2 },
       ],
       undefined,
     )

     const trackedResults = trackResult()

     expect(trackedResults).toHaveLength(2)

     void trackedResults[0]!.status

     expect(trackPropSpy).toHaveBeenCalledWith('status')
     expect(trackPropSpy).toHaveBeenCalledTimes(3)
-
-  trackPropSpy.mockRestore()
+  } finally {
+    trackPropSpy.mockRestore()
+  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/query-core/src/__tests__/queriesObserver.test.tsx` around lines 525
- 547, The test installs a spy on QueryObserver.prototype.trackProp
(trackPropSpy) but calls trackPropSpy.mockRestore() only at the end, which may
be skipped if an assertion throws; wrap the assertion block in a try/finally (or
move restore to an afterEach) so that trackPropSpy.mockRestore() always runs;
specifically, ensure the spy created for QueryObserver.prototype.trackProp
around the getOptimisticResult / trackedResults assertions is restored in a
finally block (or restored in a test-level afterEach) to avoid leaking the
patched method to other tests.
🤖 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/query-core/src/__tests__/queriesObserver.test.tsx`:
- Around line 525-547: The test installs a spy on
QueryObserver.prototype.trackProp (trackPropSpy) but calls
trackPropSpy.mockRestore() only at the end, which may be skipped if an assertion
throws; wrap the assertion block in a try/finally (or move restore to an
afterEach) so that trackPropSpy.mockRestore() always runs; specifically, ensure
the spy created for QueryObserver.prototype.trackProp around the
getOptimisticResult / trackedResults assertions is restored in a finally block
(or restored in a test-level afterEach) to avoid leaking the patched method to
other tests.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5beeeda and 9a7d894.

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

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.

1 participant