|
| 1 | +import * as Sentry from '@sentry/react-native'; |
| 2 | +import { defaultStackParser, makeFetchTransport } from '@sentry/react'; |
| 3 | +import * as SentryReactNativeModule from '@sentry/react-native'; |
| 4 | +import { reportError, setSentryExtraAttribute } from './sentryIntegration'; |
| 5 | + |
| 6 | +jest.mock('@sentry/react-native', () => { |
| 7 | + const scope = { |
| 8 | + setClient: jest.fn(), |
| 9 | + addBreadcrumb: jest.fn(), |
| 10 | + captureException: jest.fn(), |
| 11 | + setExtra: jest.fn(), |
| 12 | + }; |
| 13 | + return { |
| 14 | + ReactNativeClient: jest.fn().mockImplementation(() => ({ |
| 15 | + init: jest.fn(), |
| 16 | + captureException: jest.fn(), |
| 17 | + })), |
| 18 | + Scope: jest.fn().mockImplementation(() => scope), |
| 19 | + |
| 20 | + // Only for internal testing |
| 21 | + __test_getScope: () => scope, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +describe('sentryScope', () => { |
| 26 | + let addBreadcrumbMock: jest.Mock; |
| 27 | + let captureExceptionMock: jest.Mock; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + addBreadcrumbMock = jest.fn(); |
| 31 | + captureExceptionMock = jest.fn(); |
| 32 | + jest.spyOn(SentryReactNativeModule, 'Scope').mockReturnValue({ |
| 33 | + setClient: jest.fn(), |
| 34 | + addBreadcrumb: addBreadcrumbMock, |
| 35 | + captureException: captureExceptionMock, |
| 36 | + } as any); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + jest.restoreAllMocks(); |
| 41 | + jest.resetAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should initialise sentry scope with correct options', () => { |
| 45 | + expect(Sentry.ReactNativeClient).toHaveBeenCalledWith({ |
| 46 | + attachStacktrace: true, |
| 47 | + autoInitializeNativeSdk: false, |
| 48 | + dsn: 'https://[email protected]/8', |
| 49 | + enableAppStartTracking: false, |
| 50 | + enableAutoPerformanceTracing: false, |
| 51 | + enableCaptureFailedRequests: false, |
| 52 | + enableNative: false, |
| 53 | + enableNativeCrashHandling: false, |
| 54 | + enableNativeFramesTracking: false, |
| 55 | + enableNativeNagger: false, |
| 56 | + enableNdk: false, |
| 57 | + enableStallTracking: true, |
| 58 | + enableUserInteractionTracing: false, |
| 59 | + enableWatchdogTerminationTracking: false, |
| 60 | + maxQueueSize: 30, |
| 61 | + parentSpanIsAlwaysRootSpan: true, |
| 62 | + patchGlobalPromise: true, |
| 63 | + sendClientReports: true, |
| 64 | + integrations: [], |
| 65 | + stackParser: defaultStackParser, |
| 66 | + transport: makeFetchTransport, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('reportError', () => { |
| 71 | + it('should add breadcrumb with message if provided', () => { |
| 72 | + const err = new Error('test'); |
| 73 | + const msg = 'test message'; |
| 74 | + // @ts-ignore |
| 75 | + const { addBreadcrumb, captureException } = Sentry.__test_getScope(); |
| 76 | + reportError(err, { msg }); |
| 77 | + |
| 78 | + expect(addBreadcrumb).toHaveBeenCalledWith({ |
| 79 | + category: 'Error', |
| 80 | + message: msg, |
| 81 | + level: 'log', |
| 82 | + }); |
| 83 | + expect(captureException).toHaveBeenCalledWith(err); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should not add breadcrumb if message is not provided', () => { |
| 87 | + const err = new Error('test'); |
| 88 | + // @ts-ignore |
| 89 | + const { addBreadcrumb, captureException } = Sentry.__test_getScope(); |
| 90 | + reportError(err); |
| 91 | + |
| 92 | + expect(addBreadcrumb).not.toHaveBeenCalled(); |
| 93 | + expect(captureException).toHaveBeenCalledWith(err); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('setSentryExtraAttribute', () => { |
| 98 | + it('should set extra attribute', () => { |
| 99 | + const key = 'testKey'; |
| 100 | + const value = 'testValue'; |
| 101 | + // @ts-ignore |
| 102 | + const { setExtra } = Sentry.__test_getScope(); |
| 103 | + setSentryExtraAttribute(key, value); |
| 104 | + |
| 105 | + expect(setExtra).toHaveBeenCalledWith(key, value); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments