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 从公开仓库中收录这些内容。

举报或申请下架