Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cypress): Restructure and add Account Portal section #1679

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
94647cf
feat(cypress): Restructure and add Account Portal section
anagstef Nov 5, 2024
d2f9db8
fix broken link
anagstef Nov 5, 2024
de8b50a
code review
alexisintech Nov 7, 2024
fc35a87
Merge branch 'main' into stefanos/eco-53-cypress-add-how-to-test-acco…
alexisintech Nov 7, 2024
a58055f
Update docs/testing/cypress/custom-commands.mdx
alexisintech Nov 7, 2024
67316db
Apply suggestions from code review
alexisintech Nov 7, 2024
b336d10
fix linting
anagstef Nov 8, 2024
489473b
Update docs/testing/cypress/custom-commands.mdx
anagstef Nov 8, 2024
47f2809
Update docs/testing/cypress/custom-commands.mdx
anagstef Nov 8, 2024
d549c39
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
dbed824
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
cd36d93
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
7e1a20b
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
375781b
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
b245baf
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
426d2f8
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
3d1c642
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
bb9aa00
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
78dde7f
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
46c5a99
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
e3a22dc
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
1e142e8
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
9ac86fd
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
fe559ac
Update docs/testing/cypress/overview.mdx
anagstef Nov 8, 2024
53ffdc8
Update docs/testing/cypress/test-account-portal.mdx
anagstef Nov 8, 2024
ccd84da
Merge branch 'main' into stefanos/eco-53-cypress-add-how-to-test-acco…
victoriaxyz Nov 13, 2024
bf90bc2
Replace AP url with mock data
anagstef Nov 14, 2024
7477f14
Merge branch 'main' into stefanos/eco-53-cypress-add-how-to-test-acco…
anagstef Nov 14, 2024
f3ecff5
Update docs/manifest.json
alexisintech Nov 14, 2024
eebe0c3
Merge branch 'main' into stefanos/eco-53-cypress-add-how-to-test-acco…
victoriaxyz Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,26 @@
]
]
},
{ "title": "Cypress", "href": "/docs/testing/cypress" },
{
"title": "Cypress",
"collapse": true,
"items": [
[
{
"title": "Overview",
"href": "/docs/testing/cypress/overview"
},
{
"title": "Custom Commands",
alexisintech marked this conversation as resolved.
Show resolved Hide resolved
"href": "/docs/testing/cypress/custom-commands"
},
{
"title": "Test Account Portal",
"href": "/docs/testing/cypress/test-account-portal"
}
]
]
},
{
"title": "Postman or Insomnia",
"href": "/docs/testing/postman-or-insomnia"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,8 @@
---
title: Testing with Cypress
description: Use Cypress to write end-to-end tests with Clerk.
title: Custom Commands
description: Use Custom Commands to sign in/sign out with Clerk in your Cypress tests.
alexisintech marked this conversation as resolved.
Show resolved Hide resolved
---

> [!WARNING]
> Our `@clerk/testing` package only supports end-to-end testing. Unit tests are not supported.

Testing with Cypress requires the use of Clerk's [Testing Tokens](/docs/testing/overview#testing-tokens) to bypass various bot detection mechanisms that typically safeguard Clerk applications against malicious bots. Without Testing Tokens, you may encounter "Bot traffic detected" errors in your requests.

This guide aims to 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. Install it by running the following command:

<CodeBlockTabs options={['npm', 'yarn', 'pnpm']}>
```sh {{ filename: 'terminal' }}
npm i @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. Navigate to the [Clerk Dashboard](https://dashboard.clerk.com/last-active?path=api-keys).
1. In the navigation sidebar, select **API Keys**.
1. In the **Quick Copy** section, copy your Clerk publishable and secret key.

> [!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, you must 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 obtain a [Testing Token](/docs/testing/overview#testing-tokens) when your test suite starts, making it available for all subsequent tests to use.

### Use Clerk Testing Tokens

Now that Cypress is configured with Clerk, you can use the `setupClerkTestingToken()` function in your tests to augment them with the Testing Token. See the following example:

```tsx filename="my-test.cy.ts"
import { setupClerkTestingToken } from '@clerk/testing/cypress'

it('sign up', () => {
setupClerkTestingToken()

cy.visit('/sign-up')
// ...
})
```
</Steps>

## Custom Commands

The `@clerk/testing` package also provides [Cypress custom commands](https://docs.cypress.io/api/cypress-api/custom-commands) to sign in/sign out with Clerk in your Cypress tests without having to interact with the UI.
alexisintech marked this conversation as resolved.
Show resolved Hide resolved
To use these commands, you must import them in your [Cypress E2E support file](https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Support-file), as shown in the following example.

Expand Down Expand Up @@ -215,5 +135,3 @@ it('check Clerk loaded', () => {
// Clerk has been loaded from here on
})
```

For more information, feedback or issues, visit the [`@clerk/testing`](https://github.com/clerk/javascript/tree/main/packages/testing) package.
82 changes: 82 additions & 0 deletions docs/testing/cypress/overview.mdx
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 Clerk's [Testing Tokens](/docs/testing/overview#testing-tokens) to bypass various bot detection mechanisms that typically safeguard Clerk applications against malicious bots. Without Testing Tokens, you may encounter "Bot traffic detected" errors in your requests.
anagstef marked this conversation as resolved.
Show resolved Hide resolved

This guide aims to help you set up your environment for creating Clerk-authenticated tests with Cypress.
anagstef marked this conversation as resolved.
Show resolved Hide resolved

> [!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. Install it by running the following command:
alexisintech marked this conversation as resolved.
Show resolved Hide resolved

<CodeBlockTabs options={['npm', 'yarn', 'pnpm']}>
```sh {{ filename: 'terminal' }}
npm i @clerk/testing --save-dev
alexisintech marked this conversation as resolved.
Show resolved Hide resolved
```

```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. Navigate to the [Clerk Dashboard](https://dashboard.clerk.com/last-active?path=api-keys).
1. In the navigation sidebar, select **API Keys**.
alexisintech marked this conversation as resolved.
Show resolved Hide resolved
1. In the **Quick Copy** section, copy your Clerk publishable and secret key.

> [!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, you must call the `clerkSetup()` function in your [`cypress.config.ts`](https://docs.cypress.io/guides/references/configuration) file.
anagstef marked this conversation as resolved.
Show resolved Hide resolved

```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 obtain a [Testing Token](/docs/testing/overview#testing-tokens) when your test suite starts, making it available for all subsequent tests to use.
alexisintech marked this conversation as resolved.
Show resolved Hide resolved

### Use Clerk Testing Tokens

Now that Cypress is configured with Clerk, you can use the `setupClerkTestingToken()` function in your tests to augment them with the Testing Token. See the following example:
anagstef marked this conversation as resolved.
Show resolved Hide resolved

```tsx filename="my-test.cy.ts"
anagstef marked this conversation as resolved.
Show resolved Hide resolved
import { setupClerkTestingToken } from '@clerk/testing/cypress'

it('sign up', () => {
setupClerkTestingToken()

cy.visit('/sign-up')
// ...
anagstef marked this conversation as resolved.
Show resolved Hide resolved
})
```
</Steps>
58 changes: 58 additions & 0 deletions docs/testing/cypress/test-account-portal.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: Test Account Portal
description: Learn how to test the Account Portal flow with Cypress.
---

When testing with Cypress, navigations that involve redirects to external URLs can be tricky to test. This guide demonstrates how to test the Account Portal flow with Cypress, which involves redirecting to the Account Portal and back to your application.

In order to achieve this you need to use the `cy.origin()` command. More information about this command can be found in the [Cypress documentation](https://docs.cypress.io/api/commands/origin).

Also, you need to know your Account Portal domain to use it in the `cy.origin()` command. You can find your Account Portal domain in the Clerk Dashboard under the **Configure > Account Portal** section (e.g. `https://ready-flea-75.accounts.dev`).

The test should be structured as follows:

```ts
it('sign in with Account Portal redirects', () => {
setupClerkTestingToken()

// set the origin to the Account Portal domain
cy.origin('https://ready-flea-75.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
// ...
anagstef marked this conversation as resolved.
Show resolved Hide resolved
})

// the user should be redirected back to the protected page
cy.url().should('include', '/protected')
})
```

### Full example

The following snippet showcases a full example of a test that signs in with the Account Portal redirects. It can also be found in the [Clerk Cypress demo repo](https://github.com/clerk/clerk-cypress-nextjs).

```ts
import { setupClerkTestingToken } from '@clerk/testing/cypress'

describe('Testing Tokens', () => {
it('sign in with Account Portal redirects', () => {
setupClerkTestingToken()

cy.origin('https://ready-flea-75.accounts.dev', () => {
anagstef marked this conversation as resolved.
Show resolved Hide resolved
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')
cy.contains('h1', 'This is a PROTECTED page')
})
})
```
2 changes: 1 addition & 1 deletion docs/testing/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To avoid sending an email or SMS message with a one time passcode (OTP) during t
Testing Tokens allow you to bypass bot detection mechanisms that protect Clerk applications from malicious bots, ensuring your test suites run smoothly. Without Testing Tokens, you may encounter "Bot traffic detected" errors in your requests.

> [!NOTE]
> While you can manually implement the following logic in your test suite, Clerk provides [Playwright](/docs/testing/playwright/overview) and [Cypress](/docs/testing/cypress) integrations that handle this automatically.
> While you can manually implement the following logic in your test suite, Clerk provides [Playwright](/docs/testing/playwright/overview) and [Cypress](/docs/testing/cypress/overview) integrations that handle this automatically.

Obtained via the [Backend API](/docs/reference/backend-api/tag/Testing-Tokens){{ target: '_blank' }}, Testing Tokens are short-lived and valid only for the specific instance for which they are issued. Testing Tokens are only available in development instances.

Expand Down
Loading