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
How to Test Both Flag States in CI With a MatrixRun your CI test suite twice, once with a feature flag on and once off, using a GitHub Actions matrix so both…
How to Initialize a Flag SDK in a CI Test EnvironmentInitialize a feature flag SDK once in CI test setup and wait for it to be ready, so tests do not race the SDK…