āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā š shadcn/directory/clerk/clerk-docs/guides/development/testing/cypress/custom-commands ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
The @clerk/testing package provides Cypress custom commands to sign in and out with Clerk in your Cypress tests without having to interact with the UI.
To use these commands, you must import them in your Cypress E2E support file, as shown in the following example.
/// <reference types="cypress" />
import { addClerkCommands } from '@clerk/testing/cypress'
addClerkCommands({ Cypress, cy })
export {}
cy.clerkSignInThe cy.clerkSignIn command is used to sign in a user using Clerk. This custom command supports only the following first factor strategies:
Multi-factor authentication is not supported.
[!NOTE] This helper internally uses the
setupClerkTestingToken()helper, so you don't need to call it separately.
cy.visit.cy.clerkSignIn accepts an object with the following properties:
The sign-in strategy. Supported strategies are:
password: The command will sign in the user using the provided password and identifier.phone_code: You must have a user with a test phone number as an identifier (e.g., +15555550100).email_code: You must have a user with a test email as an identifier (e.g., your_email+clerk_test@example.com).identifierstringThe user's identifier. This could be a username, a phone number, or an email.
passwordstringThe user's password. This is required only if the strategy is set to 'password'.
</Properties>
The following example demonstrates how to use the cy.clerkSignIn() command in a test to sign in a user.
it('sign in', () => {
cy.visit(`/`)
cy.clerkSignIn({ strategy: 'phone_code', identifier: '+15555550100' })
cy.visit('/protected')
// user is signed in from here on
})
cy.clerkSignOutThe cy.clerkSignOut command is used to sign out the current user using Clerk.
cy.visit.cy.clerkSignOut accepts an optional parameter signOutOptions, which includes the following properties:
The ID of a specific session to sign out of. Useful for multi-session applications.
redirectUrl?stringThe full URL or path to navigate to after sign-out is complete. </Properties>
The following example demonstrates how to use the cy.clerkSignOut() command in a test to sign a user out.
it('sign out', () => {
cy.visit(`/`)
cy.clerkSignIn({ strategy: 'phone_code', identifier: '+15555550100' })
cy.visit('/protected')
cy.clerkSignOut()
// user is signed out from here on
})
cy.clerkLoadedThe cy.clerkLoaded command asserts that Clerk has been loaded.
cy.visit.The following example demonstrates how to use the cy.clerkLoaded() command in a test to assert that Clerk has been loaded.
it('check Clerk loaded', () => {
cy.visit(`/`)
cy.clerkLoaded()
// Clerk has been loaded from here on
})
ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā