Skip to content

fix(react): Remove unused react.componentStack event context#21183

Open
zhongrenfei1-hub wants to merge 1 commit into
getsentry:developfrom
zhongrenfei1-hub:fix/react-component-stack-context-20094
Open

fix(react): Remove unused react.componentStack event context#21183
zhongrenfei1-hub wants to merge 1 commit into
getsentry:developfrom
zhongrenfei1-hub:fix/react-component-stack-context-20094

Conversation

@zhongrenfei1-hub
Copy link
Copy Markdown

Summary

Removes the react.componentStack event context set by captureReactException.

The shipped string never has sourcemaps applied, so it's mostly unreadable in the Sentry UI. For React >= 17 the same component stack is already attached via error.cause (which does get sourcemaps); for React < 17 it offers little value either. Maintainer-confirmed in #20094.

Root cause

captureReactException wrapped the capture in withScope solely to attach the react.componentStack context. With that context gone the withScope wrapper has no other mutation, so it collapses to a direct captureException call (and withScope drops out of the imports).

Test sync: scopeSetContextSpy in errorboundary.test.tsx is removed. The two expect(cause.stack).toEqual(scopeSetContextSpy.mock.calls[...]) assertions become expect.any(String) — the strongest invariant left now that the spy is gone, since the underlying setCause / errorBoundaryError.stack = componentStack chain is unchanged. The recursive-cause negation collapses into the existing cause.name assertion, with an inline comment explaining why the original cause is preserved when the chain loops.

Fixes #20094

The `react.componentStack` event context set by `captureReactException`
on every captured error never gets sourcemaps applied, so the string it
ships is mostly unreadable. For React >= 17 the same component stack is
already attached via `error.cause` (which does get sourcemaps); for
React < 17 it offers little value either.

Drop the `setContext('react', ...)` call. The surrounding `withScope`
wrapper has no other mutation, so collapse it to a direct
`captureException` call and drop the now-unused `withScope` import.

Sync the spy assertions in `errorboundary.test.tsx`: remove the
`scopeSetContextSpy` setup and rewrite the `cause.stack`-versus-mock
expectations to `expect.any(String)` — the strongest invariant we can
still observe once the spy is gone, since the underlying `setCause` /
`errorBoundaryError.stack = componentStack` chain is unchanged. The
negation case in the recursive-cause test collapses into the existing
`cause.name` assertion, with an inline comment explaining why the
original (non-ErrorBoundary) cause is preserved when the chain loops.

Closes getsentry#20094

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@zhongrenfei1-hub zhongrenfei1-hub requested a review from a team as a code owner May 27, 2026 08:10
@zhongrenfei1-hub zhongrenfei1-hub requested review from nicohrubec and s1gr1d and removed request for a team May 27, 2026 08:10
Copy link
Copy Markdown

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 89d1a79. Configure here.

mechanism: { handled: true, type: 'auto.function.react.error_boundary' },
});

expect(scopeSetContextSpy).toHaveBeenCalledTimes(1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Leftover mockClear() call on non-spy method breaks tests

High Severity

The vi.spyOn(mockScope, 'setContext') call was removed, but the afterEach block still calls (mockScope.setContext as any).mockClear(). Since mockScope.setContext is no longer a spy, it's a plain Scope method without a .mockClear() property — this will throw a TypeError at runtime, causing every test inside describe('ErrorBoundary', ...) to fail.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 89d1a79. Configure here.

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.

React: Don't set react.componentStack context

1 participant