|
| 1 | +import { Integration } from '@sentry/types'; |
| 2 | +import { init } from '../src/sdk'; |
| 3 | +import * as sdk from '../src/sdk'; |
| 4 | + |
| 5 | +// eslint-disable-next-line no-var |
| 6 | +declare var global: any; |
| 7 | + |
| 8 | +const PUBLIC_DSN = 'https://username@domain/123'; |
| 9 | + |
| 10 | +class MockIntegration implements Integration { |
| 11 | + public name: string; |
| 12 | + public setupOnce = jest.fn(); |
| 13 | + public constructor(name: string) { |
| 14 | + this.name = name; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +const defaultIntegrationsBackup = sdk.defaultIntegrations; |
| 19 | + |
| 20 | +describe('init()', () => { |
| 21 | + beforeEach(() => { |
| 22 | + global.__SENTRY__ = {}; |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + // @ts-ignore |
| 27 | + sdk.defaultIntegrations = defaultIntegrationsBackup; |
| 28 | + }); |
| 29 | + |
| 30 | + it("doesn't install default integrations if told not to", () => { |
| 31 | + const mockDefaultIntegrations = [ |
| 32 | + new MockIntegration('Mock integration 1.1'), |
| 33 | + new MockIntegration('Mock integration 1.2'), |
| 34 | + ]; |
| 35 | + |
| 36 | + //@ts-ignore |
| 37 | + sdk.defaultIntegrations = mockDefaultIntegrations; |
| 38 | + |
| 39 | + init({ dsn: PUBLIC_DSN, defaultIntegrations: false }); |
| 40 | + |
| 41 | + expect(mockDefaultIntegrations[0].setupOnce as jest.Mock).toHaveBeenCalledTimes(0); |
| 42 | + expect(mockDefaultIntegrations[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(0); |
| 43 | + }); |
| 44 | + |
| 45 | + it('installs merged default integrations, with overrides provided through options', () => { |
| 46 | + const mockDefaultIntegrations = [ |
| 47 | + new MockIntegration('Some mock integration 2.1'), |
| 48 | + new MockIntegration('Some mock integration 2.2'), |
| 49 | + ]; |
| 50 | + |
| 51 | + //@ts-ignore |
| 52 | + sdk.defaultIntegrations = mockDefaultIntegrations; |
| 53 | + |
| 54 | + const mockIntegrations = [ |
| 55 | + new MockIntegration('Some mock integration 2.1'), |
| 56 | + new MockIntegration('Some mock integration 2.3'), |
| 57 | + ]; |
| 58 | + |
| 59 | + init({ dsn: PUBLIC_DSN, integrations: mockIntegrations }); |
| 60 | + |
| 61 | + expect(mockDefaultIntegrations[0].setupOnce as jest.Mock).toHaveBeenCalledTimes(0); |
| 62 | + expect(mockDefaultIntegrations[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(1); |
| 63 | + expect(mockIntegrations[0].setupOnce as jest.Mock).toHaveBeenCalledTimes(1); |
| 64 | + expect(mockIntegrations[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(1); |
| 65 | + }); |
| 66 | + |
| 67 | + it('installs integrations returned from a callback function', () => { |
| 68 | + const mockDefaultIntegrations = [ |
| 69 | + new MockIntegration('Some mock integration 3.1'), |
| 70 | + new MockIntegration('Some mock integration 3.2'), |
| 71 | + ]; |
| 72 | + |
| 73 | + //@ts-ignore |
| 74 | + sdk.defaultIntegrations = mockDefaultIntegrations; |
| 75 | + |
| 76 | + const newIntegration = new MockIntegration('Some mock integration 3.3'); |
| 77 | + |
| 78 | + init({ |
| 79 | + dsn: PUBLIC_DSN, |
| 80 | + integrations: integrations => { |
| 81 | + const newIntegrations = [...integrations]; |
| 82 | + newIntegrations[1] = newIntegration; |
| 83 | + return newIntegrations; |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(mockDefaultIntegrations[0].setupOnce as jest.Mock).toHaveBeenCalledTimes(1); |
| 88 | + expect(mockDefaultIntegrations[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(0); |
| 89 | + expect(newIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1); |
| 90 | + }); |
| 91 | +}); |
0 commit comments