Clerk Testing

by clerka02dbd2a933bMIT86 starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated today

E2E testing for Clerk apps. Use with Playwright or Cypress for auth flow tests.

Instructions onlySoftware Development
AI-generated overview

Guides end-to-end authentication testing for Clerk apps with Playwright or Cypress.

What it does
This skill provides instructions for writing end-to-end auth flow tests for applications that use Clerk. It covers a decision tree for Playwright or Cypress documentation, a mental model of isolated session state, setup steps such as clerkSetup() and setupClerkTestingToken(), and best practices like persisting auth with storageState. It also lists anti-patterns and framework-specific guidance. It produces guidance rather than scripts or files.
When to use it
Use it when adding or maintaining end-to-end authentication tests for a Clerk-based application, especially with Playwright or Cypress. It is also useful when tests fail because of bot detection or repeated UI sign-ins.
Requirements
Requires a Clerk publishable key and either CLERK_SECRET_KEY or CLERK_TESTING_TOKEN; keys can be generated with the Clerk CLI or taken from the Clerk Dashboard. It uses the WebFetch tool to read Clerk documentation and ships no scripts.

Testing

Decision Tree

Mental Model

Test auth = isolated session state. Each test needs fresh auth context.

  • clerkSetup() initializes test environment
  • setupClerkTestingToken() bypasses bot detection
  • storageState persists auth between tests for speed

Workflow

  1. Identify test framework (Playwright or Cypress)
  2. WebFetch the appropriate URL from decision tree above
  3. Follow official setup instructions
  4. Use development-instance keys only (pk_test_* / sk_test_*). If the project has no keys yet, npx clerk@latest init (the Clerk CLI; see clerk-setup) creates a Clerk application and writes development-instance keys to the env file. No Clerk account or Dashboard visit is needed.

Best Practices

  • Use setupClerkTestingToken() before navigating to auth pages
  • Use test API keys: pk_test_xxx, sk_test_xxx
  • Save auth state with storageState for faster tests
  • Use page.waitForSelector('[data-clerk-component]') for Clerk UI

Anti-Patterns

PatternProblemFix
Production keys in testsSecurity riskUse pk_test_* keys
No setupClerkTestingToken()Auth failsCall before navigation
UI-based sign-in every testSlow testsUse storageState

Framework-Specific

Playwright: Use globalSetup for auth state Cypress: Add addClerkCommands({ Cypress, cy }) to support file

See Also

  • clerk-setup - Install Clerk before adding tests
  • clerk-nextjs-patterns - Next.js patterns being tested
  • Demo Repo

Source and attribution

Source:clerk/skillsinskills/clerk-testingat commita02dbd2

License: MIT

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal