|
| 1 | +import { getClient, metrics, setCurrentClient } from '@sentry/core'; |
| 2 | +import { ReactNativeClient } from '../src/js'; |
| 3 | +import { getDefaultTestClientOptions, TestClient } from './mocks/client'; |
| 4 | +import { NATIVE } from './mockWrapper'; |
| 5 | + |
| 6 | +jest.mock('../src/js/wrapper', () => jest.requireActual('./mockWrapper')); |
| 7 | + |
| 8 | +const EXAMPLE_DSN = 'https://[email protected]/148053'; |
| 9 | + |
| 10 | +describe('Metrics', () => { |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + jest.useFakeTimers(); |
| 14 | + (NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => true); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.runOnlyPendingTimers(); |
| 19 | + jest.useRealTimers(); |
| 20 | + getClient()?.close(); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('beforeSendMetric', () => { |
| 24 | + it('is called when enableMetrics is true and a metric is sent', async () => { |
| 25 | + const beforeSendMetric = jest.fn(metric => metric); |
| 26 | + |
| 27 | + const client = new ReactNativeClient({ |
| 28 | + ...getDefaultTestClientOptions({ |
| 29 | + dsn: EXAMPLE_DSN, |
| 30 | + enableMetrics: true, |
| 31 | + beforeSendMetric, |
| 32 | + }), |
| 33 | + }); |
| 34 | + |
| 35 | + setCurrentClient(client); |
| 36 | + client.init(); |
| 37 | + |
| 38 | + // Send a metric |
| 39 | + metrics.count('test_metric', 1); |
| 40 | + |
| 41 | + jest.advanceTimersByTime(10000); |
| 42 | + expect(beforeSendMetric).toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('is not called when enableMetrics is false', async () => { |
| 46 | + const beforeSendMetric = jest.fn(metric => metric); |
| 47 | + |
| 48 | + const client = new ReactNativeClient({ |
| 49 | + ...getDefaultTestClientOptions({ |
| 50 | + dsn: EXAMPLE_DSN, |
| 51 | + enableMetrics: false, |
| 52 | + beforeSendMetric, |
| 53 | + }), |
| 54 | + }); |
| 55 | + |
| 56 | + setCurrentClient(client); |
| 57 | + client.init(); |
| 58 | + |
| 59 | + // Send a metric |
| 60 | + metrics.count('test_metric', 1); |
| 61 | + |
| 62 | + jest.advanceTimersByTime(10000); |
| 63 | + expect(beforeSendMetric).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('is called when enableMetrics is undefined (metrics are enabled by default)', async () => { |
| 67 | + const beforeSendMetric = jest.fn(metric => metric); |
| 68 | + |
| 69 | + const client = new ReactNativeClient({ |
| 70 | + ...getDefaultTestClientOptions({ |
| 71 | + dsn: EXAMPLE_DSN, |
| 72 | + beforeSendMetric, |
| 73 | + }), |
| 74 | + }); |
| 75 | + |
| 76 | + setCurrentClient(client); |
| 77 | + client.init(); |
| 78 | + |
| 79 | + // Send a metric |
| 80 | + metrics.count('test_metric', 1); |
| 81 | + |
| 82 | + jest.advanceTimersByTime(10000); |
| 83 | + expect(beforeSendMetric).toHaveBeenCalled(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('allows beforeSendMetric to modify metrics when enableMetrics is true', async () => { |
| 87 | + const beforeSendMetric = jest.fn(metric => { |
| 88 | + // Modify the metric |
| 89 | + return { ...metric, name: 'modified_metric' }; |
| 90 | + }); |
| 91 | + |
| 92 | + const client = new ReactNativeClient({ |
| 93 | + ...getDefaultTestClientOptions({ |
| 94 | + dsn: EXAMPLE_DSN, |
| 95 | + enableMetrics: true, |
| 96 | + beforeSendMetric, |
| 97 | + }), |
| 98 | + }); |
| 99 | + |
| 100 | + setCurrentClient(client); |
| 101 | + client.init(); |
| 102 | + |
| 103 | + // Send a metric |
| 104 | + metrics.count('test_metric', 1); |
| 105 | + |
| 106 | + jest.advanceTimersByTime(10000); |
| 107 | + expect(beforeSendMetric).toHaveBeenCalled(); |
| 108 | + const modifiedMetric = beforeSendMetric.mock.results[0]?.value; |
| 109 | + expect(modifiedMetric).toBeDefined(); |
| 110 | + expect(modifiedMetric.name).toBe('modified_metric'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('allows beforeSendMetric to drop metrics by returning null', async () => { |
| 114 | + const beforeSendMetric = jest.fn(() => null); |
| 115 | + |
| 116 | + const client = new ReactNativeClient({ |
| 117 | + ...getDefaultTestClientOptions({ |
| 118 | + dsn: EXAMPLE_DSN, |
| 119 | + enableMetrics: true, |
| 120 | + beforeSendMetric, |
| 121 | + }), |
| 122 | + }); |
| 123 | + |
| 124 | + setCurrentClient(client); |
| 125 | + client.init(); |
| 126 | + |
| 127 | + // Send a metric |
| 128 | + metrics.count('test_metric', 1); |
| 129 | + |
| 130 | + // Advance timers |
| 131 | + jest.advanceTimersByTime(10000); |
| 132 | + expect(beforeSendMetric).toHaveBeenCalled(); |
| 133 | + expect(beforeSendMetric.mock.results[0]?.value).toBeNull(); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('metrics API', () => { |
| 138 | + it('metrics.count works when enableMetrics is true', () => { |
| 139 | + const client = new ReactNativeClient({ |
| 140 | + ...getDefaultTestClientOptions({ |
| 141 | + dsn: EXAMPLE_DSN, |
| 142 | + enableMetrics: true, |
| 143 | + }), |
| 144 | + }); |
| 145 | + |
| 146 | + setCurrentClient(client); |
| 147 | + client.init(); |
| 148 | + |
| 149 | + expect(() => { |
| 150 | + metrics.count('test_metric', 1); |
| 151 | + }).not.toThrow(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('metrics can be sent with tags', async () => { |
| 155 | + const beforeSendMetric = jest.fn(metric => metric); |
| 156 | + |
| 157 | + const client = new ReactNativeClient({ |
| 158 | + ...getDefaultTestClientOptions({ |
| 159 | + dsn: EXAMPLE_DSN, |
| 160 | + enableMetrics: true, |
| 161 | + beforeSendMetric, |
| 162 | + }), |
| 163 | + }); |
| 164 | + |
| 165 | + setCurrentClient(client); |
| 166 | + client.init(); |
| 167 | + |
| 168 | + // Send a metric with tags |
| 169 | + metrics.count('test_metric', 1, { |
| 170 | + attributes: { environment: 'test' }, |
| 171 | + }); |
| 172 | + |
| 173 | + jest.advanceTimersByTime(10000); |
| 174 | + expect(beforeSendMetric).toHaveBeenCalled(); |
| 175 | + const sentMetric = beforeSendMetric.mock.calls[0]?.[0]; |
| 176 | + expect(sentMetric).toBeDefined(); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments