-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: Testing with Cypress | ||
description: Use Cypress to write end-to-end tests with Clerk. | ||
--- | ||
|
||
> [!WARNING] | ||
> Our `@clerk/testing` package only supports end-to-end testing. Unit tests are not supported. | ||
Testing with Cypress requires the use of [Testing Tokens](/docs/testing/overview#testing-tokens) to bypass various bot detection mechanisms that typically safeguard Clerk apps against malicious bots. Without Testing Tokens, you might encounter "Bot traffic detected" errors in your requests. | ||
|
||
This guide will help you set up your environment for creating Clerk-authenticated tests with Cypress. | ||
|
||
> [!IMPORTANT] | ||
> Check out the [demo repo](https://github.com/clerk/clerk-cypress-nextjs) that tests a Clerk-powered application using [Testing Tokens](/docs/testing/overview#testing-tokens). | ||
<Steps> | ||
### Install `@clerk/testing` | ||
|
||
Clerk's testing package provides integration helpers for popular testing frameworks. Run the following command to install it: | ||
|
||
<CodeBlockTabs options={['npm', 'yarn', 'pnpm']}> | ||
```sh {{ filename: 'terminal' }} | ||
npm install @clerk/testing --save-dev | ||
``` | ||
|
||
```sh {{ filename: 'terminal' }} | ||
yarn add -D @clerk/testing | ||
``` | ||
|
||
```sh {{ filename: 'terminal' }} | ||
pnpm add @clerk/testing -D | ||
``` | ||
</CodeBlockTabs> | ||
|
||
### Set your API keys | ||
|
||
In your test runner, set your publishable and secret key as the `CLERK_PUBLISHABLE_KEY` and `CLERK_SECRET_KEY` environment variables, respectively. | ||
|
||
To find your keys: | ||
|
||
1. In the Clerk Dashboard, navigate to the [**API Keys**](https://dashboard.clerk.com/last-active?path=api-keys) page. | ||
1. In the **Quick Copy** section, copy your Clerk publishable and secret key. | ||
1. Paste your keys into your `.env.local` file. | ||
|
||
> [!WARNING] | ||
> Ensure you provide the secret key in a secure manner, to avoid leaking it to third parties. For example, if you are using GitHub Actions, refer to [_Using secrets in GitHub Actions_](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions). | ||
|
||
### Global setup | ||
|
||
To set up Clerk with Cypress, call the `clerkSetup()` function in your [`cypress.config.ts`](https://docs.cypress.io/guides/references/configuration) file. | ||
```tsx filename="cypress.config.ts" | ||
import { clerkSetup } from '@clerk/testing/cypress' | ||
import { defineConfig } from 'cypress' | ||
|
||
export default defineConfig({ | ||
e2e: { | ||
setupNodeEvents(on, config) { | ||
return clerkSetup({ config }) | ||
}, | ||
baseUrl: 'http://localhost:3000', // your app's URL | ||
}, | ||
}) | ||
``` | ||
`clerkSetup()` will retrieve a [Testing Token](/docs/testing/overview#testing-tokens) once the test suite starts, making it available for all subsequent tests. | ||
### Use Clerk Testing Tokens | ||
Now that Cypress is configured with Clerk, use the `setupClerkTestingToken()` function in your tests to integrate the Testing Token. See the following example: | ||
```tsx filename="testing-tokens.cy.ts" | ||
import { setupClerkTestingToken } from '@clerk/testing/cypress' | ||
it('sign up', () => { | ||
setupClerkTestingToken() | ||
|
||
cy.visit('/sign-up') | ||
// Add any other actions to test | ||
}) | ||
``` | ||
</Steps> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
title: Test Account Portal flows | ||
description: Learn how to test the Account Portal flow with Cypress. | ||
--- | ||
|
||
Testing navigation flows with Cypress can be challenging, especially when redirects to external URLs are involved. This guide demonstrates how to use Cypress to test the Account Portal flow, including the redirect to the Account Portal and back to your app. | ||
|
||
> [!IMPORTANT] | ||
> Check out the [demo repo](https://github.com/clerk/clerk-cypress-nextjs) that demonstrates testing a Clerk-powered application using [Testing Tokens](/docs/testing/overview#testing-tokens). To run the tests, you'll need dev instance Clerk API keys, a test user with username and password, and have username and password authentication enabled in the Clerk Dashboard. | ||
## Get your Account Portal domain | ||
|
||
To get your Account Portal domain, go to the [**Account Portal**](https://dashboard.clerk.com/last-active?path=account-portal) page in the Clerk Dashboard. The domain format should resemble: `https://verb-noun-00.accounts.dev` | ||
|
||
## The test format | ||
|
||
Cypress's `cy.origin()` function lets you visit multiple domains of different origins in the same test. For more information, see the [Cypress documentation](https://docs.cypress.io/api/commands/origin). | ||
|
||
By passing your Account Portal domain to `cy.origin()`, your test can navigate to the Account Portal domain and then return to your app. | ||
|
||
Structure tests that include Account Portal redirects as follows: | ||
|
||
```ts | ||
it('sign in with Account Portal redirects', () => { | ||
setupClerkTestingToken() | ||
|
||
// Set the origin to the Account Portal domain | ||
cy.origin('https://verb-noun-00.accounts.dev', () => { | ||
// Visit a protected page that redirects to the Account Portal when not signed in | ||
cy.visit('http://localhost:3000/protected') | ||
// Fill the form to sign in | ||
// Add any other actions to test | ||
}) | ||
|
||
// The user should be redirected back to the protected page | ||
cy.url().should('include', '/protected') | ||
}) | ||
``` | ||
|
||
## Example | ||
|
||
The following example creates a test that: | ||
|
||
- visits a protected page, which redirects to the Account Portal when not signed in | ||
- completes the form to sign in | ||
- redirects back to the protected page | ||
|
||
```ts | ||
import { setupClerkTestingToken } from '@clerk/testing/cypress' | ||
|
||
describe('Testing Tokens', () => { | ||
it('sign in with Account Portal redirects', () => { | ||
setupClerkTestingToken() | ||
|
||
cy.origin('https://verb-noun-00.accounts.dev', () => { | ||
cy.visit('http://localhost:3000/protected') | ||
cy.contains('h1', 'Sign in') | ||
cy.get('.cl-signIn-root').should('exist') | ||
cy.get('input[name=identifier]').type(Cypress.env('test_user')) | ||
|
||
cy.get('.cl-formButtonPrimary').contains('button', 'Continue').click() | ||
cy.get('input[name=password]').type(Cypress.env('test_password')) | ||
|
||
cy.get('.cl-formButtonPrimary').contains('button', 'Continue').click() | ||
}) | ||
|
||
cy.url().should('include', '/protected') | ||
// Ensure the user has successfully accessed the protected page | ||
// by checking an element on the page that only the authenticated user can access | ||
cy.contains('h1', 'This is a PROTECTED page') | ||
}) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters