import { expect, test } from '@playwright/test'; import { getSpanOp, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils'; test('Sends client-side error to Sentry with auto-instrumentation', async ({ page }) => { const errorEventPromise = waitForError('tanstackstart-react', errorEvent => { return errorEvent?.exception?.values?.[0]?.value === 'Sentry Client Test Error'; }); await page.goto(`/`); // Wait for React to hydrate (see __root.tsx) before clicking — the SSR HTML // renders the button before the onClick handler is attached, and clicking // pre-hydration would fire no handler and produce no error. await page.locator('html[data-hydrated="true"]').waitFor(); await expect(page.locator('button').filter({ hasText: 'Break the client' })).toBeVisible(); await page.locator('button').filter({ hasText: 'Break the client' }).click(); const errorEvent = await errorEventPromise; expect(errorEvent).toMatchObject({ exception: { values: [ { type: 'Error', value: 'Sentry Client Test Error', mechanism: { handled: false, }, }, ], }, }); expect(errorEvent.transaction).toBe('/'); }); test('Sends server-side function error to Sentry with auto-instrumentation', async ({ page }) => { const errorEventPromise = waitForError('tanstackstart-react', errorEvent => { // The thrown error propagates back to the client over the server-function RPC and is also // captured there as an `onunhandledrejection` with the same message. Match on the server-function // mechanism so we deterministically pick the server-side event instead of racing the client one. return ( errorEvent?.exception?.values?.[0]?.value === 'Sentry Server Function Test Error' && errorEvent?.exception?.values?.[0]?.mechanism?.type === 'auto.middleware.tanstackstart.server_function' ); }); await page.goto(`/`); await expect(page.locator('button').filter({ hasText: 'Break server function' })).toBeVisible(); await page.locator('button').filter({ hasText: 'Break server function' }).click(); const errorEvent = await errorEventPromise; expect(errorEvent).toMatchObject({ exception: { values: [ { type: 'Error', value: 'Sentry Server Function Test Error', mechanism: { type: 'auto.middleware.tanstackstart.server_function', handled: false, }, }, ], }, }); expect(errorEvent.transaction).toEqual(expect.stringContaining('GET /_serverFn/')); }); test('Sends API route error to Sentry with auto-instrumentation', async ({ page }) => { const errorEventPromise = waitForError('tanstackstart-react', errorEvent => { // As with the server-function test, guard against a same-message client-side duplicate by // matching the server request mechanism, so we always assert against the server-side event. return ( errorEvent?.exception?.values?.[0]?.value === 'Sentry API Route Test Error' && errorEvent?.exception?.values?.[0]?.mechanism?.type === 'auto.middleware.tanstackstart.request' ); }); await page.goto(`/`); await expect(page.locator('button').filter({ hasText: 'Break API route' })).toBeVisible(); await page.locator('button').filter({ hasText: 'Break API route' }).click(); const errorEvent = await errorEventPromise; expect(errorEvent).toMatchObject({ exception: { values: [ { type: 'Error', value: 'Sentry API Route Test Error', mechanism: { type: 'auto.middleware.tanstackstart.request', handled: false, }, }, ], }, }); expect(errorEvent.transaction).toBe('GET /api/error'); }); // the sentry global middleware does not capture errors from SSR loader errors since they are serialized before they reach the middleware layer // this test verifies that the error is in fact not sent to Sentry test('Does not send SSR loader error to Sentry', async ({ baseURL, page }) => { let errorEventOccurred = false; waitForError('tanstackstart-react', event => { if (!event.type && event.exception?.values?.[0]?.value === 'Sentry SSR Test Error') { errorEventOccurred = true; } return false; }); const serverSpanPromise = waitForStreamedSpan('tanstackstart-react', span => { return span.is_segment && getSpanOp(span) === 'http.server' && span.attributes['url.path']?.value === '/ssr-error'; }); await page.goto('/ssr-error'); await serverSpanPromise; await (await fetch(`${baseURL}/api/flush`)).text(); expect(errorEventOccurred).toBe(false); });