|
1 | 1 | import type { Integration, Options } from '@sentry/types';
|
2 | 2 |
|
3 |
| -import { getIntegrationsToSetup } from '../../src/integration'; |
| 3 | +import { getIntegrationsToSetup, installedIntegrations, setupIntegration } from '../../src/integration'; |
| 4 | +import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; |
| 5 | + |
| 6 | +function getTestClient(): TestClient { |
| 7 | + return new TestClient( |
| 8 | + getDefaultTestClientOptions({ |
| 9 | + dsn: 'https://username@domain/123', |
| 10 | + }), |
| 11 | + ); |
| 12 | +} |
4 | 13 |
|
5 | 14 | /** JSDoc */
|
6 | 15 | class MockIntegration implements Integration {
|
@@ -317,3 +326,236 @@ describe('getIntegrationsToSetup', () => {
|
317 | 326 | expect(integrations.map(i => i.name)).toEqual(['foo', 'Debug']);
|
318 | 327 | });
|
319 | 328 | });
|
| 329 | + |
| 330 | +describe('setupIntegration', () => { |
| 331 | + beforeEach(function () { |
| 332 | + // Reset the (global!) list of installed integrations |
| 333 | + installedIntegrations.splice(0, installedIntegrations.length); |
| 334 | + }); |
| 335 | + |
| 336 | + it('works with a minimal integration', () => { |
| 337 | + class CustomIntegration implements Integration { |
| 338 | + name = 'test'; |
| 339 | + setupOnce = jest.fn(); |
| 340 | + } |
| 341 | + |
| 342 | + const client = getTestClient(); |
| 343 | + const integrationIndex = {}; |
| 344 | + const integration = new CustomIntegration(); |
| 345 | + |
| 346 | + setupIntegration(client, integration, integrationIndex); |
| 347 | + |
| 348 | + expect(integrationIndex).toEqual({ test: integration }); |
| 349 | + expect(integration.setupOnce).toHaveBeenCalledTimes(1); |
| 350 | + }); |
| 351 | + |
| 352 | + it('only calls setupOnce a single time', () => { |
| 353 | + class CustomIntegration implements Integration { |
| 354 | + name = 'test'; |
| 355 | + setupOnce = jest.fn(); |
| 356 | + } |
| 357 | + |
| 358 | + const client1 = getTestClient(); |
| 359 | + const client2 = getTestClient(); |
| 360 | + |
| 361 | + const integrationIndex = {}; |
| 362 | + const integration1 = new CustomIntegration(); |
| 363 | + const integration2 = new CustomIntegration(); |
| 364 | + const integration3 = new CustomIntegration(); |
| 365 | + const integration4 = new CustomIntegration(); |
| 366 | + |
| 367 | + setupIntegration(client1, integration1, integrationIndex); |
| 368 | + setupIntegration(client1, integration2, integrationIndex); |
| 369 | + setupIntegration(client2, integration3, integrationIndex); |
| 370 | + setupIntegration(client2, integration4, integrationIndex); |
| 371 | + |
| 372 | + expect(integrationIndex).toEqual({ test: integration4 }); |
| 373 | + expect(integration1.setupOnce).toHaveBeenCalledTimes(1); |
| 374 | + expect(integration2.setupOnce).not.toHaveBeenCalled(); |
| 375 | + expect(integration3.setupOnce).not.toHaveBeenCalled(); |
| 376 | + expect(integration4.setupOnce).not.toHaveBeenCalled(); |
| 377 | + }); |
| 378 | + |
| 379 | + it('binds preprocessEvent for each client', () => { |
| 380 | + class CustomIntegration implements Integration { |
| 381 | + name = 'test'; |
| 382 | + setupOnce = jest.fn(); |
| 383 | + preprocessEvent = jest.fn(); |
| 384 | + } |
| 385 | + |
| 386 | + const client1 = getTestClient(); |
| 387 | + const client2 = getTestClient(); |
| 388 | + |
| 389 | + const integrationIndex = {}; |
| 390 | + const integration1 = new CustomIntegration(); |
| 391 | + const integration2 = new CustomIntegration(); |
| 392 | + const integration3 = new CustomIntegration(); |
| 393 | + const integration4 = new CustomIntegration(); |
| 394 | + |
| 395 | + setupIntegration(client1, integration1, integrationIndex); |
| 396 | + setupIntegration(client1, integration2, integrationIndex); |
| 397 | + setupIntegration(client2, integration3, integrationIndex); |
| 398 | + setupIntegration(client2, integration4, integrationIndex); |
| 399 | + |
| 400 | + expect(integrationIndex).toEqual({ test: integration4 }); |
| 401 | + expect(integration1.setupOnce).toHaveBeenCalledTimes(1); |
| 402 | + expect(integration2.setupOnce).not.toHaveBeenCalled(); |
| 403 | + expect(integration3.setupOnce).not.toHaveBeenCalled(); |
| 404 | + expect(integration4.setupOnce).not.toHaveBeenCalled(); |
| 405 | + |
| 406 | + client1.captureEvent({ event_id: '1a' }); |
| 407 | + client1.captureEvent({ event_id: '1b' }); |
| 408 | + client2.captureEvent({ event_id: '2a' }); |
| 409 | + client2.captureEvent({ event_id: '2b' }); |
| 410 | + client2.captureEvent({ event_id: '2c' }); |
| 411 | + |
| 412 | + expect(integration1.preprocessEvent).toHaveBeenCalledTimes(2); |
| 413 | + expect(integration2.preprocessEvent).toHaveBeenCalledTimes(2); |
| 414 | + expect(integration3.preprocessEvent).toHaveBeenCalledTimes(3); |
| 415 | + expect(integration4.preprocessEvent).toHaveBeenCalledTimes(3); |
| 416 | + |
| 417 | + expect(integration1.preprocessEvent).toHaveBeenLastCalledWith({ event_id: '1b' }, {}, client1); |
| 418 | + expect(integration2.preprocessEvent).toHaveBeenLastCalledWith({ event_id: '1b' }, {}, client1); |
| 419 | + expect(integration3.preprocessEvent).toHaveBeenLastCalledWith({ event_id: '2c' }, {}, client2); |
| 420 | + expect(integration4.preprocessEvent).toHaveBeenLastCalledWith({ event_id: '2c' }, {}, client2); |
| 421 | + }); |
| 422 | + |
| 423 | + it('allows to mutate events in preprocessEvent', async () => { |
| 424 | + class CustomIntegration implements Integration { |
| 425 | + name = 'test'; |
| 426 | + setupOnce = jest.fn(); |
| 427 | + preprocessEvent = jest.fn(event => { |
| 428 | + event.event_id = 'mutated'; |
| 429 | + }); |
| 430 | + } |
| 431 | + |
| 432 | + const client = getTestClient(); |
| 433 | + |
| 434 | + const integrationIndex = {}; |
| 435 | + const integration = new CustomIntegration(); |
| 436 | + |
| 437 | + setupIntegration(client, integration, integrationIndex); |
| 438 | + |
| 439 | + const sendEvent = jest.fn(); |
| 440 | + client.sendEvent = sendEvent; |
| 441 | + |
| 442 | + client.captureEvent({ event_id: '1a' }); |
| 443 | + await client.flush(); |
| 444 | + |
| 445 | + expect(sendEvent).toHaveBeenCalledTimes(1); |
| 446 | + expect(sendEvent).toHaveBeenCalledWith(expect.objectContaining({ event_id: 'mutated' }), {}); |
| 447 | + }); |
| 448 | + |
| 449 | + it('binds processEvent for each client', () => { |
| 450 | + class CustomIntegration implements Integration { |
| 451 | + name = 'test'; |
| 452 | + setupOnce = jest.fn(); |
| 453 | + processEvent = jest.fn(event => { |
| 454 | + return event; |
| 455 | + }); |
| 456 | + } |
| 457 | + |
| 458 | + const client1 = getTestClient(); |
| 459 | + const client2 = getTestClient(); |
| 460 | + |
| 461 | + const integrationIndex = {}; |
| 462 | + const integration1 = new CustomIntegration(); |
| 463 | + const integration2 = new CustomIntegration(); |
| 464 | + const integration3 = new CustomIntegration(); |
| 465 | + const integration4 = new CustomIntegration(); |
| 466 | + |
| 467 | + setupIntegration(client1, integration1, integrationIndex); |
| 468 | + setupIntegration(client1, integration2, integrationIndex); |
| 469 | + setupIntegration(client2, integration3, integrationIndex); |
| 470 | + setupIntegration(client2, integration4, integrationIndex); |
| 471 | + |
| 472 | + expect(integrationIndex).toEqual({ test: integration4 }); |
| 473 | + expect(integration1.setupOnce).toHaveBeenCalledTimes(1); |
| 474 | + expect(integration2.setupOnce).not.toHaveBeenCalled(); |
| 475 | + expect(integration3.setupOnce).not.toHaveBeenCalled(); |
| 476 | + expect(integration4.setupOnce).not.toHaveBeenCalled(); |
| 477 | + |
| 478 | + client1.captureEvent({ event_id: '1a' }); |
| 479 | + client1.captureEvent({ event_id: '1b' }); |
| 480 | + client2.captureEvent({ event_id: '2a' }); |
| 481 | + client2.captureEvent({ event_id: '2b' }); |
| 482 | + client2.captureEvent({ event_id: '2c' }); |
| 483 | + |
| 484 | + expect(integration1.processEvent).toHaveBeenCalledTimes(2); |
| 485 | + expect(integration2.processEvent).toHaveBeenCalledTimes(2); |
| 486 | + expect(integration3.processEvent).toHaveBeenCalledTimes(3); |
| 487 | + expect(integration4.processEvent).toHaveBeenCalledTimes(3); |
| 488 | + |
| 489 | + expect(integration1.processEvent).toHaveBeenLastCalledWith( |
| 490 | + expect.objectContaining({ event_id: '1b' }), |
| 491 | + {}, |
| 492 | + client1, |
| 493 | + ); |
| 494 | + expect(integration2.processEvent).toHaveBeenLastCalledWith( |
| 495 | + expect.objectContaining({ event_id: '1b' }), |
| 496 | + {}, |
| 497 | + client1, |
| 498 | + ); |
| 499 | + expect(integration3.processEvent).toHaveBeenLastCalledWith( |
| 500 | + expect.objectContaining({ event_id: '2c' }), |
| 501 | + {}, |
| 502 | + client2, |
| 503 | + ); |
| 504 | + expect(integration4.processEvent).toHaveBeenLastCalledWith( |
| 505 | + expect.objectContaining({ event_id: '2c' }), |
| 506 | + {}, |
| 507 | + client2, |
| 508 | + ); |
| 509 | + }); |
| 510 | + |
| 511 | + it('allows to mutate events in processEvent', async () => { |
| 512 | + class CustomIntegration implements Integration { |
| 513 | + name = 'test'; |
| 514 | + setupOnce = jest.fn(); |
| 515 | + processEvent = jest.fn(_event => { |
| 516 | + return { event_id: 'mutated' }; |
| 517 | + }); |
| 518 | + } |
| 519 | + |
| 520 | + const client = getTestClient(); |
| 521 | + |
| 522 | + const integrationIndex = {}; |
| 523 | + const integration = new CustomIntegration(); |
| 524 | + |
| 525 | + setupIntegration(client, integration, integrationIndex); |
| 526 | + |
| 527 | + const sendEvent = jest.fn(); |
| 528 | + client.sendEvent = sendEvent; |
| 529 | + |
| 530 | + client.captureEvent({ event_id: '1a' }); |
| 531 | + await client.flush(); |
| 532 | + |
| 533 | + expect(sendEvent).toHaveBeenCalledTimes(1); |
| 534 | + expect(sendEvent).toHaveBeenCalledWith(expect.objectContaining({ event_id: 'mutated' }), {}); |
| 535 | + }); |
| 536 | + |
| 537 | + it('allows to drop events in processEvent', async () => { |
| 538 | + class CustomIntegration implements Integration { |
| 539 | + name = 'test'; |
| 540 | + setupOnce = jest.fn(); |
| 541 | + processEvent = jest.fn(_event => { |
| 542 | + return null; |
| 543 | + }); |
| 544 | + } |
| 545 | + |
| 546 | + const client = getTestClient(); |
| 547 | + |
| 548 | + const integrationIndex = {}; |
| 549 | + const integration = new CustomIntegration(); |
| 550 | + |
| 551 | + setupIntegration(client, integration, integrationIndex); |
| 552 | + |
| 553 | + const sendEvent = jest.fn(); |
| 554 | + client.sendEvent = sendEvent; |
| 555 | + |
| 556 | + client.captureEvent({ event_id: '1a' }); |
| 557 | + await client.flush(); |
| 558 | + |
| 559 | + expect(sendEvent).not.toHaveBeenCalled(); |
| 560 | + }); |
| 561 | +}); |
0 commit comments