From b7ac9e4a85a6060ef97d31cd6b8ea936e93ebcb8 Mon Sep 17 00:00:00 2001 From: teodosii Date: Tue, 20 Dec 2022 11:22:41 +0200 Subject: [PATCH] basic test for faro --- grafana-plugin/src/utils/faro.test.ts | 84 +++++++++++++++++++++++++++ grafana-plugin/src/utils/faro.ts | 4 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 grafana-plugin/src/utils/faro.test.ts diff --git a/grafana-plugin/src/utils/faro.test.ts b/grafana-plugin/src/utils/faro.test.ts new file mode 100644 index 00000000..04bb93de --- /dev/null +++ b/grafana-plugin/src/utils/faro.test.ts @@ -0,0 +1,84 @@ +import FaroHelper from 'utils/faro'; + +import 'jest/matchMedia.ts'; + +import { describe, test } from '@jest/globals'; +import '@testing-library/jest-dom'; + +jest.mock('@grafana/faro-web-sdk', () => ({ + initializeFaro: jest.fn().mockReturnValue({ + api: { + pushLog: jest.fn(), + }, + }), + getWebInstrumentations: () => [], +})); + +jest.mock('@grafana/faro-web-tracing', () => ({ + TracingInstrumentation: jest.fn(), +})); +jest.mock('@opentelemetry/instrumentation-document-load', () => ({ + DocumentLoadInstrumentation: jest.fn(), +})); +jest.mock('@opentelemetry/instrumentation-fetch', () => ({ + FetchInstrumentation: jest.fn(), +})); + +describe('Faro', () => { + const OLD_ENV = process.env; + + beforeEach(() => { + jest.resetModules(); + process.env = { ...OLD_ENV }; + }); + + afterAll(() => { + process.env = OLD_ENV; + }); + + const getProcessEnv = ( + config: { faroUrl?: string; apiKey?: string; enabled?: string } = { + faroUrl: 'localhost:12345/collect', + apiKey: 'secret', + enabled: 'true', + } + ) => { + const { faroUrl, apiKey, enabled } = config; + + return { + FARO_URL: faroUrl, + FARO_API_KEY: apiKey, + FARO_ENABLED: enabled, + }; + }; + + test('It initializes faro ENABLED === true', () => { + process.env = getProcessEnv(); + const faro = FaroHelper.initializeFaro(); + + expect(faro).toBeDefined(); + expect(faro.api.pushLog).toHaveBeenCalledTimes(1); + }); + + test('It does not initialize faro if ENABLED != true', () => { + process.env = getProcessEnv({ enabled: 'some-other-value-here' }); + const faro = FaroHelper.initializeFaro(); + expect(faro).toBeUndefined(); + }); + + test('It skips initializing if values are missing', () => { + let faro; + + process.env = getProcessEnv({ faroUrl: undefined }); + faro = FaroHelper.initializeFaro(); + expect(faro).toBeUndefined(); + + process.env = getProcessEnv({ apiKey: undefined }); + faro = FaroHelper.initializeFaro(); + expect(faro).toBeUndefined(); + + process.env = getProcessEnv({ enabled: undefined }); + faro = FaroHelper.initializeFaro(); + expect(faro).toBeUndefined(); + }); +}); diff --git a/grafana-plugin/src/utils/faro.ts b/grafana-plugin/src/utils/faro.ts index 34a13ba1..7cec49c0 100644 --- a/grafana-plugin/src/utils/faro.ts +++ b/grafana-plugin/src/utils/faro.ts @@ -28,7 +28,7 @@ class FaroHelper { }; if (!faroConfig?.enabled || !faroConfig?.url || !faroConfig?.apiKey || this.faro) { - return; + return undefined; } try { @@ -58,6 +58,8 @@ class FaroHelper { this.faro.api.pushLog(['Faro was initialized for Grafana OnCall']); } catch (ex) {} + + return this.faro; } }