|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | + |
| 6 | +describe('monitoring', () => { |
| 7 | + let mockInitMonitoring: jest.Mock; |
| 8 | + let mockIgnoreErrors: jest.Mock; |
| 9 | + let mockLog: jest.Mock; |
| 10 | + let mockLogger: Record<string, jest.Mock>; |
| 11 | + let mockHapiIntegration: jest.Mock; |
| 12 | + let mockLinkedErrorsIntegration: jest.Mock; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + jest.resetModules(); |
| 16 | + |
| 17 | + mockInitMonitoring = jest.fn(); |
| 18 | + mockIgnoreErrors = jest.fn(); |
| 19 | + mockLogger = { |
| 20 | + info: jest.fn(), |
| 21 | + error: jest.fn(), |
| 22 | + warn: jest.fn(), |
| 23 | + debug: jest.fn(), |
| 24 | + trace: jest.fn(), |
| 25 | + }; |
| 26 | + mockLog = jest.fn().mockReturnValue(mockLogger); |
| 27 | + mockHapiIntegration = jest.fn().mockReturnValue({ name: 'Hapi' }); |
| 28 | + mockLinkedErrorsIntegration = jest |
| 29 | + .fn() |
| 30 | + .mockReturnValue({ name: 'LinkedErrors' }); |
| 31 | + |
| 32 | + jest.doMock('fxa-shared/monitoring', () => ({ |
| 33 | + initMonitoring: mockInitMonitoring, |
| 34 | + })); |
| 35 | + jest.doMock('../config', () => ({ |
| 36 | + config: { |
| 37 | + getProperties: jest.fn().mockReturnValue({ |
| 38 | + log: { level: 'debug' }, |
| 39 | + sentry: { dsn: 'https://[email protected]/123' }, |
| 40 | + }), |
| 41 | + }, |
| 42 | + })); |
| 43 | + jest.doMock('./log', () => mockLog); |
| 44 | + jest.doMock('../package.json', () => ({ version: '1.234.0' }), { |
| 45 | + virtual: true, |
| 46 | + }); |
| 47 | + jest.doMock('@fxa/accounts/errors', () => ({ |
| 48 | + ignoreErrors: mockIgnoreErrors, |
| 49 | + })); |
| 50 | + jest.doMock('@sentry/node', () => ({ |
| 51 | + hapiIntegration: mockHapiIntegration, |
| 52 | + linkedErrorsIntegration: mockLinkedErrorsIntegration, |
| 53 | + })); |
| 54 | + |
| 55 | + // Requiring the module triggers the top-level initMonitoring() call. |
| 56 | + require('./monitoring'); |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(() => { |
| 60 | + jest.restoreAllMocks(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('calls initMonitoring on module load', () => { |
| 64 | + expect(mockInitMonitoring).toHaveBeenCalledTimes(1); |
| 65 | + |
| 66 | + const callArg = mockInitMonitoring.mock.calls[0][0]; |
| 67 | + |
| 68 | + // Logger is the return value of require('./log')(level, name) |
| 69 | + expect(callArg.logger).toBe(mockLogger); |
| 70 | + expect(mockLog).toHaveBeenCalledWith('debug', 'configure-sentry'); |
| 71 | + |
| 72 | + // Config includes spread properties, release, eventFilters, integrations |
| 73 | + expect(callArg.config).toEqual( |
| 74 | + expect.objectContaining({ |
| 75 | + log: { level: 'debug' }, |
| 76 | + sentry: { dsn: 'https://[email protected]/123' }, |
| 77 | + release: '1.234.0', |
| 78 | + }) |
| 79 | + ); |
| 80 | + expect(callArg.config.eventFilters).toHaveLength(1); |
| 81 | + expect(typeof callArg.config.eventFilters[0]).toBe('function'); |
| 82 | + expect(callArg.config.integrations).toHaveLength(2); |
| 83 | + }); |
| 84 | + |
| 85 | + it('passes Sentry integrations with correct configuration', () => { |
| 86 | + expect(mockHapiIntegration).toHaveBeenCalledTimes(1); |
| 87 | + expect(mockLinkedErrorsIntegration).toHaveBeenCalledWith({ |
| 88 | + key: 'jse_cause', |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('filterSentryEvent', () => { |
| 93 | + let filterSentryEvent: (event: any, hint?: any) => any; |
| 94 | + |
| 95 | + beforeEach(() => { |
| 96 | + // Extract the filterSentryEvent function from the initMonitoring call |
| 97 | + const callArg = mockInitMonitoring.mock.calls[0][0]; |
| 98 | + filterSentryEvent = callArg.config.eventFilters[0]; |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns null when ignoreErrors returns true', () => { |
| 102 | + mockIgnoreErrors.mockReturnValue(true); |
| 103 | + |
| 104 | + const event = { event_id: 'abc123' }; |
| 105 | + const hint = { originalException: new Error('ignored error') }; |
| 106 | + |
| 107 | + const result = filterSentryEvent(event, hint); |
| 108 | + |
| 109 | + expect(result).toBeNull(); |
| 110 | + expect(mockIgnoreErrors).toHaveBeenCalledWith(hint.originalException); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns the event when ignoreErrors returns false', () => { |
| 114 | + mockIgnoreErrors.mockReturnValue(false); |
| 115 | + |
| 116 | + const event = { event_id: 'def456' }; |
| 117 | + const hint = { originalException: new Error('real error') }; |
| 118 | + |
| 119 | + const result = filterSentryEvent(event, hint); |
| 120 | + |
| 121 | + expect(result).toBe(event); |
| 122 | + expect(mockIgnoreErrors).toHaveBeenCalledWith(hint.originalException); |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns the event when hint.originalException is null', () => { |
| 126 | + const event = { event_id: 'ghi789' }; |
| 127 | + const hint = { originalException: null }; |
| 128 | + |
| 129 | + const result = filterSentryEvent(event, hint); |
| 130 | + |
| 131 | + expect(result).toBe(event); |
| 132 | + expect(mockIgnoreErrors).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('returns the event when hint.originalException is undefined', () => { |
| 136 | + const event = { event_id: 'jkl012' }; |
| 137 | + const hint = { originalException: undefined }; |
| 138 | + |
| 139 | + const result = filterSentryEvent(event, hint); |
| 140 | + |
| 141 | + expect(result).toBe(event); |
| 142 | + expect(mockIgnoreErrors).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns the event when hint is undefined', () => { |
| 146 | + const event = { event_id: 'mno345' }; |
| 147 | + |
| 148 | + const result = filterSentryEvent(event, undefined); |
| 149 | + |
| 150 | + expect(result).toBe(event); |
| 151 | + expect(mockIgnoreErrors).not.toHaveBeenCalled(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('returns the event when hint is null', () => { |
| 155 | + const event = { event_id: 'pqr678' }; |
| 156 | + |
| 157 | + const result = filterSentryEvent(event, null); |
| 158 | + |
| 159 | + expect(result).toBe(event); |
| 160 | + expect(mockIgnoreErrors).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments