|
| 1 | +import type { Event } from '@sentry/types'; |
| 2 | +import { createStackParser, GLOBAL_OBJ, nodeStackLineParser, parseEnvelope } from '@sentry/utils'; |
| 3 | +import { TextDecoder, TextEncoder } from 'util'; |
| 4 | + |
| 5 | +import { createTransport, getCurrentHub, ModuleMetadata } from '../../../src'; |
| 6 | +import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; |
| 7 | + |
| 8 | +const stackParser = createStackParser(nodeStackLineParser()); |
| 9 | + |
| 10 | +const stack = new Error().stack || ''; |
| 11 | + |
| 12 | +describe('ModuleMetadata integration', () => { |
| 13 | + beforeEach(() => { |
| 14 | + TestClient.sendEventCalled = undefined; |
| 15 | + TestClient.instance = undefined; |
| 16 | + |
| 17 | + GLOBAL_OBJ._sentryModuleMetadata = GLOBAL_OBJ._sentryModuleMetadata || {}; |
| 18 | + GLOBAL_OBJ._sentryModuleMetadata[stack] = { team: 'frontend' }; |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('Adds and removes metadata from stack frames', done => { |
| 26 | + const options = getDefaultTestClientOptions({ |
| 27 | + dsn: 'https://username@domain/123', |
| 28 | + enableSend: true, |
| 29 | + stackParser, |
| 30 | + integrations: [new ModuleMetadata()], |
| 31 | + beforeSend: (event, _hint) => { |
| 32 | + // copy the frames since reverse in in-place |
| 33 | + const lastFrame = [...(event.exception?.values?.[0].stacktrace?.frames || [])].reverse()[0]; |
| 34 | + // Ensure module_metadata is populated in beforeSend callback |
| 35 | + expect(lastFrame?.module_metadata).toEqual({ team: 'frontend' }); |
| 36 | + return event; |
| 37 | + }, |
| 38 | + transport: () => |
| 39 | + createTransport({ recordDroppedEvent: () => undefined, textEncoder: new TextEncoder() }, async req => { |
| 40 | + const [, items] = parseEnvelope(req.body, new TextEncoder(), new TextDecoder()); |
| 41 | + |
| 42 | + expect(items[0][1]).toBeDefined(); |
| 43 | + const event = items[0][1] as Event; |
| 44 | + const error = event.exception?.values?.[0]; |
| 45 | + |
| 46 | + // Ensure we're looking at the same error we threw |
| 47 | + expect(error?.value).toEqual('Some error'); |
| 48 | + |
| 49 | + const lastFrame = [...(error?.stacktrace?.frames || [])].reverse()[0]; |
| 50 | + // Ensure the last frame is in fact for this file |
| 51 | + expect(lastFrame?.filename).toEqual(__filename); |
| 52 | + |
| 53 | + // Ensure module_metadata has been stripped from the event |
| 54 | + expect(lastFrame?.module_metadata).toBeUndefined(); |
| 55 | + |
| 56 | + done(); |
| 57 | + return {}; |
| 58 | + }), |
| 59 | + }); |
| 60 | + |
| 61 | + const client = new TestClient(options); |
| 62 | + const hub = getCurrentHub(); |
| 63 | + hub.bindClient(client); |
| 64 | + hub.captureException(new Error('Some error')); |
| 65 | + }); |
| 66 | +}); |
0 commit comments