Skip to content

test(query-core/queriesObserver): add test for returning cached combined result when nothing has changed#10513

Merged
sukvvon merged 1 commit intomainfrom
test/query-core-queriesObserver-cached-combined-result
Apr 18, 2026
Merged

test(query-core/queriesObserver): add test for returning cached combined result when nothing has changed#10513
sukvvon merged 1 commit intomainfrom
test/query-core-queriesObserver-cached-combined-result

Conversation

@sukvvon
Copy link
Copy Markdown
Collaborator

@sukvvon sukvvon commented Apr 18, 2026

🎯 Changes

Add a test to verify that #combineResult returns the cached #combinedResult without recalculating when none of the conditions (!#combinedResult, #result !== #lastResult, queryHashesChanged, combine !== #lastCombine) are met.

This covers the else path of the if block in #combineResult.

✅ 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 for query observer caching behavior to verify that combined results are properly cached and reused when the same parameters are provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 18, 2026

📝 Walkthrough

Walkthrough

A new test case was added to QueriesObserver.test.tsx that verifies getOptimisticResult returns a cached combined result (same reference) when invoked twice with identical queries array content and the same combine function reference.

Changes

Cohort / File(s) Summary
Test Addition
packages/query-core/src/__tests__/queriesObserver.test.tsx
Added test case asserting referential equality of combined results across two getOptimisticResult calls with identical inputs and same combine function reference, verifying caching behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested labels

package: query-core

Poem

🐰 A test hops by to check the cache,
Same queries in, same combine hash,
References match—no need to recalc,
Optimism thrives with this rigor's track! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a test for cached combined result behavior in QueriesObserver when nothing has changed.
Description check ✅ Passed The description provides clear changes, completes all required checklist items, and correctly marks the change as dev-only with no release impact.
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
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/query-core-queriesObserver-cached-combined-result

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

@sukvvon sukvvon self-assigned this Apr 18, 2026
@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Apr 18, 2026

View your CI Pipeline Execution ↗ for commit f0f4305

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

☁️ Nx Cloud last updated this comment at 2026-04-18 00:17:42 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.

Copy link
Copy Markdown
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)

652-660: This test can pass even if combine recomputes.

At Line 659, toBe alone doesn’t prove the cached branch was hit, because replaceEqualDeep can still return the previous reference after a recomputation with equal output. Please also assert that combine is not called again on the second pass.

Proposed test hardening
     const [raw1, getCombined1] = observer.getOptimisticResult(queries, combine)
     const combined1 = getCombined1(raw1)
+    const callsAfterFirst = combine.mock.calls.length

     const [raw2, getCombined2] = observer.getOptimisticResult(queries, combine)
     const combined2 = getCombined2(raw2)

     // Same combine, same queries → cached result returned
+    expect(combine.mock.calls.length).toBe(callsAfterFirst)
     expect(combined1).toBe(combined2)
🤖 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 652
- 660, The test currently only asserts reference equality
(expect(combined1).toBe(combined2)) which can pass even if combine recomputed;
update the test to ensure combine is not invoked on the second call by using a
spy/mocked function for combine (e.g., replace the plain combine with a jest.fn
or equivalent) and assert its call count before and after the second getCombined
call when calling observer.getOptimisticResult(queries, combine) and
getCombined2(raw2); specifically reference the combine function and
observer.getOptimisticResult to locate where to wrap combine and add an
assertion like expect(combine).not.toHaveBeenCalledTimes/incremented on the
second pass in addition to the existing toBe assertion.
🤖 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 652-660: The test currently only asserts reference equality
(expect(combined1).toBe(combined2)) which can pass even if combine recomputed;
update the test to ensure combine is not invoked on the second call by using a
spy/mocked function for combine (e.g., replace the plain combine with a jest.fn
or equivalent) and assert its call count before and after the second getCombined
call when calling observer.getOptimisticResult(queries, combine) and
getCombined2(raw2); specifically reference the combine function and
observer.getOptimisticResult to locate where to wrap combine and add an
assertion like expect(combine).not.toHaveBeenCalledTimes/incremented on the
second pass in addition to the existing toBe assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e27aa12f-bce0-4c51-acb6-07107b4db704

📥 Commits

Reviewing files that changed from the base of the PR and between cc97e9a and f0f4305.

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 18, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/preact-query

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

@tanstack/preact-query-devtools

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

@tanstack/preact-query-persist-client

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: f0f4305

@github-actions
Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
react full 11.99 KB (0%)
react minimal 9.02 KB (0%)

@sukvvon sukvvon merged commit 608706e into main Apr 18, 2026
10 checks passed
@sukvvon sukvvon deleted the test/query-core-queriesObserver-cached-combined-result branch April 18, 2026 00:18
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