Skip to content

Conversation

@Nick-Lucas
Copy link
Contributor

@Nick-Lucas Nick-Lucas commented Oct 28, 2025

Closes #6988

Fixes issue where mutationOptions() calls and potentially queryOptions and infiniteQueryOptions could fail when no opts are passed in

🎯 Changes

What changes are made in this PR? Is it a feature or a bug fix?

✅ Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

Summary by CodeRabbit

  • Improvements

    • Query, mutation, and infinite-query option helpers now accept optional parameters and safely handle missing options.
  • Tests

    • Tests for query and mutation options updated to run parameterized scenarios covering presence or absence of a key prefix.

@Nick-Lucas Nick-Lucas requested a review from a team as a code owner October 28, 2025 10:23
@railway-app
Copy link

railway-app bot commented Oct 28, 2025

🚅 Deployed to the trpc-pr-6990 environment in trpc-sse-and-websockets

Service Status Web Updated (UTC)
next-prisma-websockets-starter ◻️ Removed (View Logs) Oct 28, 2025 at 10:40 am
next-sse-chat ◻️ Removed (View Logs) Web Oct 28, 2025 at 10:40 am

@railway-app railway-app bot temporarily deployed to next-sse-chat (trpc-sse-and-websockets / trpc-pr-6990) October 28, 2025 10:23 Destroyed
@railway-app railway-app bot temporarily deployed to next-prisma-websockets-starter (trpc-sse-and-websockets / trpc-pr-6990) October 28, 2025 10:23 Destroyed
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 28, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • packages/tanstack-react-query/test/__snapshots__/queryOptions.test.tsx.snap is excluded by !**/*.snap

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Made the opts parameter optional for trpc query/mutation/infinite option helpers and updated callers/tests to accept and exercise optional keyPrefix (parameterized tests run with and without a prefix).

Changes

Cohort / File(s) Summary
Core options handlers
packages/tanstack-react-query/src/internals/infiniteQueryOptions.ts, packages/tanstack-react-query/src/internals/mutationOptions.ts, packages/tanstack-react-query/src/internals/queryOptions.ts
Changed opts parameter types to allow undefined (e.g., `AnyTRPC...OptionsIn
Tests (parameterized keyPrefix)
packages/tanstack-react-query/test/mutationOptions.test.tsx, packages/tanstack-react-query/test/queryOptions.test.tsx
testContext now accepts optional keyPrefix; suites converted to describe.each([...]) to run tests with and without a keyPrefix, and pass the value into testReactResource(...).
Manifest
package.json
Manifest file listed/updated in diff (no functional detail in summary).

Sequence Diagram(s)

(omitted — changes adjust parameter handling and tests without introducing new control-flow interactions that require a sequence diagram)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay special attention to packages/tanstack-react-query/src/internals/queryOptions.ts to confirm any spread of opts is guarded (e.g., opts ?? {}) to avoid runtime errors.
  • Verify consistent use of optional chaining (opts?.keyPrefix) and that types remain correct for exported helpers.
  • Check updated tests parameterization correctly covers both keyPrefix === string and undefined scenarios and that test harness invocation matches runtime usage.

Possibly related PRs

Suggested labels

👋 free for all

Suggested reviewers

  • juliusmarminge

Poem

🐰 I hopped through opts both null and filled,
I padded spreads so crashes stilled,
Tests now run with prefix or none,
Hooks stay safe — the job is done. ✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The PR description follows the required template structure and includes the essential elements: a reference to the closed issue (#6988) and a brief explanation of the fix at the top. However, the "🎯 Changes" section, which is a required part of the template, contains only the template placeholder question "What changes are made in this PR?" without substantive details about the actual code modifications made in the changeset. While the overall intent is clear from the brief description, the Changes section lacks the expected content describing what was modified. The ✅ Checklist is properly formatted but items are not marked as completed.
Docstring Coverage ⚠️ Warning Docstring coverage is 13.33% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "fix(tanstack-react-query): keyPrefix of undefined" directly addresses the core issue being fixed—the error "Cannot read properties of undefined (reading 'keyPrefix')"—which aligns with the changeset that makes opts optional and adds safe handling for undefined values in mutationOptions, queryOptions, and infiniteQueryOptions. The title uses the "fix()" prefix appropriately to indicate a bug fix. However, the title could be more descriptive about the actual solution (making opts optional) rather than just naming the error symptom.
Linked Issues Check ✅ Passed The linked issue #6988 reports a client-side crash with the error "Cannot read properties of undefined (reading 'keyPrefix')" after upgrading to trpc 11.7.0, with the implicit objective of safely handling undefined opts to prevent this crash. The PR directly addresses this by making the opts parameter optional in mutationOptions, queryOptions, and infiniteQueryOptions, adding optional chaining and nullish coalescing to safely access properties, and extending tests to verify behavior when opts is undefined. These changes prevent the error by ensuring undefined opts values are handled gracefully rather than causing runtime exceptions.
Out of Scope Changes Check ✅ Passed All code changes in the PR are directly related to the scope of fixing the undefined opts handling issue. The modifications to mutationOptions.ts, queryOptions.ts, and infiniteQueryOptions.ts make opts optional and add safe property access patterns specifically to prevent the keyPrefix undefined error. The test changes in mutationOptions.test.tsx and queryOptions.test.tsx are directly supporting the new functionality by adding parameterized tests that verify behavior with both defined and undefined keyPrefix values. No unrelated refactoring, cosmetic changes, or feature additions outside the scope of this bug fix are present.

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.

❤️ Share

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


expect(calls).toEqual(['onMutate', 'onSuccess', 'onSettled']);
});
test('useMutation with no opts', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the failing case. opts might not be set but our internal types taking from the proxy were assuming they always will, so opts.keyPrefix was erroring

@vercel
Copy link

vercel bot commented Oct 28, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
next-prisma-starter Ready Ready Preview Oct 28, 2025 10:38am
og-image Ready Ready Preview Comment Oct 28, 2025 10:38am
www Ready Ready Preview Comment Oct 28, 2025 10:38am

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 28, 2025

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/trpc/trpc/@trpc/client@6990

@trpc/next

npm i https://pkg.pr.new/trpc/trpc/@trpc/next@6990

@trpc/react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/react-query@6990

@trpc/server

npm i https://pkg.pr.new/trpc/trpc/@trpc/server@6990

@trpc/tanstack-react-query

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

@trpc/upgrade

npm i https://pkg.pr.new/trpc/trpc/@trpc/upgrade@6990

commit: 225097f

Copy link
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: 0

🧹 Nitpick comments (2)
packages/tanstack-react-query/src/internals/mutationOptions.ts (1)

130-145: Consider using nullish coalescing for consistency.

Spreading undefined at line 131 works correctly in JavaScript, but for consistency with infiniteQueryOptions.ts (line 291), consider using:

 return {
-  ...opts,
+  ...(opts ?? {}),
   mutationKey,

This makes the intent more explicit and aligns with the pattern used in the infinite query options.

packages/tanstack-react-query/src/internals/queryOptions.ts (1)

257-264: Consider using nullish coalescing for consistency.

Spreading undefined at line 259 works correctly, but for consistency with infiniteQueryOptions.ts (line 291), consider:

 return Object.assign(
   queryOptions({
-    ...opts,
+    ...(opts ?? {}),
     queryKey: queryKey,
     queryFn: inputIsSkipToken ? skipToken : queryFn,
   }),
   { trpc: createTRPCOptionsResult({ path }) },
 );

This makes the intent more explicit and aligns with the pattern used elsewhere.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ad18f6 and 386635d.

⛔ Files ignored due to path filters (1)
  • packages/tanstack-react-query/test/__snapshots__/queryOptions.test.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (5)
  • packages/tanstack-react-query/src/internals/infiniteQueryOptions.ts (2 hunks)
  • packages/tanstack-react-query/src/internals/mutationOptions.ts (1 hunks)
  • packages/tanstack-react-query/src/internals/queryOptions.ts (1 hunks)
  • packages/tanstack-react-query/test/mutationOptions.test.tsx (2 hunks)
  • packages/tanstack-react-query/test/queryOptions.test.tsx (3 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
packages/tanstack-react-query/**/*.test.tsx

