Skip to Content
Octopus Energy Packages

Vercel flags

Define a Vercel-backed flag and override it in a test

Define a feature flag with the Vercel adapter for the Flags software development kit (SDK). Override its value in a test without connecting to your flag service.

Install the SDK

Add the SDK packages to your runtime dependencies:

npm i @flags-sdk/vercel flags

Configure the app server

Merge the test SDK key into webServer.env, preserving its other values:

playwright.config.ts (webServer fragment)
env: {
  FLAGS: "vf_server_playwright",
},

vf_server_playwright is a dummy key for mocked tests. The app’s vercelAdapter reads FLAGS.

Define the flag

Create a flag module using the Vercel adapter:

lib/flags.ts
import { vercelAdapter } from "@flags-sdk/vercel";
import { flag } from "flags/next";

export const checkoutRedesign = flag<boolean>({
  key: "checkoutRedesign",
  adapter: vercelAdapter,
});

Create the checkout page

Choose the page for your router. Both variants evaluate the flag and render the corresponding heading:

Evaluate the flag in a server component:

app/checkout/page.tsx
import { checkoutRedesign } from "../../lib/flags";

export default async function CheckoutPage() {
  const redesigned = await checkoutRedesign();

  return <h1>{redesigned ? "New checkout" : "Original checkout"}</h1>;
}

Set shared defaults

This example uses MSW 2. With MSW 3, import the factory from @octopus-energy/playwright/msw/v3 instead; the shared fixture composes with either factory. See version selection.

Extend the helper with a default flag value:

lib/playwright/test.ts
import { createPlaywrightHelpers } from "@octopus-energy/playwright/msw/v2";
import { withVercelFlags } from "@octopus-energy/playwright/flags/vercel";

const { test: base, expect } = createPlaywrightHelpers();

const test = base.extend(withVercelFlags({ checkoutRedesign: false }));

export { expect, test };

Vercel can reuse cached flag values between tests. Run scenarios with different values in separate Playwright runs.

Enable the flag and assert checkout

Override the flag before navigation so the page reads the test’s value:

tests/playwright/checkout.spec.ts
import { expect, test } from "../../lib/playwright/test";

test("shows redesigned checkout", async ({ page, vercelFlags }) => {
  vercelFlags.use({ checkoutRedesign: true });

  await page.goto("/checkout");
  const heading = page.getByRole("heading", { name: "New checkout" });
  await expect(heading).toBeVisible();
});

Run the test from your app root:

npm exec -- playwright test tests/playwright/checkout.spec.ts

See the Vercel flags API for its signature.

On this page