Skip to content

Commit 948cea7

Browse files
committed
Fix test
1 parent c27f766 commit 948cea7

File tree

3 files changed

+36
-25
lines changed
  • dev-packages/browser-integration-tests/suites/integrations/featureFlags/launchdarkly
  • packages/browser/src/integrations

3 files changed

+36
-25
lines changed
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Sentry from '@sentry/browser';
2-
import { launchDarklyIntegration } from '@sentry/browser';
2+
import { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from '@sentry/browser';
3+
34

45
window.Sentry = Sentry;
56
window.LDIntegration = launchDarklyIntegration();
@@ -10,18 +11,25 @@ Sentry.init({
1011
integrations: [window.LDIntegration],
1112
});
1213

13-
// TODO: can't type this unless we duplicate to every test.ts file
1414
window.MockLaunchDarkly = {
1515
initialize(_clientId, context, options) {
16-
// const flagUsedHandler = options?.inspectors?.[0].method;
16+
const flagUsedHandler = (options && options.inspectors) ? options.inspectors[0].method : undefined;
1717

1818
return {
1919
variation(key, defaultValue) {
20-
// flagUsedHandler?.(key, { value: defaultValue }, context);
20+
if (flagUsedHandler) {
21+
flagUsedHandler(key, { value: defaultValue }, context); // TODO:this is async atm
22+
}
2123
return defaultValue;
2224
},
2325
};
2426
},
2527
};
2628

27-
console.log(window.MockLaunchDarkly)
29+
window.InitializeLD = () => {
30+
return window.MockLaunchDarkly.initialize(
31+
'example-client-id',
32+
{ kind: 'user', key: 'example-context-key' },
33+
{ inspectors: [buildLaunchDarklyFlagUsedHandler()] },
34+
);
35+
};

dev-packages/browser-integration-tests/suites/integrations/featureFlags/launchdarkly/test.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ import { expect } from '@playwright/test';
33
import { sentryTest } from '../../../../utils/fixtures';
44

55
import { envelopeRequestParser, waitForErrorRequest } from '../../../../utils/helpers';
6-
import { buildLaunchDarklyFlagUsedHandler } from '@sentry/browser';
7-
import type { LDContext, LDOptions, LDFlagValue, LDClient } from 'launchdarkly-js-client-sdk';
6+
import type { LDContext, LDOptions, LDFlagValue, LDClient, LDEvaluationDetail } from 'launchdarkly-js-client-sdk';
87
import type { Event } from '@sentry/types';
98

10-
// const MockLaunchDarkly = { //TODO:
9+
// const MockLaunchDarkly = { //TODO: remove in favor of window.MockLaunchDarkly from init.js
1110
// initialize(
1211
// _clientId: string,
1312
// context: LDContext,
1413
// options: LDOptions,
1514
// ) {
1615
// const flagUsedHandler = options?.inspectors?.[0].method;
16+
// const wellTypedHandler = flagUsedHandler as ((
17+
// flagKey: string,
18+
// flagDetail: LDEvaluationDetail,
19+
// context: LDContext,
20+
// ) => void) | undefined;
1721

1822
// return {
1923
// variation(key: string, defaultValue: LDFlagValue) {
20-
// flagUsedHandler?.(key, { value: defaultValue }, context);
24+
// wellTypedHandler?.(key, { value: defaultValue }, context);
2125
// return defaultValue;
2226
// },
2327
// };
@@ -44,24 +48,24 @@ sentryTest('e2e test', async ({ getLocalTestPath, page }) => {
4448
await page.goto(url);
4549

4650
// TODO: could this be in init.js?
47-
const ldClient = await page.evaluate(() => {
48-
return (window as any).MockLaunchDarkly.initialize(
49-
'example-client-id',
50-
{ kind: 'user', key: 'example-context-key' },
51-
{ inspectors: [buildLaunchDarklyFlagUsedHandler()] },
52-
) as LDClient;
51+
await page.waitForFunction(() => {
52+
const ldClient = (window as any).InitializeLD();
53+
ldClient.variation('feat1', false);
54+
ldClient.variation('feat2', false);
55+
ldClient.variation('feat3', false);
56+
ldClient.variation('feat2', true);
57+
return true;
5358
});
5459

55-
ldClient.variation('feat1', false);
56-
ldClient.variation('feat2', false);
57-
ldClient.variation('feat3', false);
58-
ldClient.variation('feat2', true);
60+
5961
// TODO: eviction not tested
6062

6163
// trigger error
64+
const reqPromise = waitForErrorRequest(page);
6265
await page.locator('#error').click();
66+
const req = await reqPromise;
6367

64-
const req = await waitForErrorRequest(page);
68+
// console.log(errorEventId);
6569
const event = envelopeRequestParser(req);
6670

6771
expect(event.contexts?.flags?.values).toEqual([

packages/browser/src/integrations/launchdarkly.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ export const launchDarklyIntegration = defineIntegration(() => {
3737
}) satisfies IntegrationFn;
3838

3939
/**
40-
* Constructs a LaunchDarkly hook that listens for flag evaluations and updates
41-
* the flagBuffer in our current scope.
42-
*
43-
* This needs to be registered separately in the LD SDK initialize() options,
44-
* after initializing Sentry.
40+
* LaunchDarkly hook that listens for flag evaluations and updates the `flags`
41+
* context in our Sentry scope. This needs to be registered as an
42+
* 'inspector' in LaunchDarkly initialize() options, separately from
43+
* `launchDarklyIntegration`. Both are needed to collect feature flags on error.
4544
*/
4645
export function buildLaunchDarklyFlagUsedHandler(): LDInspectionFlagUsedHandler {
4746
return {

0 commit comments

Comments
 (0)