Write end-to-end tests with flag overrides
Set flag values for a browser session, then check the running application's behavior. End-to-end (e2e) tests exercise the application through a browser.
An override sets a flag value for a request. The Flags SDK reads these values from the encrypted vercel-flag-overrides cookie.
Overrides work with any provider connected through the Flags SDK.
Before you start
Use your application's existing FLAGS_SECRET, the secret that the Flags SDK uses for Flags Explorer overrides.
The Playwright configuration below loads the application's environment files. The application and tests use the same secret to read and create overrides.
If you have not configured the Flags SDK, complete the SvelteKit quickstart first.
Install Playwright and its Chromium browser if your project does not already have them:
These commands add Playwright to your project and download Chromium for the tests.
Add a page with two flag states
Use an existing page that uses a flag, or add this /dashboard example. The heading changes with the flag value.
Declare the flag:
Configure the request hook if your application does not already use createHandle():
For an application with existing hooks, follow the createHandle() setup instructions.
Evaluate the flag in a server load function. This function supplies data when SvelteKit renders the page:
Render the heading with Svelte 5:
Configure Playwright
Use Vite's environment loader to read the application's existing FLAGS_SECRET from its development environment files:
Playwright starts the application with your existing dev script and waits for /dashboard to respond.
For another page, update webServer.url and the test's page.goto() path. The override fixture uses baseURL to select the cookie's hostname.
To test an existing server or Preview Deployment, set BASE_URL and supply that application's FLAGS_SECRET.
If you test on Vercel with Deployment Protection enabled, configure Protection Bypass for Automation.
Pass the value of VERCEL_AUTOMATION_BYPASS_SECRET in the x-vercel-protection-bypass header on each request. This secret is separate from FLAGS_SECRET, which encrypts flag overrides.
See Vite environment loading and Playwright configuration for additional options.
Create an override fixture
A Playwright fixture supplies tools and setup to a test. Add a setFlagOverrides() fixture that encrypts flag values and stores them in the override cookie:
Use the declared key, such as show-new-dashboard, as the map key. The exported function name can be different.
The cookie covers all paths on the target hostname. The encrypted values expire after one hour. See encryptOverrides() and Playwright's cookie API for details.
Test both flag values
Import test and expect from your fixture file. Call setFlagOverrides() before the first page request that evaluates the flag:
Run the tests:
Both tests should pass. Each test checks that the page shows the heading for its flag value and hides the other heading.
Playwright gives each test a separate browser context, so cookies from one test do not affect another.
Test additional values and pages
Overrides support boolean, string, and numeric values, as well as arrays and objects that your flag accepts. Set all required flag values in one call. Each setFlagOverrides() call replaces the cookie contents.
For pages that use precomputed flag values, set overrides before the request that computes those values. Check the page content to confirm that the application used the expected flag values.
The override cookie applies to requests on the configured hostname. Services that evaluate flags directly through a provider need their own test setup.
Next steps
Write unit tests with the Flags SDK