test(query-db-collection): cover query invalidation behavior#1655
Conversation
📝 WalkthroughWalkthroughAdds a Changesets patch entry for ChangesInvalidation behavior tests
Estimated code review effort: 2 (Simple) | ~12 minutes Possibly related issues
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: 0 B Total Size: 125 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 4.26 kB ℹ️ View Unchanged
|
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/query-db-collection/tests/query.test.ts (2)
1610-1644: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMissing
collection.cleanup()in these three tests.Unlike the on-demand tests (which clean up via
liveQuery.cleanup()in afinallyblock) and the inactive/removed-query tests (which callcollection.cleanup()explicitly), these three eager tests leave the collection's observer/subscription active after the test ends. SincestartSync: truecreates a liveQueryObserveragainst the sharedqueryClient, consider addingawait collection.cleanup()for symmetry and to avoid lingering subscriptions across tests.Also applies to: 1646-1681, 1881-1921
🤖 Prompt for 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. In `@packages/query-db-collection/tests/query.test.ts` around lines 1610 - 1644, The eager invalidation tests leave the live observer from createCollection/queryCollectionOptions active after completion because startSync: true starts a QueryObserver subscription on the shared queryClient. Add an explicit await collection.cleanup() at the end of these eager tests (including the matching query.test cases mentioned in the comment) so they mirror the existing cleanup patterns in the on-demand and inactive-query tests and do not leak subscriptions across test cases.
1609-2002: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRepeated test boilerplate across all 8 cases.
Each test repeats the same pattern: build
queryFnmock,createCollection,await vi.waitForfor initial materialization, mutateitems, invalidate, thenawait vi.waitForagain. Extracting a small helper (e.g.,createTestCollection(queryKey, options)and awaitForRow(collection, id, expected)assertion helper) would reduce duplication and make future invalidation tests easier to add.As per coding guidelines, "Extract common logic into utility functions when identical or near-identical code blocks appear in multiple places" and "Prefer small, focused utility functions over large inline implementations."
🤖 Prompt for 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. In `@packages/query-db-collection/tests/query.test.ts` around lines 1609 - 2002, The invalidation behavior tests repeat the same setup and assertion flow across many cases, making the suite noisy and hard to extend. Extract the shared materialization/invalidation pattern into small helpers near the existing `invalidation behavior` describe block, such as a collection factory for `createCollection`/`queryCollectionOptions` setup and a reusable row-wait assertion for `vi.waitFor` checks. Keep the individual `it` cases focused on their unique query key, invalidation type, and expected row state, while reusing the helper logic for `queryFn`, `items`, and `stripVirtualProps` assertions.Source: Coding guidelines
🤖 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.
Nitpick comments:
In `@packages/query-db-collection/tests/query.test.ts`:
- Around line 1610-1644: The eager invalidation tests leave the live observer
from createCollection/queryCollectionOptions active after completion because
startSync: true starts a QueryObserver subscription on the shared queryClient.
Add an explicit await collection.cleanup() at the end of these eager tests
(including the matching query.test cases mentioned in the comment) so they
mirror the existing cleanup patterns in the on-demand and inactive-query tests
and do not leak subscriptions across test cases.
- Around line 1609-2002: The invalidation behavior tests repeat the same setup
and assertion flow across many cases, making the suite noisy and hard to extend.
Extract the shared materialization/invalidation pattern into small helpers near
the existing `invalidation behavior` describe block, such as a collection
factory for `createCollection`/`queryCollectionOptions` setup and a reusable
row-wait assertion for `vi.waitFor` checks. Keep the individual `it` cases
focused on their unique query key, invalidation type, and expected row state,
while reusing the helper logic for `queryFn`, `items`, and `stripVirtualProps`
assertions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6a8cdbc7-b96c-4460-b752-b6450b4b6f42
📒 Files selected for processing (2)
.changeset/query-invalidation-matrix.mdpackages/query-db-collection/tests/query.test.ts
🎯 Changes
Adds a focused invalidation behavior matrix for
@tanstack/query-db-collection, covering eager and on-demand collections across exact, prefix, overlapping, inactive, removed, and failed-refetch scenarios.This is test/spec coverage only for the adapter boundary: TanStack Query remains the invalidation authority, while the collection adapter is verified to rematerialize or preserve DB rows appropriately when Query invalidation changes observer state.
Reviewer Guidance
Root Cause
Issue #344 and RFC #1643 call out that the adapter sits between TanStack Query's document-cache model and TanStack DB's normalized row store. Without explicit matrix coverage, it is easy to regress where responsibility lives: Query should own invalidation and matching semantics, while the adapter should react correctly to successful or failed observer results.
Approach
invalidation behaviorsuite inpackages/query-db-collection/tests/query.test.ts.queryClient.invalidateQueries.@tanstack/query-db-collection.No runtime adapter code changes were needed.
Key Invariants
Non-goals
Trade-offs
The tests keep setup mostly explicit rather than extracting shared helpers. That makes each matrix row easier to review as a standalone characterization, at the cost of some repetition.
Retained/persisted invalidation is intentionally noted but not expanded into a broader fixture here; the existing unit coverage focuses this PR on active, inactive, removed, overlapping, and failed-refetch behavior.
✅ Checklist
pnpm test.Verification run locally:
Both commands pass locally. The test output includes expected stderr from existing error-path tests and the new failed-refetch characterization.
🚀 Release Impact
Files changed
.changeset/query-invalidation-matrix.md— adds a patch changeset for the new query collection invalidation coverage.packages/query-db-collection/tests/query.test.ts— adds the invalidation behavior matrix.Refs #344
Refs #1643
Summary by CodeRabbit
@tanstack/query-db-collectionpackage.