|
1 | | -import { StackFrame } from '@sentry/types'; |
| 1 | +import { Event, Mechanism, StackFrame } from '@sentry/types'; |
2 | 2 |
|
3 | | -import { addContextToFrame, getEventDescription, parseRetryAfterHeader, stripUrlQueryAndFragment } from '../src/misc'; |
| 3 | +import { |
| 4 | + addContextToFrame, |
| 5 | + addExceptionMechanism, |
| 6 | + getEventDescription, |
| 7 | + parseRetryAfterHeader, |
| 8 | + stripUrlQueryAndFragment, |
| 9 | +} from '../src/misc'; |
4 | 10 |
|
5 | 11 | describe('getEventDescription()', () => { |
6 | 12 | test('message event', () => { |
@@ -220,3 +226,61 @@ describe('stripQueryStringAndFragment', () => { |
220 | 226 | expect(stripUrlQueryAndFragment(urlWithQueryStringAndFragment)).toBe(urlString); |
221 | 227 | }); |
222 | 228 | }); |
| 229 | + |
| 230 | +describe('addExceptionMechanism', () => { |
| 231 | + const defaultMechanism = { type: 'generic', handled: true }; |
| 232 | + |
| 233 | + type EventWithException = Event & { |
| 234 | + exception: { |
| 235 | + values: [{ type?: string; value?: string; mechanism?: Mechanism }]; |
| 236 | + }; |
| 237 | + }; |
| 238 | + |
| 239 | + const baseEvent: EventWithException = { |
| 240 | + exception: { values: [{ type: 'Error', value: 'Oh, no! Charlie ate the flip-flops! :-(' }] }, |
| 241 | + }; |
| 242 | + let event: EventWithException; |
| 243 | + |
| 244 | + beforeEach(() => { |
| 245 | + event = { ...baseEvent }; |
| 246 | + }); |
| 247 | + |
| 248 | + it('uses default values', () => { |
| 249 | + addExceptionMechanism(event); |
| 250 | + |
| 251 | + expect(event.exception.values[0].mechanism).toEqual(defaultMechanism); |
| 252 | + }); |
| 253 | + |
| 254 | + it('prefers current values to defaults', () => { |
| 255 | + const nonDefaultMechanism = { type: 'instrument', handled: false }; |
| 256 | + event.exception.values[0].mechanism = nonDefaultMechanism; |
| 257 | + |
| 258 | + addExceptionMechanism(event); |
| 259 | + |
| 260 | + expect(event.exception.values[0].mechanism).toEqual(nonDefaultMechanism); |
| 261 | + }); |
| 262 | + |
| 263 | + it('prefers incoming values to current values', () => { |
| 264 | + const currentMechanism = { type: 'instrument', handled: false }; |
| 265 | + const newMechanism = { handled: true }; |
| 266 | + event.exception.values[0].mechanism = currentMechanism; |
| 267 | + |
| 268 | + addExceptionMechanism(event, newMechanism); |
| 269 | + |
| 270 | + expect(event.exception.values[0].mechanism.handled).toEqual(true); |
| 271 | + }); |
| 272 | + |
| 273 | + it('merges data values', () => { |
| 274 | + const currentMechanism = { type: 'instrument', handled: false, data: { function: 'addEventListener' } }; |
| 275 | + const newMechanism = { handled: true, data: { handler: 'organizeShoes', target: 'closet' } }; |
| 276 | + event.exception.values[0].mechanism = currentMechanism; |
| 277 | + |
| 278 | + addExceptionMechanism(event, newMechanism); |
| 279 | + |
| 280 | + expect(event.exception.values[0].mechanism.data).toEqual({ |
| 281 | + function: 'addEventListener', |
| 282 | + handler: 'organizeShoes', |
| 283 | + target: 'closet', |
| 284 | + }); |
| 285 | + }); |
| 286 | +}); |
0 commit comments