📄 CodeRabbit inference engine (.cursor/rules/tanstack-react-query-tests.mdc)

packages/tanstack-react-query/**/*.test.tsx: ALWAYS use await using ctx = testReactResource(...) to set up TanStack React Query tests
Import testReactResource from the local path ./__helpers
Do NOT import testReactResource from ./test/__helpers (wrong path)
Do NOT import legacy helpers like createAppRouter from ./__testHelpers
Use ctx.useTRPC() for TanStack React Query hooks access in components under test
Use ctx.useTRPCClient() for direct (vanilla) tRPC client access in tests
Use ctx.optionsProxyClient when testing client-side options proxy
Use ctx.optionsProxyServer when testing server-side options proxy
Render via ctx.renderApp() and rerender via ctx.rerenderApp() instead of custom providers
Import testing utilities from @testing-library/react
Import user interaction utilities from @testing-library/user-event when simulating interactions
Import React Query types from @tanstack/react-query as needed
Import TanStack React Query utilities from the package source via ../src
Import React testing utilities with import * as React from 'react';

Files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/coding-guidelines.mdc)

**/*.{ts,tsx}: Always use import type for type-only imports
Separate type imports from value imports
Avoid overzealous object destructuring; prefer direct property access
Object destructuring is acceptable when a variable is used 3+ times
Prefer array destructuring
Avoid sparse array destructuring
Never destructure in function parameter declarations
Avoid destructuring potentially nullish nested properties
Maximum 3 parameters per function; use options objects when more
Type parameter names must match /^(T|$)(A-Z)?[0-9]*$/
Prefix unused variables, parameters, and caught errors with _
Prefer namespace imports for validation libraries and large modules (e.g., import * as z from 'zod', import * as React from 'react')
Follow import order: test helpers, tRPC test imports, third-party, then relative
Never import from @trpc/*/src; import from the package root instead
Do not use Symbol.dispose or Symbol.asyncDispose; use makeResource()/makeAsyncResource()
Always use await using for resource cleanup
Prefer makeResource()/makeAsyncResource() over manual disposal logic
Avoid non-null assertions (!)
Use proper type guards and optional chaining instead of non-null assertions
Switch statements must be exhaustive for union types
Rely on TypeScript inference; avoid unnecessary explicit return/output types
Use explicit types at public API boundaries, for complex generic constraints, or when inference is insufficient/ambiguous
Use the satisfies operator to retain inference while enforcing shapes
Use as const for literal type inference when appropriate
Prefer explicit typing over any
Use type assertions sparingly
Leverage TypeScript strict mode features

Files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/src/internals/mutationOptions.ts
  • packages/tanstack-react-query/src/internals/infiniteQueryOptions.ts
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
  • packages/tanstack-react-query/src/internals/queryOptions.ts
**/*.{ts,tsx,md,mdx}

📄 CodeRabbit inference engine (.cursor/rules/coding-guidelines.mdc)

Use camelCase for file names (with exceptions like TRPC/RPC/HTTP/JSON acronyms, .config.js, .d.ts, and tests)

Files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/src/internals/mutationOptions.ts
  • packages/tanstack-react-query/src/internals/infiniteQueryOptions.ts
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
  • packages/tanstack-react-query/src/internals/queryOptions.ts
{**/*.test.{ts,tsx},**/__tests__/**/*.{ts,tsx}}

📄 CodeRabbit inference engine (.cursor/rules/coding-guidelines.mdc)

{**/*.test.{ts,tsx},**/__tests__/**/*.{ts,tsx}}: Do not use Testing Library waitFor; use vi.waitFor instead
Tests may use non-null assertions
Tests may have unused variables
Tests may allow floating promises

Files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
packages/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/coding-guidelines.mdc)

No console.log in packages; use proper logging instead

Files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/src/internals/mutationOptions.ts
  • packages/tanstack-react-query/src/internals/infiniteQueryOptions.ts
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
  • packages/tanstack-react-query/src/internals/queryOptions.ts
**/*.tsx

📄 CodeRabbit inference engine (.cursor/rules/coding-guidelines.mdc)

**/*.tsx: Follow React Hooks rules (React Compiler compatible patterns)
Use the JSX runtime (no need to import React for JSX)
Prefer function components

