sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

SpyOn captureException no longer works in sentry/react 8

Open mikecousins opened this issue 1 year ago • 3 comments

Environment

SaaS (https://sentry.io/)

Steps to Reproduce

const captureExceptionSpy = vi.spyOn(Sentry, 'captureException');

Expected Result

Works fine

Actual Result

TypeError: Cannot redefine property: captureException

Product Area

Unknown

Link

No response

DSN

No response

Version

8.10.0

mikecousins avatar Jun 20 '24 02:06 mikecousins

Assigning to @getsentry/support for routing ⏲️

getsantry[bot] avatar Jun 20 '24 02:06 getsantry[bot]

Hi @mikecousins thanks for writing in. I think vitest is using the ESM build of the SDK here (because we are finally publishing them properly). This means that you are running into the fact that ESM modules are immutable (by the spec itself), so you can't rebind them via vi.spyOn.

I recommend using vi.mock instead. Docs here. This is a safer pattern that will work with all ESM libraries.

AbhiPrasad avatar Jun 20 '24 14:06 AbhiPrasad

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

getsantry[bot] avatar Aug 24 '24 07:08 getsantry[bot]