Skip to content

test({react,preact}-query): add type tests for 'useMutation'#10482

Merged
sukvvon merged 2 commits intomainfrom
test/react-query-add-useMutation-type-tests
Apr 13, 2026
Merged

test({react,preact}-query): add type tests for 'useMutation'#10482
sukvvon merged 2 commits intomainfrom
test/react-query-add-useMutation-type-tests

Conversation

@sukvvon
Copy link
Copy Markdown
Collaborator

@sukvvon sukvvon commented Apr 13, 2026

🎯 Changes

  • Add 11 type tests for useMutation in both react-query and preact-query (identical signatures), covering all 4 generic parameters:
    • TData: inference from mutationFn return type, explicit generic
    • TError: default DefaultError, custom error type, onError/onSettled callbacks
    • TVariables: inference from mutationFn parameter, default void
    • TOnMutateResult: inference from onMutate return type, onSuccess/onError callbacks
    • Return type: UseMutationResult, mutate, mutateAsync
    • queryClient as optional second argument

✅ 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 TypeScript type-validation tests for the useMutation hook in both React and Preact packages, verifying generic type inference (data, variables, error), explicit generics, mutate/mutateAsync signatures, onMutate/onSuccess/onError/onSettled callback typings, error-defaulting/custom errors, and overload behavior with an optional client.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 13, 2026

📝 Walkthrough

Walkthrough

Added two new TypeScript declaration test files that validate compile-time type inference and callback typings for useMutation across react- and preact-query packages, covering TData, TVariables, TError, TOnMutateResult, explicit generics, mutateAsync, onSettled, and QueryClient overload behavior.

Changes

Cohort / File(s) Summary
React type-declaration tests
packages/react-query/src/__tests__/useMutation.test-d.tsx
New 138-line TypeScript declaration test using expectTypeOf to verify useMutation generic inference, callback parameter types, mutate/mutateAsync signatures, explicit generics, error typing, onSettled params, and the QueryClient overload.
Preact type-declaration tests
packages/preact-query/src/__tests__/useMutation.test-d.tsx
New 138-line TypeScript declaration test mirroring the react package checks for useMutation type inference, callbacks, explicit generics, mutateAsync behavior, error defaults/customization, onSettled params, and QueryClient overload.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐇
With nimble paws I hop and test,
Types aligned and code addressed.
Mutations sing, the compiler beams,
Variables neat in tidy streams.
A rabbit's cheer for type-safe dreams.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The description follows the template structure with all required sections completed, including detailed changes, checklist items marked, and release impact clearly indicated.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly and concisely describes the main change: adding type tests for useMutation across both react-query and preact-query packages.

✏️ 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-query-add-useMutation-type-tests

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

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

nx-cloud Bot commented Apr 13, 2026

View your CI Pipeline Execution ↗ for commit eae84f4

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

☁️ Nx Cloud last updated this comment at 2026-04-13 15:54:56 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 13, 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 Apr 13, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/preact-query

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

@tanstack/preact-query-devtools

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

@tanstack/preact-query-persist-client

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: eae84f4

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 13, 2026

size-limit report 📦

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

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/react-query/src/__tests__/useMutation.test-d.tsx (1)

82-89: Optional cleanup: deduplicate CustomError test type.

CustomError is declared twice; extracting one shared declaration will reduce repetition.