Files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
🧠 Learnings (18)
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Use `ctx.optionsProxyClient` when testing client-side options proxy

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Use `ctx.optionsProxyServer` when testing server-side options proxy

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Use `ctx.useTRPC()` for TanStack React Query hooks access in components under test

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
  • packages/tanstack-react-query/src/internals/queryOptions.ts
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : ALWAYS use `await using ctx = testReactResource(...)` to set up TanStack React Query tests

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Render via `ctx.renderApp()` and rerender via `ctx.rerenderApp()` instead of custom providers

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Do NOT import `testReactResource` from `./test/__helpers` (wrong path)

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Import `testReactResource` from the local path `./__helpers`

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Import React Query types from `tanstack/react-query` as needed

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Use `ctx.useTRPCClient()` for direct (vanilla) tRPC client access in tests

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
  • packages/tanstack-react-query/src/internals/queryOptions.ts
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Import React testing utilities with `import * as React from 'react';`

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
📚 Learning: 2025-09-05T15:16:01.878Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:01.878Z
Learning: Applies to packages/react-query/**/*.test.tsx : Create the tRPC React client with createTRPCReact<typeof appRouter>()

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:01.878Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:01.878Z
Learning: Applies to packages/react-query/**/*.test.tsx : Wrap components with a custom App that provides trpc.Provider (legacy) and QueryClientProvider

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
📚 Learning: 2025-09-05T15:16:01.878Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:01.878Z
Learning: Applies to packages/react-query/**/*.test.tsx : Manage test resources with konn(): call createAppRouter() in beforeEach and ctx?.close?.() in afterEach

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:16:01.878Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:01.878Z
Learning: Applies to packages/react-query/**/*.test.tsx : Import createTRPCReact from 'trpc/react-query' (legacy API)

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/src/internals/queryOptions.ts
📚 Learning: 2025-09-05T15:17:32.520Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/upgrade-tests.mdc:0-0
Timestamp: 2025-09-05T15:17:32.520Z
Learning: Applies to packages/upgrade/**/*.{test,spec,trpc,snap}.tsx : For migration testing, exercise both legacy and modern patterns via ctx.rq and ctx.trq within the same test when relevant

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
📚 Learning: 2025-09-05T15:16:31.379Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/tanstack-react-query-tests.mdc:0-0
Timestamp: 2025-09-05T15:16:31.379Z
Learning: Applies to packages/tanstack-react-query/**/*.test.tsx : Do NOT import legacy helpers like `createAppRouter` from `./__testHelpers`

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/test/mutationOptions.test.tsx
📚 Learning: 2025-09-05T15:17:32.520Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/upgrade-tests.mdc:0-0
Timestamp: 2025-09-05T15:17:32.520Z
Learning: Applies to packages/upgrade/**/*.{test,spec,trpc,snap}.tsx : Import both 'trpc/react-query' as rq (legacy) and 'trpc/tanstack-react-query' as trq (modern) when writing migration tests

Applied to files:

  • packages/tanstack-react-query/test/queryOptions.test.tsx
  • packages/tanstack-react-query/src/internals/mutationOptions.ts
  • packages/tanstack-react-query/src/internals/queryOptions.ts
📚 Learning: 2025-09-05T15:17:32.520Z
Learnt from: CR
PR: trpc/trpc#0
File: .cursor/rules/upgrade-tests.mdc:0-0
Timestamp: 2025-09-05T15:17:32.520Z
Learning: Applies to packages/upgrade/**/*.{test,spec,trpc,snap}.tsx : Manage test resources with `await using ctx = testReactResource(...)` for setup/teardown

Applied to files:

  • packages/tanstack-react-query/test/mutationOptions.test.tsx
🧬 Code graph analysis (3)
packages/tanstack-react-query/test/queryOptions.test.tsx (1)
packages/tanstack-react-query/test/__helpers.tsx (1)
  • testReactResource (14-92)
packages/tanstack-react-query/src/internals/mutationOptions.ts (1)
packages/tanstack-react-query/src/internals/utils.ts (2)
  • unwrapLazyArg (201-203)
  • getMutationKeyInternal (183-196)
packages/tanstack-react-query/test/mutationOptions.test.tsx (1)
packages/tanstack-react-query/test/__helpers.tsx (1)
  • testReactResource (14-92)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: E2E-tests (soa)
  • GitHub Check: E2E-tests (next-formdata)
  • GitHub Check: E2E-tests (next-minimal-starter)
  • GitHub Check: E2E-tests (.test/internal-types-export)
  • GitHub Check: E2E-tests (minimal-react)
  • GitHub Check: E2E-tests (.test/ssg)
  • GitHub Check: E2E-tests (express-server)
  • GitHub Check: E2E-tests (.experimental/next-app-dir)
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
  • GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
  • GitHub Check: Test a monorepo using built declaration files
  • GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
  • GitHub Check: Release using pkg.pr.new
  • GitHub Check: typecheck-www
  • GitHub Check: E2E-tests (Deno) (deno-deploy)
  • GitHub Check: Lint and auto-fix
  • GitHub Check: test
🔇 Additional comments (9)
packages/tanstack-react-query/src/internals/infiniteQueryOptions.ts (2)

259-259: LGTM! Opts parameter now properly optional.

The parameter type change correctly allows undefined opts, which fixes the root cause of the "keyPrefix of undefined" error reported in issue #6988.


291-291: LGTM! Safe handling of undefined opts.

The nullish coalescing operator provides an explicit empty object when opts is undefined, ensuring the spread operation always has a valid object to work with.

packages/tanstack-react-query/src/internals/mutationOptions.ts (2)

104-104: LGTM! Opts parameter now properly optional.

The parameter type change correctly allows undefined opts, preventing the "keyPrefix of undefined" error.


112-112: LGTM! Optional chaining prevents the crash.

Using opts?.keyPrefix safely handles the case when opts is undefined, directly addressing the bug reported in issue #6988.

packages/tanstack-react-query/src/internals/queryOptions.ts (1)

226-226: LGTM! Opts parameter now properly optional.

The parameter type change correctly allows undefined opts, fixing the regression introduced in 11.7.0.

packages/tanstack-react-query/test/queryOptions.test.tsx (2)

17-62: LGTM! Excellent test parameterization.

The updated testContext signature and parameterized test suite effectively validate behavior with and without keyPrefix, ensuring the fix works in both scenarios.

As per coding guidelines.


64-97: LGTM! Comprehensive regression coverage.

The parameterized test structure verifies the fix for issue #6988 by exercising queryOptions calls without opts (line 73), which would previously cause the "keyPrefix of undefined" error.

packages/tanstack-react-query/test/mutationOptions.test.tsx (2)

9-46: LGTM! Excellent test parameterization.

The updated testContext signature and parameterized test suite effectively validate behavior with and without keyPrefix, ensuring comprehensive coverage of the fix.

As per coding guidelines.


105-157: LGTM! Critical regression test for the bug.

Line 114 explicitly tests mutationOptions() with no arguments, which directly exercises the scenario that caused the "keyPrefix of undefined" error in issue #6988. This test ensures the fix prevents the regression.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

path: string[];
queryKey: TRPCQueryKey<TFeatureFlags['keyPrefix']>;
opts: AnyTRPCQueryOptionsIn<TFeatureFlags>;
opts: AnyTRPCQueryOptionsIn<TFeatureFlags> | undefined;
Copy link

Choose a reason for hiding this comment

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

Bug: Undefined Options Spread Causes Runtime Errors

The opts parameter can now be undefined, but spreading ...opts without a null check causes a runtime error. This happens in trpcQueryOptions, trpcMutationOptions, and within the actualOpts creation in trpcInfiniteQueryOptions. The infiniteQueryOptions return statement already handles this correctly, which could be a good reference.

Additional Locations (2)

Fix in Cursor Fix in Web

@railway-app railway-app bot temporarily deployed to next-prisma-websockets-starter (trpc-sse-and-websockets / trpc-pr-6990) October 28, 2025 10:34 Destroyed
@railway-app railway-app bot temporarily deployed to next-sse-chat (trpc-sse-and-websockets / trpc-pr-6990) October 28, 2025 10:34 Destroyed
@Nick-Lucas Nick-Lucas force-pushed the 6988-keyprefix-undefined branch from c946059 to 225097f Compare October 28, 2025 10:34
@railway-app railway-app bot temporarily deployed to next-sse-chat (trpc-sse-and-websockets / trpc-pr-6990) October 28, 2025 10:34 Destroyed
@railway-app railway-app bot temporarily deployed to next-prisma-websockets-starter (trpc-sse-and-websockets / trpc-pr-6990) October 28, 2025 10:34 Destroyed
@Nick-Lucas Nick-Lucas enabled auto-merge (squash) October 28, 2025 10:39
@Nick-Lucas Nick-Lucas merged commit 3ae1c36 into main Oct 28, 2025
46 of 48 checks passed
@Nick-Lucas Nick-Lucas deleted the 6988-keyprefix-undefined branch October 28, 2025 10:40
@github-actions
Copy link
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Cannot read properties of undefined (reading 'keyPrefix')

3 participants