|
1 | 1 | import type { Integration, Options } from '@sentry/types'; |
| 2 | +import { logger } from '@sentry/utils'; |
2 | 3 |
|
3 | | -import { getIntegrationsToSetup, installedIntegrations, setupIntegration } from '../../src/integration'; |
| 4 | +import { Hub, makeMain } from '../../src/hub'; |
| 5 | +import { addIntegration, getIntegrationsToSetup, installedIntegrations, setupIntegration } from '../../src/integration'; |
4 | 6 | import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; |
5 | 7 |
|
6 | 8 | function getTestClient(): TestClient { |
@@ -559,3 +561,51 @@ describe('setupIntegration', () => { |
559 | 561 | expect(sendEvent).not.toHaveBeenCalled(); |
560 | 562 | }); |
561 | 563 | }); |
| 564 | + |
| 565 | +describe('addIntegration', () => { |
| 566 | + beforeEach(function () { |
| 567 | + // Reset the (global!) list of installed integrations |
| 568 | + installedIntegrations.splice(0, installedIntegrations.length); |
| 569 | + }); |
| 570 | + |
| 571 | + afterEach(() => { |
| 572 | + jest.clearAllMocks(); |
| 573 | + }); |
| 574 | + |
| 575 | + it('works with a client setup', () => { |
| 576 | + const warnings = jest.spyOn(logger, 'warn'); |
| 577 | + |
| 578 | + class CustomIntegration implements Integration { |
| 579 | + name = 'test'; |
| 580 | + setupOnce = jest.fn(); |
| 581 | + } |
| 582 | + |
| 583 | + const client = getTestClient(); |
| 584 | + const hub = new Hub(client); |
| 585 | + makeMain(hub); |
| 586 | + |
| 587 | + const integration = new CustomIntegration(); |
| 588 | + addIntegration(integration); |
| 589 | + |
| 590 | + expect(integration.setupOnce).toHaveBeenCalledTimes(1); |
| 591 | + expect(warnings).not.toHaveBeenCalled(); |
| 592 | + }); |
| 593 | + |
| 594 | + it('works without a client setup', () => { |
| 595 | + const warnings = jest.spyOn(logger, 'warn'); |
| 596 | + class CustomIntegration implements Integration { |
| 597 | + name = 'test'; |
| 598 | + setupOnce = jest.fn(); |
| 599 | + } |
| 600 | + |
| 601 | + const hub = new Hub(); |
| 602 | + makeMain(hub); |
| 603 | + |
| 604 | + const integration = new CustomIntegration(); |
| 605 | + addIntegration(integration); |
| 606 | + |
| 607 | + expect(integration.setupOnce).not.toHaveBeenCalled(); |
| 608 | + expect(warnings).toHaveBeenCalledTimes(1); |
| 609 | + expect(warnings).toHaveBeenCalledWith('Cannot add integration "test" because no SDK Client is available.'); |
| 610 | + }); |
| 611 | +}); |
0 commit comments