Skip to content

Instantly share code, notes, and snippets.

@dferber90
Created June 3, 2025 18:57
Show Gist options
  • Save dferber90/ee146a4d09eb036cd0066d07134fd598 to your computer and use it in GitHub Desktop.
Save dferber90/ee146a4d09eb036cd0066d07134fd598 to your computer and use it in GitHub Desktop.
Override Flags SDK for Playwright tests
import { test, expect } from '@playwright/test';
import { setFlagOverrides } from "./y";
test.describe('flags off', () => {
setFlagOverrides(test, {
'example-flag': false,
'another-example-flag': true,
});
test('should do something', async ({ page }) => {
const response = await page.goto('/');
expect(response?.status()).toBe(200);
// ... your test code here
});
});
import type { test as base } from '@playwright/test';
import { encryptOverrides } from 'flags';
const FEATURE_FLAGS_COOKIE_NAME = 'vercel-flag-overrides';
/**
* Sets flag overrides that work with the `flags` package.
*
* @param test - The test instance.
* @param flags - The flags to override.
*/
export function setFlagOverrides(
test: typeof base,
flags: Record<string, unknown>,
): void {
const flagsSecret = process.env.FLAGS_SECRET;
const baseUrl = process.env.BASE_URL;
if (!flagsSecret) {
throw new Error(
'Cannot call setFlags without `FLAGS_SECRET` environment variable defined.',
);
}
test.beforeEach(async ({ context }) => {
const encryptedFlags = await encryptOverrides(flags, flagsSecret);
await context.addCookies([
{
name: FEATURE_FLAGS_COOKIE_NAME,
value: encryptedFlags,
path: '/',
domain: new URL(baseUrl).hostname,
},
]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment