Skip to content

How to test code that uses feature flags: test providers, offline modes and which combinations to test

How to test code behind feature flags: pass the flag client in, use your SDK's test data source or local mode instead of the flag service, test both sides of each flag rather than every combination, with test code for OpenFeature, LaunchDarkly and Split that made no network requests.

8 min read

Short answer: Don't call the real flag service in tests. Pass the flag client into your code, and in tests give it a provider with fixed values: an in-memory provider, your SDK's test data source, or a flag file. Test both sides of each flag, the configuration you expect in production, and what happens when the flag can't be evaluated; you don't need every combination.

What should a test for flagged code check?

  • Both sides of the flag. A release flag has an old path and a new one, and both run in production while the flag is rolled out, so both need tests.
  • The configuration you expect to ship, and the fall-back configuration. Pete Hodgson's article on Martin Fowler's site says "it's most important to test the toggle configuration which you expect to become live" in production, also advises testing the fall-back configuration (release toggles off), and notes that "it is not necessary to test every possible combination". It adds that many teams also run some tests with all toggles on, which works when off means the old behavior and on the new (Feature Toggles).
  • What happens when the flag can't be evaluated. If the flag is missing, or the SDK never received flags, most SDKs return the default you pass in (Split returns control, Flagsmith uses its default flag handler); test that this gives safe behavior. Once flags have loaded, most SDKs keep serving the last known values if the service goes down.
  • Targeting rules you rely on, when the logic depends on them (for example, on only for a paid plan).

Testing every combination grows as 2ⁿ for n boolean flags, faster with multivariate flags, and is rarely needed: flags that don't interact can be tested separately, and only flags that change the same code path need to be tested together.

What testing support does each SDK have?

From each vendor's documentation, checked on 15 November 2026:

Tool Testing support Network
OpenFeature In-memory provider (JavaScript, Java, Go, Python, .NET and others); domain-scoped providers to isolate tests; Go has a test provider for parallel tests; React has OpenFeatureTestProvider None in our test
LaunchDarkly TestData data source (server SDKs in .NET, Erlang, Go, Haskell, Java, Node.js, PHP, Python, Ruby, Rust; client .NET and Android); flags from a JSON or YAML file (server SDKs; YAML needs a parser in Node.js); offline mode that returns defaults None in our test with sendEvents: false and diagnosticOptOut: true; with default options it sent analytics and diagnostic events
Split / Harness FME Localhost mode: treatments from a YAML file, using localhost as the SDK key None in our test
ConfigCat Flag overrides from a map or a custom source in Node.js (files in some other SDKs), with LocalOnly, LocalOverRemote or RemoteOverLocal; setOffline() setOffline() "won't initiate HTTP requests" per the docs; LocalOnly also prevents them, per the SDK source
Flagsmith Offline mode with a local file handler (from flagsmith eval), and a default flag handler None in offline mode, per the docs
Statsig Local overrides of gates, configs, experiments and layers (overrideGate and others) in the Node.js server core SDK initialize() makes a network request; an offline option isn't documented
Unleash Bootstrap from inline data, a URL or a file; a custom repository to run offline Polls and sends metrics by default; refreshInterval: 0 and disableMetrics: true turn them off (from the Node.js SDK source)
flagd Providers read flags from a local file (offlineFlagSourcePath in Node.js) None
GO Feature Flag The Go module and the relay proxy read flags from a local file; the Node.js provider needs a relay proxy, which can run locally Local only

Sources: OpenFeature Go, Java, Python, .NET and React; LaunchDarkly test data sources, flags from files, offline mode and testing guide; Harness FME Node.js SDK, localhost mode and unit test guidance; flagd Node.js provider; GO Feature Flag Node.js provider; ConfigCat Node.js flag overrides; Flagsmith server-side SDKs; Statsig Node.js server core; Unleash Node.js.

LaunchDarkly's offline mode only returns your defaults, so it suits tests of the fallback path, not of the flagged path. Analytics and diagnostic events are separate settings in most SDKs: with LaunchDarkly's TestData and default options, our control run sent requests to events.launchdarkly.com for both. Turn them off in tests.

Because the code receives the client, a plain mock or fake object also works for unit tests; LaunchDarkly's guide recommends mocks there. Test providers are worth it when you want the SDK's real evaluation, defaults and error codes.

How do I test with OpenFeature?

Write the code so it receives the flag client:

// checkout.js
export async function checkoutPage(client, user) {
  const newCheckout = await client.getBooleanValue('release_new_checkout', false, {
    targetingKey: user.id,
    plan: user.plan,
  })
  return newCheckout ? 'New checkout' : 'Old checkout'
}

Then give it an in-memory provider in the test, once per side of the flag, and once with the flag missing:

import { test, beforeEach } from 'node:test'
import assert from 'node:assert/strict'
import { OpenFeature, InMemoryProvider } from '@openfeature/server-sdk'
import { checkoutPage } from './checkout.js'

const flag = (variant) => ({
  release_new_checkout: { variants: { on: true, off: false }, defaultVariant: variant, disabled: false },
})

beforeEach(async () => {
  await OpenFeature.clearProviders()
})

for (const [variant, expected] of [['on', 'New checkout'], ['off', 'Old checkout']]) {
  test(`checkout with release_new_checkout ${variant}`, async () => {
    await OpenFeature.setProviderAndWait(new InMemoryProvider(flag(variant)))
    assert.equal(await checkoutPage(OpenFeature.getClient(), { id: 'u1', plan: 'pro' }), expected)
  })
}

test('flag missing from the provider falls back to the default', async () => {
  await OpenFeature.setProviderAndWait(new InMemoryProvider({}))
  const client = OpenFeature.getClient()
  assert.equal(await checkoutPage(client, { id: 'u1', plan: 'pro' }), 'Old checkout')
  const details = await client.getBooleanDetails('release_new_checkout', false)
  assert.equal(details.errorCode, 'FLAG_NOT_FOUND')
})

A contextEvaluator on the in-memory flag tests targeting too: our full test file checks that the flag is on for plan: 'pro' and off for plan: 'free'.

OpenFeature is a global. node --test, Jest and Vitest run each test file in its own process or worker, and tests inside a file one after another by default, so resetting with clearProviders() (which shuts down and removes providers, but not hooks, handlers or the global context) is enough. If tests run concurrently in one process, give each its own domain: OpenFeature.setProviderAndWait('checkout-test', provider) and OpenFeature.getClient('checkout-test'); our test shows a domain provider returning on while the default provider returns off. In Go, the SDK's test provider does this for parallel tests.

How do I test with LaunchDarkly's TestData or a flag file?

LaunchDarkly's TestData replaces the connection to LaunchDarkly, and you change flags from the test:

import { test, after } from 'node:test'
import assert from 'node:assert/strict'
import { init, integrations } from '@launchdarkly/node-server-sdk'

const td = new integrations.TestData()
const client = init('sdk-key-not-used', { updateProcessor: td.getFactory(), sendEvents: false, diagnosticOptOut: true })
after(() => client.close())

const user = { kind: 'user', key: 'u1', plan: 'pro' }

test('TestData: flag on for everyone, then off', async () => {
  await client.waitForInitialization({ timeout: 5 })
  td.update(td.flag('release_new_checkout').booleanFlag().variationForAll(true))
  assert.equal(await client.variation('release_new_checkout', user, false), true)
  td.update(td.flag('release_new_checkout').booleanFlag().variationForAll(false))
  assert.equal(await client.variation('release_new_checkout', user, false), false)
})

Per-user values work the same way (variationForUser('u1', true)). LaunchDarkly's testing guide calls reading flags from a file its recommended approach for testing without connecting to LaunchDarkly (server-side SDKs only); with integrations.FileDataSourceFactory and { "flagValues": { "release_new_checkout": true } }, the flag evaluated to true in our test (test file).

How do I test with Split's localhost mode?

Split's Node.js SDK reads treatments from a YAML file when the SDK key is localhost:

# split.yaml
- release_new_checkout:
    treatment: "on"
    keys: "u1"
- release_new_checkout:
    treatment: "off"
import { test, after } from 'node:test'
import assert from 'node:assert/strict'
import { fileURLToPath } from 'node:url'
import { SplitFactory } from '@splitsoftware/splitio'

const features = fileURLToPath(new URL('./split.yaml', import.meta.url))
const client = SplitFactory({ core: { authorizationKey: 'localhost' }, features }).client()
after(() => client.destroy())

test('localhost mode: treatment per user from a YAML file', async () => {
  await client.ready()
  assert.equal(client.getTreatment('u1', 'release_new_checkout'), 'on')
  assert.equal(client.getTreatment('u2', 'release_new_checkout'), 'off')
  assert.equal(client.getTreatment('u1', 'missing_flag'), 'control')
})

Call destroy() when done: in localhost mode the SDK re-reads the file every 15 seconds by default (scheduler.offlineRefreshRate), and that timer keeps the test process alive. A flag missing from the file returns control, the same treatment as when Split can't evaluate a flag, so the test of your fallback path is the same.

Did these tests really need no flag service?

Passing with the network off doesn't prove much on its own, because SDKs swallow failed requests. So we ran all nine tests (OpenFeature 1.23.0, LaunchDarkly Node.js SDK 9.16.5, Split 11.12.1) with Node.js 22 in a Docker container started with --network none, with a preload script that logs every socket, DNS, HTTP and fetch call. All nine passed with zero network attempts. As a control, LaunchDarkly's TestData with default options logged 8 attempts, to events.launchdarkly.com/bulk and /diagnostic. The test files, runner, control and output are published.

What about integration and end-to-end tests?

  • Integration tests: run the real SDK against a flag file (flagd, GO Feature Flag, LaunchDarkly flag files, Split localhost mode) checked into the repository, so the same flag configuration is used on every run.
  • End-to-end tests: point the app at a test environment in your flag tool, or let the test choose the flag through a mechanism only test builds have; never through something a user could set in production.
  • Flags that change mid-test: LaunchDarkly's TestData (td.update) and OpenFeature's in-memory provider (putConfiguration, which emits a configuration-changed event) let you test that your code reacts to a flag flipping, for example a kill switch.
  • Typos in flag keys: a misspelled key silently returns the default. A test that compares the keys used in code with an export of your flag tool's configuration catches them; the naming guide has a script that finds keys in code.
  • Startup and timeouts: test what happens when the SDK isn't ready yet (waitForInitialization, ready()) and how long your app waits.

When a flag is removed, delete its tests together with the old path (see stale feature flags), and keep a test per flag type for the configuration you ship; naming conventions that include the flag type make those easy to find. For React components, see feature flags in React. All tools are in the directory.