Clerk Testing

作者 clerka02dbd2a933bMIT86 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫今天更新

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

AI 產生的概覽

指導使用 Playwright 或 Cypress 對 Clerk 應用程式進行端對端身分驗證測試。

功能
此技能提供為使用 Clerk 的應用程式撰寫端對端驗證流程測試的說明。內容包括 Playwright 或 Cypress 文件的決策樹、隔離工作階段狀態的心智模型、clerkSetup() 和 setupClerkTestingToken() 等設定步驟,以及用 storageState 保存驗證狀態等最佳做法。它也列出反模式和各框架的專屬指引。它產出的是指引內容,而不是指令碼或檔案。
適用情境
在為以 Clerk 為基礎的應用程式新增或維護端對端身分驗證測試時使用,尤其是搭配 Playwright 或 Cypress。當測試因機器人偵測或反覆進行介面登入而失敗時也適用。
執行需求
需要 Clerk 可發布金鑰以及 CLERK_SECRET_KEY 或 CLERK_TESTING_TOKEN 其中之一;金鑰可透過 Clerk CLI 產生,或從 Clerk 儀表板取得。它使用 WebFetch 工具讀取 Clerk 文件,不附帶任何指令碼。

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

來源與署名

來源:clerk/skills位於skills/clerk-testing提交a02dbd2

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架