Skip to content
Latchkey

How to Mock Feature Flags in CI Tests

Swap the real provider for an in-memory one in tests so you control every flag value with no network dependency.

Tests should not depend on a live flag service. Register an in-memory provider (or stub the accessor) so each test sets the exact flag values it needs, making runs fast and deterministic.

Steps

  • Register an in-memory provider in test setup.
  • Set the flag values each test case requires.
  • Assert behavior for those forced values.

In-memory provider

Terminal
import { OpenFeature, InMemoryProvider } from '@openfeature/server-sdk';
await OpenFeature.setProviderAndWait(new InMemoryProvider({
  'new-checkout': {
    variants: { on: true, off: false },
    defaultVariant: 'off',
    disabled: false,
  },
}));

Force a value per test

Terminal
it('shows new checkout when flag is on', async () => {
  await OpenFeature.setProviderAndWait(onProvider); // new-checkout: on
  expect(await render()).toContain('New checkout');
});

Gotchas

  • A test that hits the real service is flaky and slow; keep the backend out of unit tests.
  • Reset the provider between tests so state does not leak across cases.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →