|
| 1 | +/// <reference path="../../src/promise.d.ts" /> |
| 2 | + |
| 3 | +import tracking from 'promise/setimmediate/rejection-tracking'; |
| 4 | +import { captureUnhandledRejections } from '../../src/utils/UnhandledRejectionTracking'; |
| 5 | +import { mockHermesInternal } from '../mocks/mockHermesInternal'; |
| 6 | +import { mockDevMode } from '../mocks/mockDevMode'; |
| 7 | +import { mocked } from 'jest-mock'; |
| 8 | +import { NativeCrashReporting } from '../../src/native/NativeCrashReporting'; |
| 9 | + |
| 10 | +it('tracks Promise rejections when using Hermes', () => { |
| 11 | + const enablePromiseRejectionTracker = jest.fn(); |
| 12 | + |
| 13 | + const mHermes = mockHermesInternal({ |
| 14 | + hasPromise: () => true, |
| 15 | + enablePromiseRejectionTracker, |
| 16 | + }); |
| 17 | + |
| 18 | + captureUnhandledRejections(); |
| 19 | + |
| 20 | + expect(enablePromiseRejectionTracker).toBeCalledTimes(1); |
| 21 | + expect(enablePromiseRejectionTracker).toBeCalledWith({ |
| 22 | + allRejections: true, |
| 23 | + onUnhandled: expect.any(Function), |
| 24 | + }); |
| 25 | + |
| 26 | + mHermes.mockRestore(); |
| 27 | +}); |
| 28 | + |
| 29 | +it('tracks Promise rejections when using `promise` polyfill', () => { |
| 30 | + captureUnhandledRejections(); |
| 31 | + |
| 32 | + expect(tracking.enable).toBeCalledTimes(1); |
| 33 | + expect(tracking.enable).toBeCalledWith({ |
| 34 | + allRejections: true, |
| 35 | + onUnhandled: expect.any(Function), |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +it('reports unhandled Promise rejections in release mode', () => { |
| 40 | + const mockDev = mockDevMode(false); |
| 41 | + const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 42 | + |
| 43 | + const rejection = new Error('something went wrong'); |
| 44 | + const id = 123; |
| 45 | + |
| 46 | + // Simulate an immediate unhandled promise rejection |
| 47 | + mocked(tracking.enable).mockImplementationOnce((options) => { |
| 48 | + options?.onUnhandled?.(id, rejection); |
| 49 | + }); |
| 50 | + |
| 51 | + captureUnhandledRejections(); |
| 52 | + |
| 53 | + expect(NativeCrashReporting.sendHandledJSCrash).toBeCalledTimes(1); |
| 54 | + |
| 55 | + mockDev.mockRestore(); |
| 56 | + consoleWarnSpy.mockRestore(); |
| 57 | +}); |
| 58 | + |
| 59 | +it('does not report unhandled Promise rejections in dev mode', () => { |
| 60 | + const mockDev = mockDevMode(true); |
| 61 | + const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 62 | + |
| 63 | + const id = 123; |
| 64 | + const rejection = new TypeError("Couldn't fetch data"); |
| 65 | + |
| 66 | + // Simulate an immediate unhandled promise rejection |
| 67 | + mocked(tracking.enable).mockImplementationOnce((options) => { |
| 68 | + options?.onUnhandled?.(id, rejection); |
| 69 | + }); |
| 70 | + |
| 71 | + captureUnhandledRejections(); |
| 72 | + |
| 73 | + expect(NativeCrashReporting.sendHandledJSCrash).not.toBeCalled(); |
| 74 | + |
| 75 | + mockDev.mockRestore(); |
| 76 | + consoleWarnSpy.mockRestore(); |
| 77 | +}); |
| 78 | + |
| 79 | +it('does not report non-error unhandled Promise rejections', () => { |
| 80 | + const mockDev = mockDevMode(true); |
| 81 | + const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 82 | + |
| 83 | + const id = 123; |
| 84 | + const rejection = 'something went wrong'; |
| 85 | + |
| 86 | + // Simulate an immediate unhandled promise rejection |
| 87 | + mocked(tracking.enable).mockImplementationOnce((options) => { |
| 88 | + options?.onUnhandled?.(id, rejection); |
| 89 | + }); |
| 90 | + |
| 91 | + captureUnhandledRejections(); |
| 92 | + |
| 93 | + expect(NativeCrashReporting.sendHandledJSCrash).not.toBeCalled(); |
| 94 | + |
| 95 | + mockDev.mockRestore(); |
| 96 | + consoleWarnSpy.mockRestore(); |
| 97 | +}); |
0 commit comments