Skip to content

Commit d909f58

Browse files
authored
feat: Add Testing Tokens docs (#942)
1 parent d0a7d83 commit d909f58

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

docs/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@
359359
["Test Emails and Phones", "/testing/test-emails-and-phones"],
360360
"# Testing Frameworks",
361361
["Cypress", "/testing/cypress"],
362+
["Playwright", "/testing/playwright"],
362363
["Postman or Insomnia", "/testing/postman-or-insomnia"]
363364
]
364365
],

docs/testing/overview.mdx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ Testing is an important part of every application. Clerk has built some helpers
1111

1212
To avoid sending an email or SMS message with a one time passcode (OTP) during testing, you can use a faux email address or phone number that has a fixed code. Read the complete documentation [here](https://clerk.com/docs/testing/test-emails-and-phones).
1313

14-
## End-to-end (E2E) testing
14+
## Testing Tokens
1515

16-
Currently, end-to-end testing is not fully supported with Clerk due to tests getting detected as bot traffic. We are currently working on a solution for this.
16+
Testing Tokens enable bypassing various bot detection mechanisms that typically safeguard Clerk applications against malicious bots, thus allowing test suites to run uninhibited. Without Testing Tokens, you may encounter "Bot traffic detected" errors in your requests.
17+
18+
Obtained via the [Backend API](https://clerk.com/docs/reference/backend-api/tag/Testing-Tokens), 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.
19+
20+
Upon retrieval, include the token value in a `__clerk_testing_token` query parameter with your Frontend API requests. For instance, a sign-up request using a Testing Token, would look like this:
21+
22+
```
23+
POST https://happy-hippo-1.clerk.accounts.dev/v1/sign_ups?__clerk_testing_token=1713877200-c_2J2MvPu9PnXcuhbPZNao0LOXqK9A7YrnBn0HmIWxy
24+
```
25+
26+
While manually integrating this logic in your test suite is possible, if you use Playwright you can use our [Playwright integration](/docs/testing/playwright), which will automatically perform all of this for you. We are working on a similar integration for Cypress.
27+
28+
For more information, feedback or issues, visit the
29+
[`@clerk/testing`](https://github.com/clerk/javascript/tree/main/packages/testing)
30+
package.

docs/testing/playwright.mdx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: Testing with Playwright
3+
description: Use Playwright to write end-to-end tests with Clerk.
4+
---
5+
6+
# Testing with Playwright
7+
8+
Playwright is a well-established JavaScript testing framework. This guide aims to help you set up your environment for creating authenticated tests with Clerk. This guide will assume you're somewhat familiar with Clerk and Playwright.
9+
10+
Before diving in, you might want to start by visiting the [Playwright starter](https://github.com/clerk/playwright-clerk-nextjs-example) for an example repo that tests a Clerk-powered application using [Testing Tokens](/docs/testing/overview#testing-tokens).
11+
12+
<Steps>
13+
### Install `@clerk/testing`
14+
15+
Clerk's testing package provides integration helpers for popular testing frameworks.
16+
17+
```bash filename="terminal"
18+
npm install @clerk/testing --save-dev
19+
```
20+
21+
### Set your API keys
22+
23+
Set your publishable and secret keys in your test runner, as the `CLERK_PUBLISHABLE_KEY` and `CLERK_SECRET_KEY` environment variables respectively.
24+
25+
<Callout type="warning">
26+
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).
27+
</Callout>
28+
29+
### Global setup
30+
31+
In your [global setup file](https://playwright.dev/docs/test-global-setup-teardown), call the `clerkSetup` function:
32+
33+
```tsx filename="global-setup.ts"
34+
import { clerkSetup } from '@clerk/testing/playwright';
35+
import { test as setup } from '@playwright/test';
36+
37+
setup('global setup', async ({ }) => {
38+
await clerkSetup();
39+
});
40+
```
41+
42+
`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.
43+
44+
<Callout type="info">
45+
You can set the Testing Token yourself as opposed to calling `clerkSetup`, by
46+
setting it in the `CLERK_TESTING_TOKEN` environment variable.
47+
</Callout>
48+
49+
### Use `setupClerkTestingToken`
50+
51+
Then, in your tests use the `setupClerkTestingToken` function to augment your requests with the token:
52+
53+
```tsx filename="my-test.spec.ts"
54+
import { setupClerkTestingToken } from "@clerk/testing/playwright";
55+
import { test } from "@playwright/test";
56+
57+
test("sign up", async ({ page }) => {
58+
await setupClerkTestingToken({ page });
59+
60+
await page.goto("/sign-up");
61+
// ...
62+
});
63+
```
64+
65+
</Steps>
66+
67+
For more information, feedback or issues, visit the [`@clerk/testing`](https://github.com/clerk/javascript/tree/main/packages/testing) package.

0 commit comments

Comments
 (0)