Skip to main content

Published

Set up feature flag coverage

In brief: Paste one prompt into your coding agent, or do it by hand. Connect your flag provider in TestChimp settings, add @testchimp/flags to the frontend, add the server middleware only if your backend evaluates flags, then run your tests.

Let your agent do it​

With the TestChimp skill installed, paste this into your coding agent:

Set up feature flag coverage with TestChimp for this repo.
Use the TestChimp skill: /testchimp setup feature flag coverage.
Detect which flag SDK the app uses (LaunchDarkly, PostHog, OpenFeature), instrument the frontend and, if the backend evaluates flags, the Node backend. Show me your plan before you change anything.
Do not put any provider credentials in chat. Tell me when I need to connect the provider in TestChimp settings.

The agent finds your flag SDK and where the app creates its flag client, shows you a plan, adds the hooks, sets TESTCHIMP_FLAG_ECHO=1 in test environment config only, runs one test and checks that evaluations reached TestChimp. It never asks for provider credentials: when the connection is needed, it gives you the settings link. See the Set up Feature Flag Coverage workflow for each step.

The short form is /testchimp setup feature flag coverage.

Manual setup​

Before you start​

  • Your Playwright tests use @testchimp/playwright 0.4.0 or later. Flag capture is on by default; there is nothing to enable on the test side.
  • Your tests are linked to scenarios, so their flags roll up to stories. See Linking scenarios.

Step 1: Connect your flag provider​

An org admin opens Project settings → Integrations → Feature flags, picks the provider, enters the credentials, tests the connection, and picks the environment your tests run against. TestChimp syncs the catalog when you save, once a day after that, and whenever you click Sync now.

Provider guides: LaunchDarkly, PostHog, Statsig.

You can skip this step and come back to it. Capture works without a connection; the connection adds the arms that no test has run yet.

Step 2: Instrument the frontend​

Install the package in the app under test:

npm install @testchimp/flags

Then add the hook where your app creates its flag client:

The LaunchDarkly JS SDK only accepts inspectors at initialize time, so wrap the options you pass to initialize:

import { instrumentLaunchDarkly } from '@testchimp/flags/web';

const client = LDClient.initialize(clientSideId, context, instrumentLaunchDarkly({ bootstrap }));

Each variation call is recorded. allFlags() is not counted.

For any other SDK, report the arm yourself with reportFlag(flagKey, variant, providerId). Statsig has no hook yet; see Statsig for the options.

In production these calls do nothing: they check for window.__TESTCHIMP_FLAGS__, which only the TestChimp fixture defines.

Step 3: Instrument the backend (only if it evaluates flags)​

Skip this step if all your flags are evaluated in the browser.

If the backend that answers your app's requests evaluates flags, add the collection middleware and the provider hook (Node only in this version):

// Express or Connect
import { flagCollectionMiddleware } from '@testchimp/flags/server';
app.use(flagCollectionMiddleware());

// Fastify
import { fastifyFlagCollection } from '@testchimp/flags/server';
await app.register(fastifyFlagCollection);

// Next.js App Router route handler, or any (req, res) handler
import { withFlagCollection } from '@testchimp/flags/server';
export const GET = withFlagCollection(async (request: Request) => Response.json(await loadOrders()));
import { instrumentLaunchDarklyNode } from '@testchimp/flags/server';

instrumentLaunchDarklyNode(ldClient); // @launchdarkly/node-server-sdk v9.3+, after init

Then set this in the test environment that runs the backend, and nowhere else:

TESTCHIMP_FLAG_ECHO=1

Never add TESTCHIMP_FLAG_ECHO to a production env file or manifest. Without it, the middleware does nothing.

For a backend in another language, implement the header contract. It takes about 25 lines.

Step 4: Run your tests​

Run your Playwright tests as usual, locally or in CI. The fixture records the flags each test ran and uploads them when the test ends.

Step 5: Verify​

Open Insights → Feature flag coverage. Stories whose tests evaluated flags show their flags and arms. If you connected a provider, untested arms appear too.

Agents can check the same with get-feature-flag-setup-status: anyEvaluationsIngested should be true, and sourcesSeen should list FRONTEND, and BACKEND if you set up the backend. See Reference.

Troubleshooting​

A flag shows as untested, but the test ran it in the backend. Check that TESTCHIMP_FLAG_ECHO=1 is set in the environment the backend runs in, not only in the test runner. Then check middleware order: the collection middleware must run before the handlers that evaluate flags. Flags evaluated in a service behind the one answering the request are not captured.

No evaluations reach TestChimp. Check that the tests use @testchimp/playwright and that TESTCHIMP_DISABLE_FLAG_CAPTURE is not set. Check that the frontend hook is installed where the flag client is created, before the first evaluation. For LaunchDarkly, the options must be wrapped at initialize time. For PostHog, lookups with send_event: false are not recorded.

Flags show, but not under any story. The tests that ran them are not linked to scenarios, or those scenarios are not linked to stories. get-feature-flag-setup-status lists these flags in observedUnmappedFlagKeys. Link the tests, or pin the flag to a story.

An arm shows a "not in provider" note. A test recorded a value that the catalog does not list. Click Sync now in settings. If it persists, the value your app reports differs from the provider's variation, for example a display name instead of the value.

The header was truncated. The encoded x-testchimp-flags header is capped at 4096 bytes. When a request evaluates more flags than fit, the first entries are kept and x-testchimp-flags-truncated: 1 is set. Flags past the cap are not recorded for that response.

The flag shows "Catalog unknown". No connected provider has a flag with that key. Connect the provider, or check that the provider environment and project match what the tests run against.

Turn capture off​

To stop the fixture from recording flags for a run, set:

TESTCHIMP_DISABLE_FLAG_CAPTURE=1

It is independent of API capture (TESTCHIMP_ENABLE_API_CAPTURE).

FAQ

Can my coding agent set this up?

Yes. With the TestChimp skill installed, run /testchimp setup feature flag coverage. The agent shows a plan first and never asks for provider credentials.

Do I need to enable flag capture in tests?

No. @testchimp/playwright captures flags by default. Set TESTCHIMP_DISABLE_FLAG_CAPTURE=1 to turn it off.

When do I need TESTCHIMP_FLAG_ECHO?

Only when your backend evaluates flags. Set it in the test environment that runs the backend, never in production.

Why does a backend flag show as untested?

Usually TESTCHIMP_FLAG_ECHO is not set where the backend runs, or the collection middleware runs after the handlers that evaluate flags.