Diff feature flags to quickly debug tests failing due to someone changing one of the flags.
As I explained in my previous blog post on feature flags and testing, you need the flags to be the same to ensure consistent application behavior during end-to-end tests. If you want to test the specific feature, control the flag from the test. But sometimes the tests unexpectedly fail when one of the developers or QA engineers is testing something and modifies the flag accidentally (and forgets to turn it back off to the default).
Here is how you can quickly understand why some tests failed. Imagine we have a LaunchDarkly project with a flag "testing-launch-darkly-control-from-cypress"
The flag has several variations
Currently, our application is receiving the "Formal" greeting, and the tests confirm it
Store all current feature flags as JSON
In my case, I am using LaunchDarkly and I control it via my cypress-ld-control plugin. This plugin has a CLI utility for grabbing one or more project flags and printing the JSON to STDIO.
1
$ LAUNCH_DARKLY_AUTH_TOKEN=... npx list-ld-flags --project demo-project --environment test
You can save the produced output into a JSON file
1
$ npx list-ld-flags --project demo-project --environment test > ld-flags.json
Commit the JSON file and store in your source control, as it should not change too often.
Diff flags before running tests
Before launching end-to-end tests, output a diff of the current feature flags and the saved flags JSON file. For example, using GitHub Actions:
1 2 3 4 5 6
-name:LDflagdifferences run:nodebin/list-ld-flags.js--environmenttest--diffld-flags.json env: # our CI has the project key as an environment variable LAUNCH_DARKLY_PROJECT_KEY:${{secrets.LAUNCH_DARKLY_PROJECT_KEY}} LAUNCH_DARKLY_AUTH_TOKEN:${{secrets.LAUNCH_DARKLY_AUTH_TOKEN}}
Let's say something changes, maybe someone is testing the new greeting and flipped the LaunchDarkly switch. The CI output shows the change:
Nice, if any E2E tests fail we will quickly know the suspected reason. The tool also outputs nice GitHub Actions summary with each flag object's diff:
Easy to spot any potential feature flags that might affect the tests.
Update the flags JSON when needed
If the feature flag defaults change, you might need to update the JSON file. I have a little workflow that the user can run:
name:ld-flags on: workflow_dispatch: inputs: update-file: description:'Update the ld-flags.json file with the latest flags from LaunchDarkly' required:false default:false type:boolean
-name:DiffLDflags run:npxlist-ld-flags--environmenttest--diffld-flags.json env: # our CI has the project key as an environment variable LAUNCH_DARKLY_PROJECT_KEY:${{secrets.LAUNCH_DARKLY_PROJECT_KEY}} LAUNCH_DARKLY_AUTH_TOKEN:${{secrets.LAUNCH_DARKLY_AUTH_TOKEN}}