Skip to content

test(query-core): assert exact cache event payloads in subscribe callbacks#11053

Merged
sukvvon merged 1 commit into
mainfrom
test/query-core-cache-callback-exact-event-args
Jul 12, 2026
Merged

test(query-core): assert exact cache event payloads in subscribe callbacks#11053
sukvvon merged 1 commit into
mainfrom
test/query-core-cache-callback-exact-event-args

Conversation

@sukvvon

@sukvvon sukvvon commented Jul 12, 2026

Copy link
Copy Markdown
Member

🎯 Changes

QueryCache/MutationCache subscribe callbacks receive an event object whose query/mutation is an instance the library creates internally. Several tests only checked that the callback fired (toHaveBeenCalled()) or matched the type field partially (objectContaining), leaving the instance and the exact payload shape unverified.

Each test can recover the instance from the cache (find({ queryKey }), or the in-scope mutation/observer), so the assertions now pin the full event object. Where the callback fires multiple times in a known order, the assertion is also pinned to the right call (added is the first event, removed from gc is the last).

  • queryCacheadded events (4 tests) — assert toHaveBeenNthCalledWith(1, { query, type: 'added' }); added is always the first event, and the earlier toHaveBeenCalledWith/toHaveBeenCalled() did not pin the call position
  • mutationCache — "should still notify removal..." — dropped objectContaining so { type: 'removed', mutation } also asserts no extra fields (single call is already guaranteed by toHaveBeenCalledTimes(1))
  • query — "queries should be garbage collected even if they never fetched" — captures the query before gc removes it, then asserts toHaveBeenLastCalledWith({ type: 'removed', query }) (gc removal is the last event)
  • queryObserver — "should notify cache listeners when setOptions is called" — asserts the full { type: 'observerOptionsUpdated', query, observer } (single call is already guaranteed by toHaveBeenCalledTimes(1))
  • queryClient — "should notify listeners when a query is reset"toHaveBeenCalled()toHaveBeenNthCalledWith(1, objectContaining({ type: 'updated', query, action: { type: 'setState' } })) (the action.state snapshot is left open, as it depends on timing)

✅ Checklist

🚀 Release Impact

  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Tests
    • Strengthened cache and observer notification tests to verify complete event payloads.
    • Added precise assertions for query and mutation additions, removals, updates, garbage collection, and option changes.
    • Improved validation of callback invocation order and associated query, observer, and action details.

@nx-cloud

nx-cloud Bot commented Jul 12, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 971cf30

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

☁️ Nx Cloud last updated this comment at 2026-07-12 04:19:05 UTC

@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR strengthens query-core cache subscription tests by asserting complete event payloads, including event type and the associated mutation, query, or observer.

Changes

Cache notification tests

Layer / File(s) Summary
Cache event payload assertions
packages/query-core/src/__tests__/mutationCache.test.tsx, packages/query-core/src/__tests__/query.test.tsx, packages/query-core/src/__tests__/queryCache.test.tsx
Removal and addition tests now verify subscriber callbacks receive the expected event type and cached entity.
Update notification payload assertions
packages/query-core/src/__tests__/queryClient.test.tsx, packages/query-core/src/__tests__/queryObserver.test.tsx
Reset and observer option tests now verify updated callback payloads include the resolved query and observer where applicable.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and accurately describes the main change: tightening cache event payload assertions in subscribe callbacks.
Description check ✅ Passed The description follows the required sections and covers the change, checklist, and release impact, with only a minor checklist wording omission.
✨ 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-cache-callback-exact-event-args

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

@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.

@sukvvon sukvvon self-assigned this Jul 12, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/query-core/src/__tests__/queryClient.test.tsx`:
- Around line 1617-1620: Update the callback assertion in the queryCache event
test to verify the emitted reset action alongside type and query. Extend the
expect.objectContaining payload for the updated event with the expected reset
action, preserving the existing query assertion.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b62d349c-7490-446d-80ef-365de705030f

📥 Commits

Reviewing files that changed from the base of the PR and between 8773a15 and e689aa7.

📒 Files selected for processing (5)
  • packages/query-core/src/__tests__/mutationCache.test.tsx
  • packages/query-core/src/__tests__/query.test.tsx
  • packages/query-core/src/__tests__/queryCache.test.tsx
  • packages/query-core/src/__tests__/queryClient.test.tsx
  • packages/query-core/src/__tests__/queryObserver.test.tsx

Comment thread packages/query-core/src/__tests__/queryClient.test.tsx
@pkg-pr-new

pkg-pr-new Bot commented Jul 12, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/lit-query

npm i https://pkg.pr.new/@tanstack/lit-query@11053

@tanstack/preact-query

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

@tanstack/preact-query-devtools

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

@tanstack/preact-query-persist-client

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: 971cf30

@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
react full 12.13 KB (0%)
react minimal 9.08 KB (0%)

@sukvvon sukvvon force-pushed the test/query-core-cache-callback-exact-event-args branch from e689aa7 to cd7feeb Compare July 12, 2026 03:49
@sukvvon sukvvon changed the title test(query-core): assert exact cache event arguments in subscribe callbacks test(query-core): assert exact cache event payloads in subscribe callbacks Jul 12, 2026
@sukvvon sukvvon force-pushed the test/query-core-cache-callback-exact-event-args branch from cd7feeb to d31ecb5 Compare July 12, 2026 03:52
@sukvvon sukvvon force-pushed the test/query-core-cache-callback-exact-event-args branch from d31ecb5 to 971cf30 Compare July 12, 2026 04:11
@sukvvon sukvvon merged commit b2ce5bc into main Jul 12, 2026
9 checks passed
@sukvvon sukvvon deleted the test/query-core-cache-callback-exact-event-args branch July 12, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant