[{"data":1,"prerenderedAt":1095},["ShallowReactive",2],{"blog-post-\u002Fblog\u002Fhow-to-test-code-that-uses-feature-flags":3},{"id":4,"title":5,"author":6,"body":7,"date":1083,"description":1084,"draft":1085,"extension":1086,"image":1087,"meta":1088,"navigation":129,"path":1089,"seo":1090,"sitemap":1091,"stem":1093,"__hash__":1094},"blog\u002Fblog\u002Fhow-to-test-code-that-uses-feature-flags.md","How to test code that uses feature flags","Alejandro Beiderman",{"type":8,"value":9,"toc":1067},"minimark",[10,14,17,22,25,28,31,35,40,43,75,78,82,85,88,255,258,523,530,541,545,557,737,740,743,747,750,756,886,892,938,944,950,954,957,961,964,968,971,974,995,999,1006,1017,1031,1035,1038,1042,1063],[11,12,13],"p",{},"A feature flag is a branch in the code that evaluates at runtime based on a value that is served from a feature flags provider service. The fact that the flag value is controlled from an external service is what makes it powerful but also makes it awkward to test. We want to test both paths, and not only that, we want the tests to be able to control the state of the flag as part of their setup and assert on the expected behavior for each state of the flag or configuration value.",[11,15,16],{},"This post is about the general problem of testing feature flags, regardless of the flag service or SDK you use. The examples use TypeScript and the ConfigDirector server SDK, but the approaches have equivalents in other languages and services, and the common mistakes are usually the same everywhere as well.",[18,19,21],"h2",{"id":20},"unit-tests-and-functional-tests-need-different-things","Unit tests and functional tests need different things",[11,23,24],{},"A unit test treats the flag as an input. The test decides the value, calls the code, and asserts on the result. Connecting to the flag service would take that decision away from the test since the value would come from whatever an environment currently serves, which someone can change between two runs, and the test would no longer be valid. The connection would also add latency and a dependency on connectivity to a suite that runs regularly on a workstation and in every CI job. So for unit tests the question is how to put the value under the test's control while keeping as much of the real SDK in the loop as possible.",[11,26,27],{},"A functional test exercises the application at runtime: a browser against a server, an HTTP client against an API, a smoke test after a deploy. Here the flag service can legitimately stay in the loop. The application starts exactly as it does in production, with the same connection path and the values of a real environment, so the test also covers the wiring between the application and the service. What that costs is control. The suite can only observe what the environment serves at that moment, a test that needs a flag in a given state has to set it first through the service's API, and every run shares that environment with every other run. Pinning the flags to known values gives the control back and gives up the wiring. Both are reasonable positions, and many teams hold both at once in different suites.",[11,29,30],{},"The rest of this post looks at approaches for the two kinds of tests separately.",[18,32,34],{"id":33},"unit-tests","Unit tests",[36,37,39],"h3",{"id":38},"where-a-test-lands-without-any-setup","Where a test lands without any setup",[11,41,42],{},"Code that reads a flag through an SDK, tested with no attention to the flag, ends up in one of three places.",[44,45,46,54,60],"ol",{},[47,48,49,53],"li",{},[50,51,52],"strong",{},"The SDK connects."," The test opens a connection to the flag service, waits for a response, and gets the values of whatever environment the SDK key points to. If the connection fails, the SDK serves the in-code default value instead. The result depends on the network and on the dashboard, and the test cannot choose which branch it runs.",[47,55,56,59],{},[50,57,58],{},"The SDK is never initialized."," Every read returns the in-code default value you wrote in the call. Every test passes, and every test exercises the same branch. Nothing in the suite reports that the other branch was never executed.",[47,61,62,65,66,70,71,74],{},[50,63,64],{},"The SDK module is mocked."," ",[67,68,69],"code",{},"jest.mock(\"@configdirector\u002Fserver-sdk\")"," with a ",[67,72,73],{},"getValue"," that returns whatever the test says. The tests are fast, need no network and can reach both branches. What they no longer exercise is the SDK itself: how it converts types, what it returns for a key that does not exist, what a read sees before the client is ready. Each of those happens in production and none of them can happen in the test.",[11,76,77],{},"These are three points on one trade-off, between how much of the real SDK is in the loop and how much the test controls. Mocking maximizes control. Connecting maximizes fidelity. The two approaches below keep most of the fidelity and all of the control.",[36,79,81],{"id":80},"pass-the-client-in-and-use-a-test-client","Pass the client in and use a test client",[11,83,84],{},"Most flag SDKs ship, or can be given, a client that serves values from memory. The ConfigDirector server SDK has a testing entry point that creates one. It is the real SDK client, connected to an in-memory server that the test controls. Only the connection is replaced. Everything that happens after a value arrives, including type checks, readiness and watchers, is the same code that runs in production.",[11,86,87],{},"The one thing the application code has to do is accept the client rather than create it:",[89,90,96],"pre",{"className":91,"code":92,"filename":93,"language":94,"meta":95,"style":95},"language-ts shiki shiki-themes github-light-default github-light-default github-dark-default","import type { ConfigDirectorClient } from \"@configdirector\u002Fserver-sdk\";\n\nexport class CheckoutService {\n  constructor(private readonly configs: ConfigDirectorClient) {}\n\n  isNewCheckoutEnabled(user: { id: string }): boolean {\n    return this.configs.getValue(\"new-checkout\", false, { id: user.id });\n  }\n}\n","checkout-service.ts","ts","",[67,97,98,124,131,147,174,179,215,243,249],{"__ignoreMap":95},[99,100,103,107,110,114,117,121],"span",{"class":101,"line":102},"line",1,[99,104,106],{"class":105},"s5nWI","import",[99,108,109],{"class":105}," type",[99,111,113],{"class":112},"s_bOF"," { ConfigDirectorClient } ",[99,115,116],{"class":105},"from",[99,118,120],{"class":119},"s-BGO"," \"@configdirector\u002Fserver-sdk\"",[99,122,123],{"class":112},";\n",[99,125,127],{"class":101,"line":126},2,[99,128,130],{"emptyLinePlaceholder":129},true,"\n",[99,132,134,137,140,144],{"class":101,"line":133},3,[99,135,136],{"class":105},"export",[99,138,139],{"class":105}," class",[99,141,143],{"class":142},"sQJ0q"," CheckoutService",[99,145,146],{"class":112}," {\n",[99,148,150,153,156,159,162,165,168,171],{"class":101,"line":149},4,[99,151,152],{"class":105},"  constructor",[99,154,155],{"class":112},"(",[99,157,158],{"class":105},"private",[99,160,161],{"class":105}," readonly",[99,163,164],{"class":142}," configs",[99,166,167],{"class":105},":",[99,169,170],{"class":142}," ConfigDirectorClient",[99,172,173],{"class":112},") {}\n",[99,175,177],{"class":101,"line":176},5,[99,178,130],{"emptyLinePlaceholder":129},[99,180,182,186,188,191,193,196,199,201,205,208,210,213],{"class":101,"line":181},6,[99,183,185],{"class":184},"sd69k","  isNewCheckoutEnabled",[99,187,155],{"class":112},[99,189,190],{"class":142},"user",[99,192,167],{"class":105},[99,194,195],{"class":112}," { ",[99,197,198],{"class":142},"id",[99,200,167],{"class":105},[99,202,204],{"class":203},"sDkKp"," string",[99,206,207],{"class":112}," })",[99,209,167],{"class":105},[99,211,212],{"class":203}," boolean",[99,214,146],{"class":112},[99,216,218,221,224,227,229,231,234,237,240],{"class":101,"line":217},7,[99,219,220],{"class":105},"    return",[99,222,223],{"class":203}," this",[99,225,226],{"class":112},".configs.",[99,228,73],{"class":184},[99,230,155],{"class":112},[99,232,233],{"class":119},"\"new-checkout\"",[99,235,236],{"class":112},", ",[99,238,239],{"class":203},"false",[99,241,242],{"class":112},", { id: user.id });\n",[99,244,246],{"class":101,"line":245},8,[99,247,248],{"class":112},"  }\n",[99,250,252],{"class":101,"line":251},9,[99,253,254],{"class":112},"}\n",[11,256,257],{},"Each branch then gets its own test, with the value set in the test:",[89,259,262],{"className":91,"code":260,"filename":261,"language":94,"meta":95,"style":95},"import { createTestClient } from \"@configdirector\u002Fserver-sdk\u002Ftesting\";\nimport { CheckoutService } from \".\u002Fcheckout-service\";\n\ntest(\"shows the new checkout when the flag is on\", async () => {\n  const testClient = createTestClient({ values: { \"new-checkout\": true } });\n  await testClient.client.initialize();\n  const service = new CheckoutService(testClient.client);\n\n  expect(service.isNewCheckoutEnabled({ id: \"user-123\" })).toBe(true);\n});\n\ntest(\"shows the old checkout when the flag is off\", async () => {\n  const testClient = createTestClient({ values: { \"new-checkout\": false } });\n  await testClient.client.initialize();\n  const service = new CheckoutService(testClient.client);\n\n  expect(service.isNewCheckoutEnabled({ id: \"user-123\" })).toBe(false);\n});\n","checkout-service.test.ts",[67,263,264,278,292,296,319,347,361,378,382,412,418,423,443,464,475,490,495,518],{"__ignoreMap":95},[99,265,266,268,271,273,276],{"class":101,"line":102},[99,267,106],{"class":105},[99,269,270],{"class":112}," { createTestClient } ",[99,272,116],{"class":105},[99,274,275],{"class":119}," \"@configdirector\u002Fserver-sdk\u002Ftesting\"",[99,277,123],{"class":112},[99,279,280,282,285,287,290],{"class":101,"line":126},[99,281,106],{"class":105},[99,283,284],{"class":112}," { CheckoutService } ",[99,286,116],{"class":105},[99,288,289],{"class":119}," \".\u002Fcheckout-service\"",[99,291,123],{"class":112},[99,293,294],{"class":101,"line":133},[99,295,130],{"emptyLinePlaceholder":129},[99,297,298,301,303,306,308,311,314,317],{"class":101,"line":149},[99,299,300],{"class":184},"test",[99,302,155],{"class":112},[99,304,305],{"class":119},"\"shows the new checkout when the flag is on\"",[99,307,236],{"class":112},[99,309,310],{"class":105},"async",[99,312,313],{"class":112}," () ",[99,315,316],{"class":105},"=>",[99,318,146],{"class":112},[99,320,321,324,327,330,333,336,338,341,344],{"class":101,"line":176},[99,322,323],{"class":105},"  const",[99,325,326],{"class":203}," testClient",[99,328,329],{"class":105}," =",[99,331,332],{"class":184}," createTestClient",[99,334,335],{"class":112},"({ values: { ",[99,337,233],{"class":119},[99,339,340],{"class":112},": ",[99,342,343],{"class":203},"true",[99,345,346],{"class":112}," } });\n",[99,348,349,352,355,358],{"class":101,"line":181},[99,350,351],{"class":105},"  await",[99,353,354],{"class":112}," testClient.client.",[99,356,357],{"class":184},"initialize",[99,359,360],{"class":112},"();\n",[99,362,363,365,368,370,373,375],{"class":101,"line":217},[99,364,323],{"class":105},[99,366,367],{"class":203}," service",[99,369,329],{"class":105},[99,371,372],{"class":105}," new",[99,374,143],{"class":184},[99,376,377],{"class":112},"(testClient.client);\n",[99,379,380],{"class":101,"line":245},[99,381,130],{"emptyLinePlaceholder":129},[99,383,384,387,390,393,396,399,402,405,407,409],{"class":101,"line":251},[99,385,386],{"class":184},"  expect",[99,388,389],{"class":112},"(service.",[99,391,392],{"class":184},"isNewCheckoutEnabled",[99,394,395],{"class":112},"({ id: ",[99,397,398],{"class":119},"\"user-123\"",[99,400,401],{"class":112}," })).",[99,403,404],{"class":184},"toBe",[99,406,155],{"class":112},[99,408,343],{"class":203},[99,410,411],{"class":112},");\n",[99,413,415],{"class":101,"line":414},10,[99,416,417],{"class":112},"});\n",[99,419,421],{"class":101,"line":420},11,[99,422,130],{"emptyLinePlaceholder":129},[99,424,426,428,430,433,435,437,439,441],{"class":101,"line":425},12,[99,427,300],{"class":184},[99,429,155],{"class":112},[99,431,432],{"class":119},"\"shows the old checkout when the flag is off\"",[99,434,236],{"class":112},[99,436,310],{"class":105},[99,438,313],{"class":112},[99,440,316],{"class":105},[99,442,146],{"class":112},[99,444,446,448,450,452,454,456,458,460,462],{"class":101,"line":445},13,[99,447,323],{"class":105},[99,449,326],{"class":203},[99,451,329],{"class":105},[99,453,332],{"class":184},[99,455,335],{"class":112},[99,457,233],{"class":119},[99,459,340],{"class":112},[99,461,239],{"class":203},[99,463,346],{"class":112},[99,465,467,469,471,473],{"class":101,"line":466},14,[99,468,351],{"class":105},[99,470,354],{"class":112},[99,472,357],{"class":184},[99,474,360],{"class":112},[99,476,478,480,482,484,486,488],{"class":101,"line":477},15,[99,479,323],{"class":105},[99,481,367],{"class":203},[99,483,329],{"class":105},[99,485,372],{"class":105},[99,487,143],{"class":184},[99,489,377],{"class":112},[99,491,493],{"class":101,"line":492},16,[99,494,130],{"emptyLinePlaceholder":129},[99,496,498,500,502,504,506,508,510,512,514,516],{"class":101,"line":497},17,[99,499,386],{"class":184},[99,501,389],{"class":112},[99,503,392],{"class":184},[99,505,395],{"class":112},[99,507,398],{"class":119},[99,509,401],{"class":112},[99,511,404],{"class":184},[99,513,155],{"class":112},[99,515,239],{"class":203},[99,517,411],{"class":112},[99,519,521],{"class":101,"line":520},18,[99,522,417],{"class":112},[11,524,525,526,529],{},"Two tests with two names and two explicit values. A loop over ",[67,527,528],{},"[true, false]"," inside one test would be shorter, and its failure message would not say which branch broke. Two named tests cost two more lines and pay for them the first time one fails.",[11,531,532,533,536,537,540],{},"A lot of code creates the client when a module loads, in a ",[67,534,535],{},"configs.ts"," that exports a ready-made client. That code can still be tested this way: swap the module's export for the test client's ",[67,538,539],{},"client"," property with your test runner's module mocking. The difference from mocking the SDK is what gets swapped in. It is a real client with an in-memory connection, not a hand-written stub.",[36,542,544],{"id":543},"openfeature-swap-the-provider","OpenFeature: swap the provider",[11,546,547,548,556],{},"If the code reads flags through ",[549,550,555],"a",{"href":551,"rel":552,"target":554},"https:\u002F\u002Fopenfeature.dev",[553],"noopener","_blank","OpenFeature",", the seam already exists. OpenFeature separates the API the code calls from the provider that supplies the values, and its SDKs ship an in-memory provider. The production code does not change for the test:",[89,558,561],{"className":91,"code":559,"filename":560,"language":94,"meta":95,"style":95},"import { OpenFeature, TypedInMemoryProvider } from \"@openfeature\u002Fserver-sdk\";\n\nafterEach(() => OpenFeature.close());\n\ntest(\"shows the new checkout when the flag is on\", async () => {\n  await OpenFeature.setProviderAndWait(\n    new TypedInMemoryProvider({\n      \"new-checkout\": { variants: { on: true, off: false }, defaultVariant: \"on\" },\n    }),\n  );\n  const client = OpenFeature.getClient();\n\n  expect(await client.getBooleanValue(\"new-checkout\", false)).toBe(true);\n});\n","checkout.test.ts",[67,562,563,577,581,600,604,622,634,645,669,674,679,695,699,733],{"__ignoreMap":95},[99,564,565,567,570,572,575],{"class":101,"line":102},[99,566,106],{"class":105},[99,568,569],{"class":112}," { OpenFeature, TypedInMemoryProvider } ",[99,571,116],{"class":105},[99,573,574],{"class":119}," \"@openfeature\u002Fserver-sdk\"",[99,576,123],{"class":112},[99,578,579],{"class":101,"line":126},[99,580,130],{"emptyLinePlaceholder":129},[99,582,583,586,589,591,594,597],{"class":101,"line":133},[99,584,585],{"class":184},"afterEach",[99,587,588],{"class":112},"(() ",[99,590,316],{"class":105},[99,592,593],{"class":112}," OpenFeature.",[99,595,596],{"class":184},"close",[99,598,599],{"class":112},"());\n",[99,601,602],{"class":101,"line":149},[99,603,130],{"emptyLinePlaceholder":129},[99,605,606,608,610,612,614,616,618,620],{"class":101,"line":176},[99,607,300],{"class":184},[99,609,155],{"class":112},[99,611,305],{"class":119},[99,613,236],{"class":112},[99,615,310],{"class":105},[99,617,313],{"class":112},[99,619,316],{"class":105},[99,621,146],{"class":112},[99,623,624,626,628,631],{"class":101,"line":181},[99,625,351],{"class":105},[99,627,593],{"class":112},[99,629,630],{"class":184},"setProviderAndWait",[99,632,633],{"class":112},"(\n",[99,635,636,639,642],{"class":101,"line":217},[99,637,638],{"class":105},"    new",[99,640,641],{"class":184}," TypedInMemoryProvider",[99,643,644],{"class":112},"({\n",[99,646,647,650,653,655,658,660,663,666],{"class":101,"line":245},[99,648,649],{"class":119},"      \"new-checkout\"",[99,651,652],{"class":112},": { variants: { on: ",[99,654,343],{"class":203},[99,656,657],{"class":112},", off: ",[99,659,239],{"class":203},[99,661,662],{"class":112}," }, defaultVariant: ",[99,664,665],{"class":119},"\"on\"",[99,667,668],{"class":112}," },\n",[99,670,671],{"class":101,"line":251},[99,672,673],{"class":112},"    }),\n",[99,675,676],{"class":101,"line":414},[99,677,678],{"class":112},"  );\n",[99,680,681,683,686,688,690,693],{"class":101,"line":420},[99,682,323],{"class":105},[99,684,685],{"class":203}," client",[99,687,329],{"class":105},[99,689,593],{"class":112},[99,691,692],{"class":184},"getClient",[99,694,360],{"class":112},[99,696,697],{"class":101,"line":425},[99,698,130],{"emptyLinePlaceholder":129},[99,700,701,703,705,708,711,714,716,718,720,722,725,727,729,731],{"class":101,"line":445},[99,702,386],{"class":184},[99,704,155],{"class":112},[99,706,707],{"class":105},"await",[99,709,710],{"class":112}," client.",[99,712,713],{"class":184},"getBooleanValue",[99,715,155],{"class":112},[99,717,233],{"class":119},[99,719,236],{"class":112},[99,721,239],{"class":203},[99,723,724],{"class":112},")).",[99,726,404],{"class":184},[99,728,155],{"class":112},[99,730,343],{"class":203},[99,732,411],{"class":112},[99,734,735],{"class":101,"line":466},[99,736,417],{"class":112},[11,738,739],{},"The in-memory provider also accepts a function from the evaluation context to a variant name, so a test of code that treats some users differently can serve one value to one user and another to the rest.",[11,741,742],{},"The trade-off is that the in-memory provider is OpenFeature's code, not your flag service's. It covers how your code reacts to a value. It does not cover how your service's provider behaves when it cannot connect or when a flag has another type than the code expects. The provider is responsible for testing that, and a suite that wants it anyway can add one test with the real provider's own test client.",[36,744,746],{"id":745},"cases-beyond-the-two-branches","Cases beyond the two branches",[11,748,749],{},"Once both branches of a flag are covered, a few more situations are each worth one test, because they are where production differs from the path a test usually takes.",[11,751,752,755],{},[50,753,754],{},"The client is not ready."," A request arrives before the SDK has connected, or the flag service is unreachable at startup, and the code sees the in-code default value. Whether that is the behavior you want depends on the feature. For a new checkout, serving the old one is likely fine. For a kill switch whose in-code default value is \"on\", it may not be. A test client that can fail initialization lets the test say what should happen:",[89,757,759],{"className":91,"code":758,"language":94,"meta":95,"style":95},"test(\"serves the old checkout while the client cannot connect\", async () => {\n  const testClient = createTestClient({ values: { \"new-checkout\": true }, timeout: 500 });\n  testClient.failInitialization();\n  await testClient.client.initialize();\n  const service = new CheckoutService(testClient.client);\n\n  expect(testClient.client.isReady).toBe(false);\n  expect(service.isNewCheckoutEnabled({ id: \"user-123\" })).toBe(false);\n});\n",[67,760,761,780,807,817,827,841,845,860,882],{"__ignoreMap":95},[99,762,763,765,767,770,772,774,776,778],{"class":101,"line":102},[99,764,300],{"class":184},[99,766,155],{"class":112},[99,768,769],{"class":119},"\"serves the old checkout while the client cannot connect\"",[99,771,236],{"class":112},[99,773,310],{"class":105},[99,775,313],{"class":112},[99,777,316],{"class":105},[99,779,146],{"class":112},[99,781,782,784,786,788,790,792,794,796,798,801,804],{"class":101,"line":126},[99,783,323],{"class":105},[99,785,326],{"class":203},[99,787,329],{"class":105},[99,789,332],{"class":184},[99,791,335],{"class":112},[99,793,233],{"class":119},[99,795,340],{"class":112},[99,797,343],{"class":203},[99,799,800],{"class":112}," }, timeout: ",[99,802,803],{"class":203},"500",[99,805,806],{"class":112}," });\n",[99,808,809,812,815],{"class":101,"line":133},[99,810,811],{"class":112},"  testClient.",[99,813,814],{"class":184},"failInitialization",[99,816,360],{"class":112},[99,818,819,821,823,825],{"class":101,"line":149},[99,820,351],{"class":105},[99,822,354],{"class":112},[99,824,357],{"class":184},[99,826,360],{"class":112},[99,828,829,831,833,835,837,839],{"class":101,"line":176},[99,830,323],{"class":105},[99,832,367],{"class":203},[99,834,329],{"class":105},[99,836,372],{"class":105},[99,838,143],{"class":184},[99,840,377],{"class":112},[99,842,843],{"class":101,"line":181},[99,844,130],{"emptyLinePlaceholder":129},[99,846,847,849,852,854,856,858],{"class":101,"line":217},[99,848,386],{"class":184},[99,850,851],{"class":112},"(testClient.client.isReady).",[99,853,404],{"class":184},[99,855,155],{"class":112},[99,857,239],{"class":203},[99,859,411],{"class":112},[99,861,862,864,866,868,870,872,874,876,878,880],{"class":101,"line":245},[99,863,386],{"class":184},[99,865,389],{"class":112},[99,867,392],{"class":184},[99,869,395],{"class":112},[99,871,398],{"class":119},[99,873,401],{"class":112},[99,875,404],{"class":184},[99,877,155],{"class":112},[99,879,239],{"class":203},[99,881,411],{"class":112},[99,883,884],{"class":101,"line":251},[99,885,417],{"class":112},[11,887,888,891],{},[50,889,890],{},"The value changes while the application runs."," Flags are not read once. If the code caches a value, subscribes to changes, or rebuilds something expensive when a value flips, test the flip. With a test client that is one call:",[89,893,895],{"className":91,"code":894,"language":94,"meta":95,"style":95},"testClient.setValue(\"new-checkout\", false);\nexpect(service.isNewCheckoutEnabled({ id: \"user-123\" })).toBe(false);\n",[67,896,897,915],{"__ignoreMap":95},[99,898,899,902,905,907,909,911,913],{"class":101,"line":102},[99,900,901],{"class":112},"testClient.",[99,903,904],{"class":184},"setValue",[99,906,155],{"class":112},[99,908,233],{"class":119},[99,910,236],{"class":112},[99,912,239],{"class":203},[99,914,411],{"class":112},[99,916,917,920,922,924,926,928,930,932,934,936],{"class":101,"line":126},[99,918,919],{"class":184},"expect",[99,921,389],{"class":112},[99,923,392],{"class":184},[99,925,395],{"class":112},[99,927,398],{"class":119},[99,929,401],{"class":112},[99,931,404],{"class":184},[99,933,155],{"class":112},[99,935,239],{"class":203},[99,937,411],{"class":112},[11,939,940,943],{},[50,941,942],{},"The flag is removed."," Eventually the flag is cleaned up in the service, and sometimes that happens before the code stops reading it. A test that removes the value and asserts on what the code does next describes that situation before it occurs in production.",[11,945,946,949],{},[50,947,948],{},"The type does not match."," A flag read as a string that is defined as a boolean. Most SDKs return the in-code default value with a type-mismatch reason. A test client that preserves that behavior lets the test assert it. A hand-written mock has no such behavior to preserve.",[36,951,953],{"id":952},"targeting-rules","Targeting rules",[11,955,956],{},"A rule like \"users in Germany see the new checkout\" is configuration that lives in the service, and the test client serves one value to every context. A unit test can state \"given this value, this happens\" for each value, but it cannot state which users receive which value, because that decision is not in the code. Checking the rule itself is a job for the service's own tooling or for a functional test against a real environment, which is one of the reasons to keep a connected functional suite.",[18,958,960],{"id":959},"functional-tests","Functional tests",[11,962,963],{},"A functional suite starts the whole application, so it has to make one decision for every flag at once: where do the values come from. There are two answers.",[36,965,967],{"id":966},"connected-to-the-flag-service","Connected to the flag service",[11,969,970],{},"The application starts as it does in production and reads its values from a real environment. This is the only kind of test that covers the SDK key, the network path, the connection mode and the service's actual response, and it is the only automated way to exercise targeting rules. A smoke test after a deploy is the typical case: the question it answers is \"does this deployment talk to the flag service and serve what the environment says\", and no pinned value can answer it.",[11,972,973],{},"The trade-offs are the ones that come with shared state:",[975,976,977,983,989],"ul",{},[47,978,979,982],{},[50,980,981],{},"The suite needs an environment of its own."," If it shares one with people, a flag someone changes mid-run changes the test's result. Most services make environments cheap enough to give each suite one, or one per branch.",[47,984,985,988],{},[50,986,987],{},"A test that needs a flag in a given state sets it first."," That means a call to the service's API or CLI before the test, a wait for the change to reach the application, and a reset afterwards. Two runs of the suite against the same environment cannot overlap.",[47,990,991,994],{},[50,992,993],{},"The service sees the suite as usage."," Evaluations are counted, and anything the service derives from usage, such as a flag's lifecycle state or an alert on evaluation volume, includes the test traffic. A dedicated environment keeps that separate from production's numbers.",[36,996,998],{"id":997},"every-flag-pinned","Every flag pinned",[11,1000,1001,1002,1005],{},"The application starts with every flag at a value the suite chose and opens no connection. Most SDKs have a mode for this: the client reads a file or an in-memory map and serves those values to every context. In the ConfigDirector SDKs it is the ",[67,1003,1004],{},"offline"," connection mode, which takes a values map or the path of a JSON file. The application starts the same way it does in production, except that the values come from a file the suite owns.",[11,1007,1008,1009,1012,1013,1016],{},"What this buys is a deterministic run and a matrix. One suite, run once with ",[67,1010,1011],{},"new-checkout-on.json"," and once with ",[67,1014,1015],{},"new-checkout-off.json",", covers both branches end to end, with no dashboard involved and no two runs contending for one environment. What it gives up is everything the connected suite covers: the key, the connection, and the rules. Two more things follow from pinning:",[975,1018,1019,1025],{},[47,1020,1021,1024],{},[50,1022,1023],{},"Targeting rules are not applied."," Every user receives the same value. A test of \"user A sees the new checkout and user B does not\" is a unit test with a value per test, or a connected test, not a pinned one.",[47,1026,1027,1030],{},[50,1028,1029],{},"A file that does not load has to stop the run."," If the SDK logs a warning and continues on in-code default values, the suite passes with every flag at its fallback and nothing reports it. The ConfigDirector SDKs refuse to start on a missing or invalid file. If yours does not, add an assertion at startup that one known flag has the value the file says.",[36,1032,1034],{"id":1033},"holding-both","Holding both",[11,1036,1037],{},"The two suites answer different questions, so there is no reason to pick only one. A common arrangement is a pinned matrix in CI on every pull request, where speed and determinism matter and the wiring has not changed, and a connected smoke test against a dedicated environment after each deploy, where the wiring is exactly what changed. How much of each a team wants depends on how often its deployments break at the connection versus in the code behind the flag.",[18,1039,1041],{"id":1040},"in-configdirector","In ConfigDirector",[11,1043,1044,1045,1050,1051,1056,1057,1062],{},"Every ConfigDirector SDK ships a test client that is the real client with its connection replaced, and an offline mode that pins values for a functional suite. The ",[549,1046,1049],{"href":1047,"rel":1048,"target":554},"https:\u002F\u002Fdocs.configdirector.com\u002Fsdks\u002Fserver\u002Fnode-js#test-your-code",[553],"Node.js SDK page"," shows the test client, its controls for holding and failing initialization, and what to expect from watchers under test, and its ",[549,1052,1055],{"href":1053,"rel":1054,"target":554},"https:\u002F\u002Fdocs.configdirector.com\u002Fsdks\u002Fserver\u002Fnode-js#run-without-a-connection",[553],"offline mode section"," shows the values file. The ",[549,1058,1061],{"href":1059,"rel":1060,"target":554},"https:\u002F\u002Fdocs.configdirector.com\u002Fsdks\u002Fopenfeature",[553],"OpenFeature pages"," show the in-memory provider for each language.",[1064,1065,1066],"style",{},"html pre.shiki code .s5nWI, html code.shiki .s5nWI{--shiki-light:#CF222E;--shiki-default:#CF222E;--shiki-dark:#FF7B72}html pre.shiki code .s_bOF, html code.shiki .s_bOF{--shiki-light:#1F2328;--shiki-default:#1F2328;--shiki-dark:#E6EDF3}html pre.shiki code .s-BGO, html code.shiki .s-BGO{--shiki-light:#0A3069;--shiki-default:#0A3069;--shiki-dark:#A5D6FF}html pre.shiki code .sQJ0q, html code.shiki .sQJ0q{--shiki-light:#953800;--shiki-default:#953800;--shiki-dark:#FFA657}html pre.shiki code .sd69k, html code.shiki .sd69k{--shiki-light:#8250DF;--shiki-default:#8250DF;--shiki-dark:#D2A8FF}html pre.shiki code .sDkKp, html code.shiki .sDkKp{--shiki-light:#0550AE;--shiki-default:#0550AE;--shiki-dark:#79C0FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":95,"searchDepth":126,"depth":126,"links":1068},[1069,1070,1077,1082],{"id":20,"depth":126,"text":21},{"id":33,"depth":126,"text":34,"children":1071},[1072,1073,1074,1075,1076],{"id":38,"depth":133,"text":39},{"id":80,"depth":133,"text":81},{"id":543,"depth":133,"text":544},{"id":745,"depth":133,"text":746},{"id":952,"depth":133,"text":953},{"id":959,"depth":126,"text":960,"children":1078},[1079,1080,1081],{"id":966,"depth":133,"text":967},{"id":997,"depth":133,"text":998},{"id":1033,"depth":133,"text":1034},{"id":1040,"depth":126,"text":1041},"2026-11-30","Every feature flag doubles the paths through your code. Unit tests and functional tests need different things from a flag, and each approach to supplying one is a trade-off. Here is how to pick.",false,"md","\u002Fhow-to-test-code-that-uses-feature-flags-cover.webp",{},"\u002Fblog\u002Fhow-to-test-code-that-uses-feature-flags",{"title":5,"description":1084},{"loc":1089,"lastmod":1092},"2026-11-30T22:18:33.000Z","blog\u002Fhow-to-test-code-that-uses-feature-flags","HDnokr4xssO9dvCxxFbpzNRo1RAJpCOuDFNcl2VJPyM",1796077225294]