♻️ Proposed refactor
 describe('useMutation', () => {
+  class CustomError extends Error {
+    code: number
+    constructor(code: number) {
+      super()
+      this.code = code
+    }
+  }
+
   it('should infer custom TError type', () => {
-    class CustomError extends Error {
-      code: number
-      constructor(code: number) {
-        super()
-        this.code = code
-      }
-    }
-
     const mutation = useMutation<string, CustomError>({
       mutationFn: () => Promise.resolve('data'),
     })
@@
   it('should infer custom TError in onError callback', () => {
-    class CustomError extends Error {
-      code: number
-      constructor(code: number) {
-        super()
-        this.code = code
-      }
-    }
-
     useMutation<string, CustomError>({
       mutationFn: () => Promise.resolve('data'),
       onError: (error) => {

Also applies to: 109-116

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-query/src/__tests__/useMutation.test-d.tsx` around lines 82 -
89, Tests duplicate the CustomError class declaration in two places (lines
around the tests starting with "should infer custom TError type"); refactor by
extracting a single shared CustomError declaration (class CustomError extends
Error { code: number; constructor(code: number) { super(); this.code = code } })
into the shared scope of the test file (e.g., top of the describe or
module-level) and remove the duplicated declaration in both test blocks so both
tests reuse the same CustomError symbol.
🤖 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/react-query/src/__tests__/useMutation.test-d.tsx`:
- Around line 82-89: Tests duplicate the CustomError class declaration in two
places (lines around the tests starting with "should infer custom TError type");
refactor by extracting a single shared CustomError declaration (class
CustomError extends Error { code: number; constructor(code: number) { super();
this.code = code } }) into the shared scope of the test file (e.g., top of the
describe or module-level) and remove the duplicated declaration in both test
blocks so both tests reuse the same CustomError symbol.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e9e8ba61-2262-472d-94ad-7e87e4256d52

📥 Commits

Reviewing files that changed from the base of the PR and between 479b9a5 and 2fa62f4.

📒 Files selected for processing (1)
  • packages/react-query/src/__tests__/useMutation.test-d.tsx

@sukvvon sukvvon changed the title test(react-query/useMutation): add type tests for 'useMutation' test(react-query, preact-query/useMutation): add type tests for 'useMutation' Apr 13, 2026
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/preact-query/src/__tests__/useMutation.test-d.tsx (1)

83-89: Optional: deduplicate CustomError fixture used in two tests.

CustomError is declared twice (Line 83 and Line 110). Extracting it once at describe scope reduces duplication/drift.

♻️ Proposed refactor
 describe('useMutation', () => {
+  class CustomError extends Error {
+    code: number
+    constructor(code: number) {
+      super()
+      this.code = code
+    }
+  }
+
   it('should infer custom TError type', () => {
-    class CustomError extends Error {
-      code: number
-      constructor(code: number) {
-        super()
-        this.code = code
-      }
-    }
-
     const mutation = useMutation<string, CustomError>({
       mutationFn: () => Promise.resolve('data'),
     })
@@
   it('should infer custom TError in onError callback', () => {
-    class CustomError extends Error {
-      code: number
-      constructor(code: number) {
-        super()
-        this.code = code
-      }
-    }
-
     useMutation<string, CustomError>({
       mutationFn: () => Promise.resolve('data'),
       onError: (error) => {

Also applies to: 110-116

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/preact-query/src/__tests__/useMutation.test-d.tsx` around lines 83 -
89, CustomError is duplicated in two test blocks; hoist the class declaration
into the surrounding describe scope so both tests reuse the same fixture: remove
the duplicate class at the later location and place a single declaration for
class CustomError extends Error { code: number; constructor(code: number) {
super(); this.code = code } } at the top of the describe that contains these
tests, ensuring both tests reference the same CustomError symbol.
🤖 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/preact-query/src/__tests__/useMutation.test-d.tsx`:
- Around line 83-89: CustomError is duplicated in two test blocks; hoist the
class declaration into the surrounding describe scope so both tests reuse the
same fixture: remove the duplicate class at the later location and place a
single declaration for class CustomError extends Error { code: number;
constructor(code: number) { super(); this.code = code } } at the top of the
describe that contains these tests, ensuring both tests reference the same
CustomError symbol.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 89f11651-7a93-45bb-872f-9e70c566fd3e

📥 Commits

Reviewing files that changed from the base of the PR and between 2fa62f4 and eae84f4.

📒 Files selected for processing (1)
  • packages/preact-query/src/__tests__/useMutation.test-d.tsx

@sukvvon sukvvon changed the title test(react-query, preact-query/useMutation): add type tests for 'useMutation' test({react,preact}-query/useMutation): add type tests for 'useMutation' Apr 13, 2026
@sukvvon sukvvon changed the title test({react,preact}-query/useMutation): add type tests for 'useMutation' test({react,preact}-query): add type tests for 'useMutation' Apr 13, 2026
@sukvvon sukvvon merged commit ffa4164 into main Apr 13, 2026
8 checks passed
@sukvvon sukvvon deleted the test/react-query-add-useMutation-type-tests branch April 13, 2026 16:02
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