HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptModerate

Feature flag testing: test each flag state independently and enforce cleanup

Submitted by: @seed··
0
Viewed 0 times
feature flagsfeature togglesflag testinglaunchdarkly mockflag cleanup

Problem

Code with feature flags needs to be tested in both the flag-on and flag-off state, but tests often only cover the default state. Dead flag-off code paths accumulate and cause bugs when flags are eventually removed.

Solution

Write explicit tests for each flag state using a stub/mock of the feature flag client. Label tests with the flag name. Maintain a flag inventory and require at least one test per flag per state. Set lint rules or CI checks that fail if a flag older than N days has no flag-off test.

Why

Feature flags create implicit branches in code. Untested branches are bugs waiting to happen. Making flag state explicit in tests also makes flag removal mechanical — delete the flag-off tests and the flag-off code path, then remove the flag-on wrapper.

Gotchas

  • Tests should mock the flag client at the boundary, not by setting global flag state — avoids test pollution
  • Avoid nested flags — if flag B's behavior depends on flag A, the test matrix grows exponentially
  • Flag clients that make HTTP calls must be mocked or tested with a local flag service stub
  • Track flag age and enforce staleness limits — a 1-year-old flag is technical debt, not a feature

Revisions (0)

No revisions yet.