Skip to content

test({react,preact}-query/useSuspenseQueries): inline test helpers, remove shared 'queryClient', and use 'beforeEach'/'afterEach' for setup and teardown#10352

Merged
sukvvon merged 3 commits intomainfrom
test/react-preact-query-useSuspenseQueries-inline-test-helpers
Mar 29, 2026
Merged

test({react,preact}-query/useSuspenseQueries): inline test helpers, remove shared 'queryClient', and use 'beforeEach'/'afterEach' for setup and teardown#10352
sukvvon merged 3 commits intomainfrom
test/react-preact-query-useSuspenseQueries-inline-test-helpers

Conversation

@sukvvon
Copy link
Copy Markdown
Collaborator

@sukvvon sukvvon commented Mar 29, 2026

🎯 Changes

  • Remove shared abstractions (createQuery, resolveQueries, QUERY_DURATION, withSuspenseWrapper, QueriesContainer, TestComponent) and inline them directly in each test for better readability
  • Move queryClient from module scope into beforeEach so each test gets a fresh instance, preventing cross-test state leakage
  • Replace beforeAll/afterAll with beforeEach/afterEach for fake timer setup/teardown to be consistent with other test files (e.g. useQuery.test.tsx)
  • Remove unused FunctionalComponent import from preact-query test
  • Replace [1] as const with queryKey() util
  • Fix act callback type error in preact-query by using async () => { await ... } pattern

✅ 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
    • Enhanced test isolation and refactored test suite structure for improved maintainability.

…emove shared 'queryClient', and use 'beforeEach'/'afterEach' for setup and teardown
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 29, 2026

📝 Walkthrough

Walkthrough

The changes refactor test suites for useSuspenseQueries in both preact-query and react-query packages. Shared test helpers (createQuery, resolveQueries, QUERY_DURATION) have been removed, test suites converted to use per-test queryClient instances with localized fake timer management, and helper-based test components have been replaced with inline implementations using explicit time advancement calls.

Changes

Cohort / File(s) Summary
Test Suite Refactoring
packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx, packages/react-query/src/__tests__/useSuspenseQueries.test.tsx
Removed shared test utilities and helpers; transitioned from module-level to per-test queryClient instances; switched from beforeAll/afterAll fake timer lifecycle to per-test vi.useFakeTimers() management; inlined Page test components with explicit vi.advanceTimersByTimeAsync() calls replacing helper-based time advancement; added per-test cleanup via queryClient.clear() in afterEach.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Helpers are gone, now tests stand tall,
Each query client answers the call,
Timers tick true in each test's own space,
No shared state in the testing race,
Cleaner, faster, a refactor won!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: inlining test helpers, removing shared queryClient, and replacing beforeAll/afterAll with beforeEach/afterEach.
Description check ✅ Passed The description is comprehensive and complete, covering all changes, following the template structure, and includes checked checklist items confirming contribution guide compliance and local testing.

✏️ 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/react-preact-query-useSuspenseQueries-inline-test-helpers

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

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Mar 29, 2026

View your CI Pipeline Execution ↗ for commit cacc758

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

☁️ Nx Cloud last updated this comment at 2026-03-29 17:33:36 UTC

@sukvvon sukvvon self-assigned this Mar 29, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 29, 2026

🚀 Changeset Version Preview

No changeset entries found. Merging this PR will not cause a version bump for any packages.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 29, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/preact-query

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

@tanstack/preact-query-devtools

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

@tanstack/preact-query-persist-client

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: cacc758

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 29, 2026

size-limit report 📦

Path Size
react full 11.98 KB (0%)
react minimal 9.01 KB (0%)

