clerk-testing
作者: clerk
E2E测试工具,用于在Playwright和Cypress中测试Clerk认证流程。支持Playwright和Cypress框架,并提供框架特定的设置模式(Playwright的globalSetup,Cypress的自定义命令)。提供clerkSetup()和setupClerkTestingToken()工具,用于初始化测试环境并绕过机器人检测。包含storageState持久化功能,可在测试间复用已认证的会话,减少测试执行时间。需要测试API密钥(pk_test_*,...)
npx skills add https://github.com/clerk/skills --skill clerk-testingTesting
Decision Tree
Mental Model
Test auth = isolated session state. Each test needs fresh auth context.
clerkSetup()initializes test environmentsetupClerkTestingToken()bypasses bot detectionstorageStatepersists auth between tests for speed
Workflow
- Identify test framework (Playwright or Cypress)
- WebFetch the appropriate URL from decision tree above
- Follow official setup instructions
- Use
pk_test_*andsk_test_*keys only
Best Practices
- Use
setupClerkTestingToken()before navigating to auth pages - Use test API keys:
pk_test_xxx,sk_test_xxx - Save auth state with
storageStatefor faster tests - Use
page.waitForSelector('[data-clerk-component]')for Clerk UI
Anti-Patterns
| Pattern | Problem | Fix |
|---|---|---|
| Production keys in tests | Security risk | Use pk_test_* keys |
No setupClerkTestingToken() | Auth fails | Call before navigation |
| UI-based sign-in every test | Slow tests | Use 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 testsclerk-nextjs-patterns- Next.js patterns being tested- Demo Repo