Skip to content

Commit a19e151

Browse files
committed
feat(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. Additionally, added logging of all installed @clerk/* packages (including transitive dependencies) during setup to aid in debugging version mismatches in CI.
1 parent dc4bbc4 commit a19e151

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

integration/models/application.ts

Lines changed: 37 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';
@@ -46,6 +49,10 @@ export const application = (
4649
const log = logger.child({ prefix: 'setup' }).info;
4750
await run(scripts.setup, { cwd: appDirPath, log });
4851
state.completedSetup = true;
52+
// Print all Clerk package versions (direct and transitive)
53+
const clerkPackagesLog = logger.child({ prefix: 'clerk-packages' }).info;
54+
clerkPackagesLog('Installed @clerk/* packages:');
55+
await run('pnpm list @clerk/* --depth 100', { cwd: appDirPath, log: clerkPackagesLog });
4956
}
5057
},
5158
dev: async (opts: { port?: number; manualStart?: boolean; detached?: boolean; serverUrl?: string } = {}) => {
@@ -82,6 +89,36 @@ export const application = (
8289
log(`Server started at ${runtimeServerUrl}, pid: ${proc.pid}`);
8390
cleanupFns.push(() => awaitableTreekill(proc.pid, 'SIGKILL'));
8491
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+
85122
return { port, serverUrl: runtimeServerUrl, pid: proc.pid };
86123
},
87124
build: async () => {

0 commit comments

Comments
 (0)