Skip to content

Commit 6705a94

Browse files
committed
fix(repo): Better route interception hierarchy
1 parent 0ae0403 commit 6705a94

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
lines changed

integration/testUtils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ const createClerkUtils = ({ page }: TestArgs) => {
102102
};
103103
};
104104

105-
const createTestingTokenUtils = ({ page }: TestArgs) => {
105+
const createTestingTokenUtils = ({ context }: TestArgs) => {
106106
return {
107-
setup: async () => setupClerkTestingToken({ page }),
107+
setup: async () => setupClerkTestingToken({ context }),
108108
};
109109
};
110110

integration/tests/non-secure-context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ testAgainstRunningApps({ withPattern: ['next.appRouter.withEmailCodes'] })(
5151
server.close();
5252
});
5353

54-
test('sign-in flow', async ({ page }) => {
55-
const u = createTestUtils({ app, page });
54+
test('sign-in flow', async ({ context, page }) => {
55+
const u = createTestUtils({ app, context, page });
5656

5757
await u.po.testingToken.setup();
5858
await u.page.goto(`http://${APP_HOST}`, { timeout: 50000 });

integration/tests/resiliency.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })('resilienc
197197
await expect(page.getByText('Clerk is loading', { exact: true })).toBeHidden();
198198
});
199199

200-
// TODO: Fix detection of hotloaded clerk-js failing
201-
test.skip('clerk-js client fails and status degraded', async ({ page, context }) => {
200+
test('clerk-js client fails and status degraded', async ({ page, context }) => {
202201
const u = createTestUtils({ app, page, context });
203202

204203
await page.route('**/v1/client?**', route => route.fulfill(make500ClerkResponse()));
@@ -227,8 +226,7 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })('resilienc
227226
await expect(page.getByText('Clerk is loading', { exact: true })).toBeHidden();
228227
});
229228

230-
// TODO: Fix flakiness when intercepting environment requests
231-
test.skip('clerk-js environment fails and status degraded', async ({ page, context }) => {
229+
test('clerk-js environment fails and status degraded', async ({ page, context }) => {
232230
const u = createTestUtils({ app, page, context });
233231

234232
await page.route('**/v1/environment?**', route => route.fulfill(make500ClerkResponse()));

integration/tests/reverification.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withReverification] })(
3737
});
3838

3939
test.afterAll(async () => {
40-
await fakeOrganization.delete();
41-
await fakeViewer.deleteIfExists();
42-
await fakeAdmin.deleteIfExists();
43-
await app.teardown();
40+
try {
41+
await fakeOrganization.delete();
42+
await fakeViewer.deleteIfExists();
43+
await fakeAdmin.deleteIfExists();
44+
} catch (error) {
45+
console.error(error);
46+
} finally {
47+
await app.teardown();
48+
}
4449
});
4550

4651
test('reverification prompt on adding new email address', async ({ page, context }) => {

packages/testing/src/playwright/helpers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ type PlaywrightClerkSignInParams = {
8888
};
8989

9090
const signIn = async ({ page, signInParams, setupClerkTestingTokenOptions }: PlaywrightClerkSignInParams) => {
91-
await setupClerkTestingToken({ page, options: setupClerkTestingTokenOptions });
91+
const context = page.context();
92+
if (!context) {
93+
throw new Error('Page context is not available. Make sure the page is properly initialized.');
94+
}
95+
96+
await setupClerkTestingToken({ context, options: setupClerkTestingTokenOptions });
9297
await loaded({ page });
9398

9499
await page.evaluate(signInHelper, { signInParams });

packages/testing/src/playwright/setupClerkTestingToken.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
1-
import type { Page } from '@playwright/test';
1+
import type { BrowserContext, Page } from '@playwright/test';
22

33
import type { SetupClerkTestingTokenOptions } from '../common';
44
import { ERROR_MISSING_FRONTEND_API_URL, TESTING_TOKEN_PARAM } from '../common';
55

66
type SetupClerkTestingTokenParams = {
7-
page: Page;
7+
context?: BrowserContext;
8+
page?: Page;
89
options?: SetupClerkTestingTokenOptions;
910
};
1011

1112
/**
1213
* Bypasses bot protection by appending the testing token in the Frontend API requests.
1314
*
15+
* @param params.context - The Playwright browser context object.
1416
* @param params.page - The Playwright page object.
1517
* @param params.options.frontendApiUrl - The frontend API URL for your Clerk dev instance, without the protocol.
1618
* @returns A promise that resolves when the bot protection bypass is set up.
1719
* @throws An error if the Frontend API URL is not provided.
1820
* @example
1921
* import { setupClerkTestingToken } from '@clerk/testing/playwright';
2022
*
21-
* test('should bypass bot protection', async ({ page }) => {
22-
* await setupClerkTestingToken({ page });
23+
* test('should bypass bot protection', async ({ context }) => {
24+
* await setupClerkTestingToken({ context });
25+
* const page = await context.newPage();
2326
* await page.goto('https://your-app.com');
2427
* // Continue with your test...
2528
* });
2629
*/
27-
export const setupClerkTestingToken = async ({ page, options }: SetupClerkTestingTokenParams) => {
30+
export const setupClerkTestingToken = async ({ context, options, page }: SetupClerkTestingTokenParams) => {
31+
const browserContext = context ?? page?.context();
32+
33+
if (!browserContext) {
34+
throw new Error('Either context or page must be provided to setup testing token');
35+
}
36+
2837
const fapiUrl = options?.frontendApiUrl || process.env.CLERK_FAPI;
2938
if (!fapiUrl) {
3039
throw new Error(ERROR_MISSING_FRONTEND_API_URL);
3140
}
3241
const apiUrl = `https://${fapiUrl}/v1/**/*`;
3342

34-
await page.route(apiUrl, async route => {
43+
await browserContext.route(apiUrl, async route => {
3544
const originalUrl = new URL(route.request().url());
3645
const testingToken = process.env.CLERK_TESTING_TOKEN;
3746

0 commit comments

Comments
 (0)