|
| 1 | +import type { BacktraceReport } from '@backtrace/sdk-core'; |
| 2 | +import type { BacktraceClient } from '../src/BacktraceClient'; |
| 3 | + |
| 4 | +jest.mock('react-native', () => ({ |
| 5 | + NativeModules: {}, |
| 6 | + Platform: { |
| 7 | + OS: 'android', |
| 8 | + select: (options: Record<string, unknown>) => |
| 9 | + options.android !== undefined ? options.android : options.default, |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('promise/setimmediate/rejection-tracking', () => ({ |
| 14 | + enable: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('../src/crashReporter/CrashReporter', () => ({ |
| 18 | + CrashReporter: { markFatalError: jest.fn() }, |
| 19 | +})); |
| 20 | + |
| 21 | +const mockIsNativeBridgeEnabled = jest.fn().mockReturnValue(true); |
| 22 | +jest.mock('../src/common/DebuggerHelper', () => ({ |
| 23 | + DebuggerHelper: { isNativeBridgeEnabled: () => mockIsNativeBridgeEnabled() }, |
| 24 | +})); |
| 25 | + |
| 26 | +import { NativeModules } from 'react-native'; |
| 27 | + |
| 28 | +const nativeHandlerMock = { |
| 29 | + start: jest.fn(), |
| 30 | + stop: jest.fn(), |
| 31 | + reportProcessed: jest.fn(), |
| 32 | +}; |
| 33 | + |
| 34 | +NativeModules.BacktraceAndroidBackgroundUnhandledExceptionHandler = nativeHandlerMock; |
| 35 | + |
| 36 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 37 | +const { AndroidUnhandledExceptionHandler } = require('../src/handlers/android/AndroidUnhandledExceptionHandler'); |
| 38 | + |
| 39 | +type NativeExceptionCallback = (classifier: string, message: string, stackTrace: string) => Promise<void>; |
| 40 | + |
| 41 | +describe('AndroidUnhandledExceptionHandler', () => { |
| 42 | + let originalErrorUtils: unknown; |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + jest.clearAllMocks(); |
| 46 | + mockIsNativeBridgeEnabled.mockReturnValue(true); |
| 47 | + |
| 48 | + originalErrorUtils = (global as unknown as { ErrorUtils?: unknown }).ErrorUtils; |
| 49 | + (global as unknown as { ErrorUtils: unknown }).ErrorUtils = { |
| 50 | + getGlobalHandler: () => jest.fn(), |
| 51 | + setGlobalHandler: jest.fn(), |
| 52 | + }; |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + (global as unknown as { ErrorUtils: unknown }).ErrorUtils = originalErrorUtils; |
| 57 | + }); |
| 58 | + |
| 59 | + function captureNativeCallback(client: BacktraceClient): NativeExceptionCallback { |
| 60 | + new AndroidUnhandledExceptionHandler().captureManagedErrors(client); |
| 61 | + return nativeHandlerMock.start.mock.calls[0][0]; |
| 62 | + } |
| 63 | + |
| 64 | + it('Should signal reportProcessed only after the report send resolves', async () => { |
| 65 | + let resolveSend!: () => void; |
| 66 | + const sendMock = jest.fn().mockReturnValue( |
| 67 | + new Promise<void>((resolve) => { |
| 68 | + resolveSend = resolve; |
| 69 | + }), |
| 70 | + ); |
| 71 | + const callback = captureNativeCallback({ send: sendMock } as unknown as BacktraceClient); |
| 72 | + |
| 73 | + const callbackPromise = callback('java.lang.RuntimeException', 'boom', 'a.b(C.java:1)'); |
| 74 | + |
| 75 | + expect(sendMock).toHaveBeenCalledTimes(1); |
| 76 | + expect(nativeHandlerMock.reportProcessed).not.toHaveBeenCalled(); |
| 77 | + |
| 78 | + resolveSend(); |
| 79 | + await callbackPromise; |
| 80 | + |
| 81 | + expect(nativeHandlerMock.reportProcessed).toHaveBeenCalledTimes(1); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Should signal reportProcessed when the send fails', async () => { |
| 85 | + const sendMock = jest.fn().mockRejectedValue(new Error('offline')); |
| 86 | + const callback = captureNativeCallback({ send: sendMock } as unknown as BacktraceClient); |
| 87 | + |
| 88 | + await callback('java.lang.RuntimeException', 'boom', 'a.b(C.java:1)'); |
| 89 | + |
| 90 | + expect(nativeHandlerMock.reportProcessed).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it("Should send the exception as a report tagged with error.type 'Unhandled exception'", async () => { |
| 94 | + const sendMock = jest.fn().mockResolvedValue(undefined); |
| 95 | + const callback = captureNativeCallback({ send: sendMock } as unknown as BacktraceClient); |
| 96 | + |
| 97 | + await callback('java.lang.IllegalStateException', 'boom', 'a.b(C.java:1)'); |
| 98 | + |
| 99 | + const report = sendMock.mock.calls[0][0] as BacktraceReport; |
| 100 | + expect(report.attributes['error.type']).toBe('Unhandled exception'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Should not start the native handler when the native bridge is unavailable', () => { |
| 104 | + mockIsNativeBridgeEnabled.mockReturnValue(false); |
| 105 | + |
| 106 | + new AndroidUnhandledExceptionHandler().captureManagedErrors({ send: jest.fn() } as unknown as BacktraceClient); |
| 107 | + |
| 108 | + expect(nativeHandlerMock.start).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('Should stop the native handler on dispose', () => { |
| 112 | + const handler = new AndroidUnhandledExceptionHandler(); |
| 113 | + handler.captureManagedErrors({ send: jest.fn() } as unknown as BacktraceClient); |
| 114 | + |
| 115 | + handler.dispose(); |
| 116 | + |
| 117 | + expect(nativeHandlerMock.stop).toHaveBeenCalledTimes(1); |
| 118 | + }); |
| 119 | +}); |
0 commit comments