Skip to content

Commit 0e21635

Browse files
committed
fix(e2e): automatically setup clerk testing tokens in app.dev()
Integration tests that create their own applications (using `.clone().commit()`) were failing in CI with "CLERK_FAPI is required" errors. This happened because `setupClerkTestingToken()` needs the CLERK_FAPI environment variable, which is set by calling `clerkSetup()`. Long-running apps automatically call `clerkSetup()` during init, but apps created inline in tests did not. Rather than requiring each test to manually call `clerkSetup()`, moved it into the `Application.dev()` method to run automatically after the server starts. This ensures all applications (both long-running and inline) have proper testing token setup without manual intervention, preventing future test failures and reducing boilerplate.
1 parent 50e3337 commit 0e21635

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

integration/models/application.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as path from 'node:path';
22

3+
import { parsePublishableKey } from '@clerk/shared/keys';
4+
import { clerkSetup } from '@clerk/testing/playwright';
5+
36
import { awaitableTreekill, createLogger, fs, getPort, run, waitForIdleProcess, waitForServer } from '../scripts';
47
import type { ApplicationConfig } from './applicationConfig.js';
58
import type { EnvironmentConfig } from './environment.js';
@@ -86,6 +89,36 @@ export const application = (
8689
log(`Server started at ${runtimeServerUrl}, pid: ${proc.pid}`);
8790
cleanupFns.push(() => awaitableTreekill(proc.pid, 'SIGKILL'));
8891
state.serverUrl = runtimeServerUrl;
92+
93+
// Setup Clerk testing tokens after the server is running
94+
if (state.env) {
95+
try {
96+
const publishableKey = state.env.publicVariables.get('CLERK_PUBLISHABLE_KEY');
97+
const secretKey = state.env.privateVariables.get('CLERK_SECRET_KEY');
98+
const apiUrl = state.env.privateVariables.get('CLERK_API_URL');
99+
100+
if (publishableKey && secretKey) {
101+
const { instanceType, frontendApi: frontendApiUrl } = parsePublishableKey(publishableKey);
102+
103+
if (instanceType !== 'development') {
104+
log('Skipping clerkSetup for non-development instance');
105+
} else {
106+
await clerkSetup({
107+
publishableKey,
108+
frontendApiUrl,
109+
secretKey,
110+
// @ts-expect-error apiUrl is not a typed option for clerkSetup, but it is accepted at runtime.
111+
apiUrl,
112+
dotenv: false,
113+
});
114+
log('Clerk testing tokens setup complete');
115+
}
116+
}
117+
} catch (error) {
118+
logger.warn('Failed to setup Clerk testing tokens:', error);
119+
}
120+
}
121+
89122
return { port, serverUrl: runtimeServerUrl, pid: proc.pid };
90123
},
91124
build: async () => {

integration/tests/custom-flows/sign-in.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { parsePublishableKey } from '@clerk/shared/keys';
2-
import { clerkSetup } from '@clerk/testing/playwright';
31
import { expect, test } from '@playwright/test';
42

53
import type { Application } from '../../models/application';
@@ -19,20 +17,6 @@ test.describe('Custom Flows Sign In @custom', () => {
1917
await app.withEnv(appConfigs.envs.withEmailCodes);
2018
await app.dev();
2119

22-
const publishableKey = appConfigs.envs.withEmailCodes.publicVariables.get('CLERK_PUBLISHABLE_KEY');
23-
const secretKey = appConfigs.envs.withEmailCodes.privateVariables.get('CLERK_SECRET_KEY');
24-
const apiUrl = appConfigs.envs.withEmailCodes.privateVariables.get('CLERK_API_URL');
25-
const { frontendApi: frontendApiUrl } = parsePublishableKey(publishableKey);
26-
27-
await clerkSetup({
28-
publishableKey,
29-
frontendApiUrl,
30-
secretKey,
31-
// @ts-expect-error
32-
apiUrl,
33-
dotenv: false,
34-
});
35-
3620
const u = createTestUtils({ app });
3721
fakeUser = u.services.users.createFakeUser({
3822
fictionalEmail: true,

integration/tests/custom-flows/sign-up.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { parsePublishableKey } from '@clerk/shared/keys';
2-
import { clerkSetup } from '@clerk/testing/playwright';
31
import { expect, test } from '@playwright/test';
42

53
import type { Application } from '../../models/application';
@@ -19,20 +17,6 @@ test.describe('Custom Flows Sign Up @custom', () => {
1917
await app.withEnv(appConfigs.envs.withEmailCodes);
2018
await app.dev();
2119

22-
const publishableKey = appConfigs.envs.withEmailCodes.publicVariables.get('CLERK_PUBLISHABLE_KEY');
23-
const secretKey = appConfigs.envs.withEmailCodes.privateVariables.get('CLERK_SECRET_KEY');
24-
const apiUrl = appConfigs.envs.withEmailCodes.privateVariables.get('CLERK_API_URL');
25-
const { frontendApi: frontendApiUrl } = parsePublishableKey(publishableKey);
26-
27-
await clerkSetup({
28-
publishableKey,
29-
frontendApiUrl,
30-
secretKey,
31-
// @ts-expect-error
32-
apiUrl,
33-
dotenv: false,
34-
});
35-
3620
const u = createTestUtils({ app });
3721
fakeUser = u.services.users.createFakeUser({
3822
fictionalEmail: true,

0 commit comments

Comments
 (0)