… with 'queryKey()' util and fix 'act' callback type in preact-query
@sukvvon sukvvon force-pushed the test/react-preact-query-useSuspenseQueries-inline-test-helpers branch from 50f9ef1 to cacc758 Compare March 29, 2026 17:30
@sukvvon sukvvon marked this pull request as ready for review March 29, 2026 17:43
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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx`:
- Around line 115-118: The test incorrectly passes the query function to
queryClient.setQueryData (queryClient.setQueryData(key, queryFn)), which treats
it as an updater and caches a Promise; change it to pass the resolved data
instead by invoking and awaiting the function (or directly supplying the
resolved value) so setQueryData receives the resolved result for the key
(reference symbols: key, queryFn, queryClient.setQueryData).

In `@packages/react-query/src/__tests__/useSuspenseQueries.test.tsx`:
- Around line 111-114: The test is seeding the cache with the query function
itself (queryFn) so setQueryData treats it as an updater and stores the Promise
instead of the resolved value; change the seed to the resolved value by calling
queryFn() and awaiting it (or simply pass the literal resolved value 1) when
calling queryClient.setQueryData so the cache contains the resolved data rather
than a function/Promise.
🪄 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: 38a141fc-f2e2-4510-99e3-fd44f3c1b72d

📥 Commits

Reviewing files that changed from the base of the PR and between 9e1bb94 and cacc758.

📒 Files selected for processing (2)
  • packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx
  • packages/react-query/src/__tests__/useSuspenseQueries.test.tsx

Comment on lines +115 to +118
const key = queryKey()
const queryFn = () => sleep(1000).then(() => 1)

queryClient.setQueryData(query.queryKey, query.queryFn)
queryClient.setQueryData(key, queryFn)
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.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C5 --iglob 'packages/**/queryClient.ts' '\bsetQueryData\b|functionalUpdate'
rg -n -C4 --iglob 'packages/**/utils.ts' '\bfunctionalUpdate\b'

Repository: TanStack/query

Length of output: 5743


🏁 Script executed:

cat -n packages/query-core/src/utils.ts | sed -n '98,110p'

Repository: TanStack/query

Length of output: 511


🏁 Script executed:

cat -n packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx | sed -n '110,125p'

Repository: TanStack/query

Length of output: 700


🏁 Script executed:

cat -n packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx | sed -n '114,135p'

Repository: TanStack/query

Length of output: 765


🏁 Script executed:

rg -n 'already fetched|already cached' packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx

Repository: TanStack/query

Length of output: 139


🏁 Script executed:

cat -n packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx | sed -n '114,145p'

Repository: TanStack/query

Length of output: 1076


🏁 Script executed:

rg -n 'suspend|Suspense|throw' packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx | head -20

Repository: TanStack/query

Length of output: 982


🏁 Script executed:

rg -n -A10 'useSuspenseQueries.*=' packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx | head -40

Repository: TanStack/query

Length of output: 606


🏁 Script executed:

fd -e ts -e tsx packages/preact-query/src --exec grep -l 'useSuspenseQueries' {} \; | head -5

Repository: TanStack/query

Length of output: 463


🏁 Script executed:

fd --full-path 'packages/preact-query/src' -e ts -e tsx | grep -i suspense

Repository: TanStack/query

Length of output: 695


🏁 Script executed:

rg -n 'const onSuspend|toHaveBeenCalled\(\)' packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx | head -15

Repository: TanStack/query

Length of output: 364


Pass resolved data to setQueryData, not the query function.

Line 118 passes queryFn to setQueryData, which treats it as an updater function (via functionalUpdate). This executes queryFn() immediately and caches the Promise it returns, rather than caching the resolved data. The test intent—verifying behavior when data is "already fetched"—requires resolved data in the cache, not a pending Promise.

🧪 Proposed fix
-    queryClient.setQueryData(key, queryFn)
+    queryClient.setQueryData(key, 1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const key = queryKey()
const queryFn = () => sleep(1000).then(() => 1)
queryClient.setQueryData(query.queryKey, query.queryFn)
queryClient.setQueryData(key, queryFn)
const key = queryKey()
const queryFn = () => sleep(1000).then(() => 1)
queryClient.setQueryData(key, 1)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx` around lines
115 - 118, The test incorrectly passes the query function to
queryClient.setQueryData (queryClient.setQueryData(key, queryFn)), which treats
it as an updater and caches a Promise; change it to pass the resolved data
instead by invoking and awaiting the function (or directly supplying the
resolved value) so setQueryData receives the resolved result for the key
(reference symbols: key, queryFn, queryClient.setQueryData).

Comment on lines +111 to +114
const key = queryKey()
const queryFn = () => sleep(1000).then(() => 1)

queryClient.setQueryData(key, queryFn)
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.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C5 --iglob 'packages/**/queryClient.ts' '\bsetQueryData\b|functionalUpdate'
rg -n -C4 --iglob 'packages/**/utils.ts' '\bfunctionalUpdate\b'

Repository: TanStack/query

Length of output: 5743


🏁 Script executed:

fd -t f 'useSuspenseQueries.test.tsx' packages/react-query/src

Repository: TanStack/query

Length of output: 120


🏁 Script executed:

sed -n '105,120p' packages/react-query/src/__tests__/useSuspenseQueries.test.tsx

Repository: TanStack/query

Length of output: 578


🏁 Script executed:

rg -n 'setQueryData.*queryFn' packages/react-query/src/__tests__/useSuspenseQueries.test.tsx

Repository: TanStack/query

Length of output: 104


🏁 Script executed:

sed -n '108,135p' packages/react-query/src/__tests__/useSuspenseQueries.test.tsx

Repository: TanStack/query

Length of output: 732


🏁 Script executed:

rg -n 'setQueryData' packages/react-query/src/__tests__/useSuspenseQueries.test.tsx | head -20

Repository: TanStack/query

Length of output: 712


Seed the cache with resolved data, not the query function.

Line 114 passes queryFn to setQueryData, but setQueryData invokes the functionalUpdate utility, which treats functions as updaters and calls them. This seeds the cache with the Promise returned by queryFn(), not with the resolved value 1. The test fails to exercise the cached-data path as intended.

Fix
-    queryClient.setQueryData(key, queryFn)
+    queryClient.setQueryData(key, 1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const key = queryKey()
const queryFn = () => sleep(1000).then(() => 1)
queryClient.setQueryData(key, queryFn)
const key = queryKey()
const queryFn = () => sleep(1000).then(() => 1)
queryClient.setQueryData(key, 1)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-query/src/__tests__/useSuspenseQueries.test.tsx` around lines
111 - 114, The test is seeding the cache with the query function itself
(queryFn) so setQueryData treats it as an updater and stores the Promise instead
of the resolved value; change the seed to the resolved value by calling
queryFn() and awaiting it (or simply pass the literal resolved value 1) when
calling queryClient.setQueryData so the cache contains the resolved data rather
than a function/Promise.

@sukvvon sukvvon merged commit afb5812 into main Mar 29, 2026
8 checks passed
@sukvvon sukvvon deleted the test/react-preact-query-useSuspenseQueries-inline-test-helpers branch March 29, 2026 17:53
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