|
| 1 | +const mockTrackTrace = jest.fn(); |
| 2 | +const mockSetSendLiveMetrics = jest.fn().mockReturnThis(); |
| 3 | +const mockStart = jest.fn().mockReturnThis(); |
| 4 | + |
| 5 | +jest.mock('applicationinsights', () => { |
| 6 | + return { |
| 7 | + setup: jest.fn(() => ({ |
| 8 | + setSendLiveMetrics: mockSetSendLiveMetrics, |
| 9 | + start: mockStart, |
| 10 | + })), |
| 11 | + defaultClient: { |
| 12 | + context: { |
| 13 | + tags: {}, |
| 14 | + keys: { |
| 15 | + cloudRole: 'undefined-frontend', |
| 16 | + }, |
| 17 | + }, |
| 18 | + trackTrace: mockTrackTrace, |
| 19 | + }, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +import * as appInsights from 'applicationinsights'; |
| 24 | +import { AppInsights } from '../../../../main/modules/appinsights'; |
| 25 | + |
| 26 | +jest.mock('config', () => ({ |
| 27 | + get: jest.fn(() => { |
| 28 | + return 'CONFIG_CONNECTION_STRING'; |
| 29 | + }), |
| 30 | +})); |
| 31 | + |
| 32 | +describe('App Insights', () => { |
| 33 | + beforeEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + delete process.env.APP_INSIGHTS_CONNECTION_STRING; |
| 36 | + appInsights.defaultClient.context.tags = {}; |
| 37 | + }); |
| 38 | + |
| 39 | + it('Uses environment variable for connection string', () => { |
| 40 | + process.env.APP_INSIGHTS_CONNECTION_STRING = 'ENV_CONNECTION_STRING'; |
| 41 | + const appInsightsLibrary = new AppInsights(); |
| 42 | + appInsightsLibrary.enable(); |
| 43 | + |
| 44 | + expect(appInsights.setup).toHaveBeenCalledWith('ENV_CONNECTION_STRING'); |
| 45 | + expect(mockSetSendLiveMetrics).toHaveBeenCalledWith(true); |
| 46 | + expect(mockStart).toHaveBeenCalled(); |
| 47 | + expect(appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.cloudRole]).toBe( |
| 48 | + 'pip-frontend' |
| 49 | + ); |
| 50 | + expect(mockTrackTrace).toHaveBeenCalledWith({ message: 'App insights activated' }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('Uses config for connection string if env not set', () => { |
| 54 | + const appInsightsLibrary = new AppInsights(); |
| 55 | + appInsightsLibrary.enable(); |
| 56 | + |
| 57 | + expect(appInsights.setup).toHaveBeenCalledWith('CONFIG_CONNECTION_STRING'); |
| 58 | + expect(mockSetSendLiveMetrics).toHaveBeenCalledWith(true); |
| 59 | + expect(mockStart).toHaveBeenCalled(); |
| 60 | + expect(appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.cloudRole]).toBe( |
| 61 | + 'pip-frontend' |
| 62 | + ); |
| 63 | + expect(mockTrackTrace).toHaveBeenCalledWith({ message: 'App insights activated' }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('Does nothing if no connection string is available', async () => { |
| 67 | + jest.resetModules(); |
| 68 | + jest.doMock('config', () => ({ |
| 69 | + get: jest.fn(() => undefined), |
| 70 | + })); |
| 71 | + delete process.env.APP_INSIGHTS_CONNECTION_STRING; |
| 72 | + |
| 73 | + const { AppInsights: AppInsightsNoConfig } = await import('../../../../main/modules/appinsights'); |
| 74 | + const appInsightsLibrary = new AppInsightsNoConfig(); |
| 75 | + appInsightsLibrary.enable(); |
| 76 | + |
| 77 | + expect(appInsights.setup).not.toHaveBeenCalled(); |
| 78 | + expect(mockSetSendLiveMetrics).not.toHaveBeenCalled(); |
| 79 | + expect(mockStart).not.toHaveBeenCalled(); |
| 80 | + expect(mockTrackTrace).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments