|
1 |
| -import type { Event, EventHint, ScopeContext } from '@sentry/types'; |
| 1 | +import type { |
| 2 | + Attachment, |
| 3 | + Breadcrumb, |
| 4 | + Client, |
| 5 | + ClientOptions, |
| 6 | + Event, |
| 7 | + EventHint, |
| 8 | + EventProcessor, |
| 9 | + ScopeContext, |
| 10 | +} from '@sentry/types'; |
2 | 11 | import { GLOBAL_OBJ, createStackParser } from '@sentry/utils';
|
| 12 | +import { clearGlobalData } from '../../src'; |
3 | 13 |
|
4 |
| -import { Scope } from '../../src/scope'; |
5 |
| -import { applyDebugIds, applyDebugMeta, parseEventHintOrCaptureContext } from '../../src/utils/prepareEvent'; |
| 14 | +import { Scope, getGlobalScope } from '../../src/scope'; |
| 15 | +import { |
| 16 | + applyDebugIds, |
| 17 | + applyDebugMeta, |
| 18 | + parseEventHintOrCaptureContext, |
| 19 | + prepareEvent, |
| 20 | +} from '../../src/utils/prepareEvent'; |
6 | 21 |
|
7 | 22 | describe('applyDebugIds', () => {
|
8 | 23 | afterEach(() => {
|
@@ -173,3 +188,170 @@ describe('parseEventHintOrCaptureContext', () => {
|
173 | 188 | });
|
174 | 189 | });
|
175 | 190 | });
|
| 191 | + |
| 192 | +describe('prepareEvent', () => { |
| 193 | + beforeEach(() => { |
| 194 | + clearGlobalData(); |
| 195 | + }); |
| 196 | + |
| 197 | + it('works without any scope data', async () => { |
| 198 | + const eventProcessor = jest.fn((a: unknown) => a) as EventProcessor; |
| 199 | + |
| 200 | + const scope = new Scope(); |
| 201 | + |
| 202 | + const event = { message: 'foo' }; |
| 203 | + |
| 204 | + const options = {} as ClientOptions; |
| 205 | + const client = { |
| 206 | + getEventProcessors() { |
| 207 | + return [eventProcessor]; |
| 208 | + }, |
| 209 | + } as Client; |
| 210 | + const processedEvent = await prepareEvent( |
| 211 | + options, |
| 212 | + event, |
| 213 | + { |
| 214 | + integrations: [], |
| 215 | + }, |
| 216 | + scope, |
| 217 | + client, |
| 218 | + ); |
| 219 | + |
| 220 | + expect(eventProcessor).toHaveBeenCalledWith(processedEvent, { |
| 221 | + integrations: [], |
| 222 | + // no attachments are added to hint |
| 223 | + }); |
| 224 | + |
| 225 | + expect(processedEvent).toEqual({ |
| 226 | + timestamp: expect.any(Number), |
| 227 | + event_id: expect.any(String), |
| 228 | + environment: 'production', |
| 229 | + message: 'foo', |
| 230 | + sdkProcessingMetadata: { |
| 231 | + propagationContext: { |
| 232 | + spanId: expect.any(String), |
| 233 | + traceId: expect.any(String), |
| 234 | + }, |
| 235 | + }, |
| 236 | + }); |
| 237 | + }); |
| 238 | + |
| 239 | + it('merges scope data', async () => { |
| 240 | + const breadcrumb1 = { message: '1', timestamp: 111 } as Breadcrumb; |
| 241 | + const breadcrumb2 = { message: '2', timestamp: 222 } as Breadcrumb; |
| 242 | + const breadcrumb3 = { message: '3', timestamp: 123 } as Breadcrumb; |
| 243 | + |
| 244 | + const eventProcessor1 = jest.fn((a: unknown) => a) as EventProcessor; |
| 245 | + const eventProcessor2 = jest.fn((b: unknown) => b) as EventProcessor; |
| 246 | + |
| 247 | + const attachment1 = { filename: '1' } as Attachment; |
| 248 | + const attachment2 = { filename: '2' } as Attachment; |
| 249 | + |
| 250 | + const scope = new Scope(); |
| 251 | + scope.update({ |
| 252 | + user: { id: '1', email: '[email protected]' }, |
| 253 | + tags: { tag1: 'aa', tag2: 'aa' }, |
| 254 | + extra: { extra1: 'aa', extra2: 'aa' }, |
| 255 | + contexts: { os: { name: 'os1' }, culture: { display_name: 'name1' } }, |
| 256 | + propagationContext: { spanId: '1', traceId: '1' }, |
| 257 | + fingerprint: ['aa'], |
| 258 | + }); |
| 259 | + scope.addBreadcrumb(breadcrumb1); |
| 260 | + scope.addEventProcessor(eventProcessor1); |
| 261 | + scope.addAttachment(attachment1); |
| 262 | + |
| 263 | + const globalScope = getGlobalScope(); |
| 264 | + |
| 265 | + globalScope.addBreadcrumb(breadcrumb2); |
| 266 | + globalScope.addEventProcessor(eventProcessor2); |
| 267 | + globalScope.setSDKProcessingMetadata({ aa: 'aa' }); |
| 268 | + globalScope.addAttachment(attachment2); |
| 269 | + |
| 270 | + const event = { message: 'foo', breadcrumbs: [breadcrumb3], fingerprint: ['dd'] }; |
| 271 | + |
| 272 | + const options = {} as ClientOptions; |
| 273 | + const processedEvent = await prepareEvent( |
| 274 | + options, |
| 275 | + event, |
| 276 | + { |
| 277 | + integrations: [], |
| 278 | + }, |
| 279 | + scope, |
| 280 | + ); |
| 281 | + |
| 282 | + expect(eventProcessor1).toHaveBeenCalledTimes(1); |
| 283 | + expect(eventProcessor2).toHaveBeenCalledTimes(1); |
| 284 | + |
| 285 | + // Test that attachments are correctly merged |
| 286 | + expect(eventProcessor1).toHaveBeenCalledWith(processedEvent, { |
| 287 | + integrations: [], |
| 288 | + attachments: [attachment2, attachment1], |
| 289 | + }); |
| 290 | + |
| 291 | + expect(processedEvent).toEqual({ |
| 292 | + timestamp: expect.any(Number), |
| 293 | + event_id: expect.any(String), |
| 294 | + environment: 'production', |
| 295 | + message: 'foo', |
| 296 | + user: { id: '1', email: '[email protected]' }, |
| 297 | + tags: { tag1: 'aa', tag2: 'aa' }, |
| 298 | + extra: { extra1: 'aa', extra2: 'aa' }, |
| 299 | + contexts: { os: { name: 'os1' }, culture: { display_name: 'name1' } }, |
| 300 | + fingerprint: ['dd', 'aa'], |
| 301 | + breadcrumbs: [breadcrumb3, breadcrumb2, breadcrumb1], |
| 302 | + sdkProcessingMetadata: { |
| 303 | + aa: 'aa', |
| 304 | + propagationContext: { |
| 305 | + spanId: '1', |
| 306 | + traceId: '1', |
| 307 | + }, |
| 308 | + }, |
| 309 | + }); |
| 310 | + }); |
| 311 | + |
| 312 | + it('works without a scope', async () => { |
| 313 | + const breadcrumb1 = { message: '1', timestamp: 111 } as Breadcrumb; |
| 314 | + const breadcrumb2 = { message: '2', timestamp: 222 } as Breadcrumb; |
| 315 | + |
| 316 | + const eventProcessor1 = jest.fn((a: unknown) => a) as EventProcessor; |
| 317 | + |
| 318 | + const attachment1 = { filename: '1' } as Attachment; |
| 319 | + const attachment2 = { filename: '2' } as Attachment; |
| 320 | + |
| 321 | + const globalScope = getGlobalScope(); |
| 322 | + |
| 323 | + globalScope.addBreadcrumb(breadcrumb1); |
| 324 | + globalScope.addEventProcessor(eventProcessor1); |
| 325 | + globalScope.setSDKProcessingMetadata({ aa: 'aa' }); |
| 326 | + globalScope.addAttachment(attachment1); |
| 327 | + |
| 328 | + const event = { message: 'foo', breadcrumbs: [breadcrumb2], fingerprint: ['dd'] }; |
| 329 | + |
| 330 | + const options = {} as ClientOptions; |
| 331 | + const processedEvent = await prepareEvent(options, event, { |
| 332 | + integrations: [], |
| 333 | + attachments: [attachment2], |
| 334 | + }); |
| 335 | + |
| 336 | + expect(eventProcessor1).toHaveBeenCalledTimes(1); |
| 337 | + |
| 338 | + // Test that attachments are correctly merged |
| 339 | + expect(eventProcessor1).toHaveBeenCalledWith(processedEvent, { |
| 340 | + integrations: [], |
| 341 | + attachments: [attachment2, attachment1], |
| 342 | + }); |
| 343 | + |
| 344 | + expect(processedEvent).toEqual({ |
| 345 | + timestamp: expect.any(Number), |
| 346 | + event_id: expect.any(String), |
| 347 | + environment: 'production', |
| 348 | + message: 'foo', |
| 349 | + fingerprint: ['dd'], |
| 350 | + breadcrumbs: [breadcrumb2, breadcrumb1], |
| 351 | + sdkProcessingMetadata: { |
| 352 | + aa: 'aa', |
| 353 | + propagationContext: globalScope.getPropagationContext(), |
| 354 | + }, |
| 355 | + }); |
| 356 | + }); |
| 357 | +}); |
0 commit comments