From b85269d8b33611ca934dd2f229473e81870a0356 Mon Sep 17 00:00:00 2001 From: Billy Vong Date: Wed, 20 Nov 2024 11:33:23 -0500 Subject: [PATCH] add error hook test --- .../featureFlags/openfeature/basic/test.ts | 3 +- .../openfeature/errorHook/init.js | 20 ++++++++ .../openfeature/errorHook/test.ts | 49 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/init.js create mode 100644 dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/test.ts diff --git a/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/basic/test.ts b/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/basic/test.ts index 9a4e360a3b06..a3de589677ea 100644 --- a/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/basic/test.ts +++ b/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/basic/test.ts @@ -22,14 +22,13 @@ sentryTest('Basic test with eviction, update, and no async tasks', async ({ getL const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true }); await page.goto(url); - await page.waitForFunction(bufferSize => { + await page.evaluate(bufferSize => { const client = (window as any).initialize(); for (let i = 1; i <= bufferSize; i++) { client.getBooleanValue(`feat${i}`, false); } client.getBooleanValue(`feat${bufferSize + 1}`, true); // eviction client.getBooleanValue('feat3', true); // update - return true; }, FLAG_BUFFER_SIZE); const reqPromise = waitForErrorRequest(page); diff --git a/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/init.js b/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/init.js new file mode 100644 index 000000000000..971e08755fe6 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/init.js @@ -0,0 +1,20 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; +window.sentryOpenFeatureIntegration = Sentry.openFeatureIntegration(); + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + sampleRate: 1.0, + integrations: [window.sentryOpenFeatureIntegration], +}); + +window.initialize = () => { + return { + getBooleanValue(flag, value) { + let hook = new Sentry.OpenFeatureIntegrationHook(); + hook.error({ flagKey: flag, defaultValue: false }, new Error('flag eval error')); + return value; + }, + }; +}; diff --git a/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/test.ts b/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/test.ts new file mode 100644 index 000000000000..719782d0b0ab --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/integrations/featureFlags/openfeature/errorHook/test.ts @@ -0,0 +1,49 @@ +import { expect } from '@playwright/test'; + +import { sentryTest } from '../../../../../utils/fixtures'; + +import { envelopeRequestParser, shouldSkipFeatureFlagsTest, waitForErrorRequest } from '../../../../../utils/helpers'; + +const FLAG_BUFFER_SIZE = 100; // Corresponds to constant in featureFlags.ts, in browser utils. + +sentryTest('Flag evaluation error hook', async ({ getLocalTestUrl, page }) => { + if (shouldSkipFeatureFlagsTest()) { + sentryTest.skip(); + } + + await page.route('https://dsn.ingest.sentry.io/**/*', route => { + return route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ id: 'test-id' }), + }); + }); + + const url = await getLocalTestUrl({ testDir: __dirname, skipDsnRouteHandler: true }); + await page.goto(url); + + await page.evaluate(bufferSize => { + const client = (window as any).initialize(); + for (let i = 1; i <= bufferSize; i++) { + client.getBooleanValue(`feat${i}`, false); + } + client.getBooleanValue(`feat${bufferSize + 1}`, true); // eviction + client.getBooleanValue('feat3', true); // update + }, FLAG_BUFFER_SIZE); + + const reqPromise = waitForErrorRequest(page); + await page.locator('#error').click(); + const req = await reqPromise; + const event = envelopeRequestParser(req); + + // Default value is mocked as false -- these will all error and use default + // value + const expectedFlags = [{ flag: 'feat2', result: false }]; + for (let i = 4; i <= FLAG_BUFFER_SIZE; i++) { + expectedFlags.push({ flag: `feat${i}`, result: false }); + } + expectedFlags.push({ flag: `feat${FLAG_BUFFER_SIZE + 1}`, result: false }); + expectedFlags.push({ flag: 'feat3', result: false }); + + expect(event.contexts?.flags?.values).toEqual(expectedFlags); +});