javascript icon indicating copy to clipboard operation
javascript copied to clipboard

@clerk/testing: clerkSetup() doesn't fall back to NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY

Open joerizz23 opened this issue 9 months ago • 1 comments

Description

clerkSetup() from @clerk/testing/playwright requires the env var CLERK_PUBLISHABLE_KEY to be set. However, the standard Next.js convention (and what @clerk/nextjs uses) is NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY. This forces developers to pass both env vars explicitly when running tests.

Environment

  • @clerk/testing: 1.13.36
  • @clerk/nextjs: latest
  • @playwright/test: 1.57.0
  • Next.js: 15.4.11

Reproduction

  1. Configure a Next.js app with Clerk using NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY in .env.local (standard setup)
  2. Set up Playwright with @clerk/testing
  3. Run tests without explicitly setting CLERK_PUBLISHABLE_KEY
  4. clerkSetup() fails because it only looks for CLERK_PUBLISHABLE_KEY

Workaround

Must pass both env vars when running tests:

CLERK_PUBLISHABLE_KEY=$NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY \
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_xxx \
npx playwright test

Expected Behavior

clerkSetup() should fall back to NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY if CLERK_PUBLISHABLE_KEY is not set, since that's the standard env var name used in Next.js projects with @clerk/nextjs.

Suggested Fix

In the clerkSetup() initialization:

const publishableKey = process.env.CLERK_PUBLISHABLE_KEY 
  ?? process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY;

joerizz23 avatar Feb 19 '26 20:02 joerizz23

Hi! @clerk/testing is framework agnostic. It works with any SDK, each with their own env var prefix (NEXT_PUBLIC_, PUBLIC_, VITE_, NUXT_PUBLIC_, etc.). Adding a fallback for NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY would either special-case Next.js or require us to maintain an ever-growing list of framework-specific prefixes, neither of which is a good fit for a generic testing package.

The intended approach is to pass the publishable key explicitly via clerkSetup() if the variable is not CLERK_PUBLISHABLE_KEY

wobsoriano avatar Mar 06 '26 16:03 wobsoriano