Skip to content

chore(e2e): Run existing protect tests with a JWT v2 instance #5615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 16, 2025
2 changes: 2 additions & 0 deletions .changeset/purple-falcons-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
5 changes: 5 additions & 0 deletions .changeset/silly-spies-cut.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/testing': patch
---

Accept `apiUrl` from `clerkSetup()`.
2 changes: 2 additions & 0 deletions integration/models/longRunningApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const longRunningApplication = (params: LongRunningApplicationParams) =>
try {
const publishableKey = params.env.publicVariables.get('CLERK_PUBLISHABLE_KEY');
const secretKey = params.env.privateVariables.get('CLERK_SECRET_KEY');
const apiUrl = params.env.privateVariables.get('CLERK_API_URL');
const { instanceType, frontendApi: frontendApiUrl } = parsePublishableKey(publishableKey);

if (instanceType !== 'development') {
Expand All @@ -71,6 +72,7 @@ export const longRunningApplication = (params: LongRunningApplicationParams) =>
publishableKey,
frontendApiUrl,
secretKey,
apiUrl,
dotenv: false,
});
}
Expand Down
8 changes: 8 additions & 0 deletions integration/presets/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ const withSessionTasks = base
.setEnvVariable('public', 'CLERK_PUBLISHABLE_KEY', instanceKeys.get('with-session-tasks').pk)
.setEnvVariable('private', 'CLERK_ENCRYPTION_KEY', constants.E2E_CLERK_ENCRYPTION_KEY || 'a-key');

const withBillingStaging = base
.clone()
.setId('withBillingStaging')
.setEnvVariable('private', 'CLERK_API_URL', 'https://api.clerkstage.dev')
.setEnvVariable('private', 'CLERK_SECRET_KEY', instanceKeys.get('with-billing-staging').sk)
.setEnvVariable('public', 'CLERK_PUBLISHABLE_KEY', instanceKeys.get('with-billing-staging').pk);

export const envs = {
base,
withKeyless,
Expand All @@ -165,4 +172,5 @@ export const envs = {
withSignInOrUpEmailLinksFlow,
withSignInOrUpwithRestrictedModeFlow,
withSessionTasks,
withBillingStaging,
} as const;
1 change: 1 addition & 0 deletions integration/presets/longRunningApps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const createLongRunningApps = () => {
config: next.appRouter,
env: envs.withSessionTasks,
},
{ id: 'next.appRouter.withBillingStaging', config: next.appRouter, env: envs.withBillingStaging },
{
id: 'next.appRouter.withLegalConsent',
config: next.appRouter,
Expand Down
6 changes: 5 additions & 1 deletion integration/tests/protect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { appConfigs } from '../presets';
import type { FakeOrganization, FakeUser } from '../testUtils';
import { createTestUtils, testAgainstRunningApps } from '../testUtils';

testAgainstRunningApps({ withEnv: [appConfigs.envs.withCustomRoles] })('authorization @nextjs', ({ app }) => {
testAgainstRunningApps({
withEnv: [appConfigs.envs.withCustomRoles, appConfigs.envs.withBillingStaging],
})('authorization @nextjs', ({ app }) => {
test.describe.configure({ mode: 'serial' });

let fakeAdmin: FakeUser;
Expand Down Expand Up @@ -40,6 +42,8 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withCustomRoles] })('authoriz
await u.po.signIn.waitForMounted();
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password });
await u.po.expect.toBeSignedIn();
const jwtVersion = await page.evaluate(() => window.Clerk.session?.lastActiveToken?.jwt?.claims?.v);
expect(jwtVersion).toBe(app.env.id === 'withBillingStaging' ? 2 : undefined);

await u.po.organizationSwitcher.goTo();
await u.po.organizationSwitcher.waitForMounted();
Expand Down
4 changes: 2 additions & 2 deletions packages/testing/src/common/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dotenv from 'dotenv';
import type { ClerkSetupOptions, ClerkSetupReturn } from './types';

export const fetchEnvVars = async (options?: ClerkSetupOptions): Promise<ClerkSetupReturn> => {
const { debug = false, dotenv: loadDotEnv = true, ...rest } = options || {};
const { debug = false, dotenv: loadDotEnv = true, apiUrl: apiUrlProp, ...rest } = options || {};

const log = (msg: string) => {
if (debug) {
Expand Down Expand Up @@ -48,7 +48,7 @@ export const fetchEnvVars = async (options?: ClerkSetupOptions): Promise<ClerkSe
log('Fetching testing token from Clerk Backend API...');

try {
const apiUrl = process.env.CLERK_API_URL;
const apiUrl = apiUrlProp || process.env.CLERK_API_URL;
const clerkClient = createClerkClient({ secretKey, apiUrl });
const tokenData = await clerkClient.testingTokens.createTestingToken();
testingToken = tokenData.token;
Expand Down
7 changes: 7 additions & 0 deletions packages/testing/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export type ClerkSetupOptions = {
*/
secretKey?: string;

/*
* The backend API URL
* If not provided, the library will look for the key in the following environment variables:
* - CLERK_API_URL
*/
apiUrl?: string;

/*
* Automatic loading of environment variables from .env files.
* Default: true
Expand Down
Loading