|
| 1 | +import { describe, it, jest, expect, afterEach } from '@jest/globals'; |
| 2 | + |
| 3 | +import * as Sentry from '@sentry/browser'; |
| 4 | +import { |
| 5 | + initSentry, |
| 6 | + getSentry, |
| 7 | + getSentryHubWrapper, |
| 8 | +} from '../src/sentry'; |
| 9 | + |
| 10 | +jest.mock('@sentry/browser', () => ({ |
| 11 | + BrowserClient: jest.fn(), |
| 12 | + Hub: jest.fn(), |
| 13 | + Breadcrumbs: jest.fn(), |
| 14 | + GlobalHandlers: jest.fn(), |
| 15 | + LinkedErrors: jest.fn(), |
| 16 | + Dedupe: jest.fn(), |
| 17 | + HttpContext: jest.fn(), |
| 18 | + BrowserTracing: jest.fn(), |
| 19 | + makeFetchTransport: jest.fn(), |
| 20 | + defaultStackParser: jest.fn(), |
| 21 | + withScope: jest.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +const mockScope = { |
| 25 | + setTag: jest.fn(), |
| 26 | +}; |
| 27 | + |
| 28 | +describe('Sentry', () => { |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should initialize Sentry Hub and return wrapper', () => { |
| 35 | + const hub = initSentry(true); |
| 36 | + expect(Sentry.BrowserClient).toHaveBeenCalledTimes(1); |
| 37 | + expect(Sentry.Hub).toHaveBeenCalledTimes(1); |
| 38 | + expect(hub).toBeTruthy(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return null if Sentry is disabled', () => { |
| 42 | + const hub = initSentry(false); |
| 43 | + expect(Sentry.BrowserClient).not.toHaveBeenCalled(); |
| 44 | + expect(Sentry.Hub).not.toHaveBeenCalled(); |
| 45 | + expect(hub).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should get initialized Sentry Hub', () => { |
| 49 | + const hub = initSentry(true); |
| 50 | + const hubValues = String(Object.values(hub)); |
| 51 | + |
| 52 | + const retrievedHub = getSentry(); |
| 53 | + const retrievedValues = String(Object.values(retrievedHub)); |
| 54 | + |
| 55 | + expect(hubValues).toEqual(retrievedValues); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should wrap Sentry Hub correctly', () => { |
| 59 | + const mockHub = { |
| 60 | + addBreadcrumb: jest.fn(), |
| 61 | + captureMessage: jest.fn(), |
| 62 | + captureException: jest.fn(), |
| 63 | + withScope: jest.fn(callback => callback(mockScope)), |
| 64 | + }; |
| 65 | + |
| 66 | + const tag = { key: 'testKey', value: 'testValue' }; |
| 67 | + const breadcrumb = { category: 'test breadcrumb' }; |
| 68 | + |
| 69 | + const sentryHubWrapper = getSentryHubWrapper(mockHub, tag); |
| 70 | + |
| 71 | + sentryHubWrapper.addBreadcrumb(breadcrumb); |
| 72 | + expect(mockHub.addBreadcrumb).toHaveBeenCalledWith(breadcrumb); |
| 73 | + |
| 74 | + sentryHubWrapper.captureMessage('test message'); |
| 75 | + expect(mockHub.captureMessage).toHaveBeenCalledWith('test message'); |
| 76 | + |
| 77 | + sentryHubWrapper.captureException('test exception'); |
| 78 | + expect(mockHub.captureException).toHaveBeenCalledWith('test exception'); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
0 commit comments