Skip to content

Commit a06d5cd

Browse files
authored
fix(testing): Fix captcha triggering issue in Playwright tests (#5075)
1 parent 5b87dc7 commit a06d5cd

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/testing': patch
3+
---
4+
5+
Fix captcha triggering on Playwright tests

packages/testing/src/playwright/setupClerkTestingToken.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,41 @@ export const setupClerkTestingToken = async ({ page, options }: SetupClerkTestin
3131
}
3232
const apiUrl = `https://${fapiUrl}/v1/**/*`;
3333

34-
await page.route(apiUrl, (route, request) => {
35-
const originalUrl = new URL(request.url());
34+
await page.route(apiUrl, async route => {
35+
const originalUrl = new URL(route.request().url());
3636
const testingToken = process.env.CLERK_TESTING_TOKEN;
3737

3838
if (testingToken) {
3939
originalUrl.searchParams.set(TESTING_TOKEN_PARAM, testingToken);
4040
}
4141

42-
void route.continue({
43-
url: originalUrl.toString(),
44-
});
42+
try {
43+
const response = await route.fetch({
44+
url: originalUrl.toString(),
45+
});
46+
47+
const json = await response.json();
48+
49+
// Override captcha_bypass in /v1/client
50+
if (json?.response?.captcha_bypass === false) {
51+
json.response.captcha_bypass = true;
52+
}
53+
54+
// Override captcha_bypass in piggybacking
55+
if (json?.client?.captcha_bypass === false) {
56+
json.client.captcha_bypass = true;
57+
}
58+
59+
await route.fulfill({
60+
response,
61+
json,
62+
});
63+
} catch {
64+
await route
65+
.continue({
66+
url: originalUrl.toString(),
67+
})
68+
.catch(console.error);
69+
}
4570
});
4671
};

packages/types/src/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface ClientJSON extends ClerkResourceJSON {
7070
sessions: SessionJSON[];
7171
sign_up: SignUpJSON | null;
7272
sign_in: SignInJSON | null;
73-
captcha_bypass?: boolean;
73+
captcha_bypass?: boolean; // this is used by the @clerk/testing package
7474
last_active_session_id: string | null;
7575
cookie_expires_at: number | null;
7676
created_at: number;

0 commit comments

Comments
 (0)