/* eslint-disable @typescript-eslint/unbound-method */ import type * as SentryBrowser from '@sentry/browser'; import { createTransport, getCurrentScope, setCurrentClient } from '@sentry/core'; import { render } from '@solidjs/testing-library'; import userEvent from '@testing-library/user-event'; import { ErrorBoundary } from 'solid-js'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { BrowserClient, withSentryErrorBoundary } from '../../src/client'; const mockCaptureException = vi.fn(); vi.mock('@sentry/browser', async () => { const actual = await vi.importActual('@sentry/browser'); return { ...actual, captureException: (...args) => mockCaptureException(...args), } as typeof SentryBrowser; }); const user = userEvent.setup(); const SentryErrorBoundary = withSentryErrorBoundary(ErrorBoundary); describe('withSentryErrorBoundary', () => { function createMockBrowserClient(): BrowserClient { return new BrowserClient({ integrations: [], tracesSampleRate: 1, transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => Promise.resolve({})), stackParser: () => [], }); } beforeEach(() => { vi.clearAllMocks(); const client = createMockBrowserClient(); setCurrentClient(client); }); afterEach(() => { getCurrentScope().setClient(undefined); }); it('calls `captureException` when an error occurs`', () => { render(() => ( Ooops, an error occurred.}> {/* @ts-expect-error: component doesn't exist on purpose */} )); expect(mockCaptureException).toHaveBeenCalledTimes(1); expect(mockCaptureException).toHaveBeenLastCalledWith(new ReferenceError('NonExistentComponent is not defined'), { mechanism: { handled: true, type: 'auto.function.solid.error_boundary', }, }); }); it('renders the fallback component', async () => { const { findByText } = render(() => ( Ooops, an error occurred.}> {/* @ts-expect-error: component doesn't exist on purpose */} )); expect(await findByText('Ooops, an error occurred.')).toBeInTheDocument(); }); it('passes the `error` and `reset` function to the fallback component', () => { const mockFallback = vi.fn(); render(() => ( {/* @ts-expect-error: component doesn't exist on purpose */} )); expect(mockFallback).toHaveBeenCalledTimes(1); expect(mockFallback).toHaveBeenCalledWith( new ReferenceError('NonExistentComponent is not defined'), expect.any(Function), ); }); it('calls `captureException` again after resetting', async () => { const { findByRole } = render(() => ( }> {/* @ts-expect-error: component doesn't exist on purpose */} )); expect(mockCaptureException).toHaveBeenCalledTimes(1); expect(mockCaptureException).toHaveBeenNthCalledWith(1, new ReferenceError('NonExistentComponent is not defined'), { mechanism: { handled: true, type: 'auto.function.solid.error_boundary', }, }); const button = await findByRole('button'); await user.click(button); expect(mockCaptureException).toHaveBeenCalledTimes(2); expect(mockCaptureException).toHaveBeenNthCalledWith(2, new ReferenceError('NonExistentComponent is not defined'), { mechanism: { handled: true, type: 'auto.function.solid.error_boundary', }, }); }); it('renders children when there is no error', async () => { const { queryByText } = render(() => ( Oops, an error occurred.}>
Adopt a cat
)); expect(await queryByText('Adopt a cat')).toBeInTheDocument(); expect(await queryByText('Ooops, an error occurred')).not.toBeInTheDocument(); }); });