Skip to content

Commit bb57007

Browse files
committed
fix(clerk-js): keep degraded-load test from stalling on the background retry
The "fails fast when the client fetch hangs" test made every mocked fetch hang, including the background /client retry, which left a forever-pending promise that stalled the test under CI. Scope the hang to the primary fetch and let the retry resolve. Also inline the session-cookie read at its use sites in the recovery path instead of hoisting a variable.
1 parent 3ba3d61 commit bb57007

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

packages/clerk-js/src/core/__tests__/clerk.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,15 +879,17 @@ describe('Clerk singleton', () => {
879879

880880
it('fails fast and marks Clerk degraded when the client fetch hangs', async () => {
881881
vi.useFakeTimers();
882-
mockClientFetch.mockReturnValue(new Promise(() => {}));
882+
// Only the primary fetch hangs; the background retry must resolve so it can't stall the test.
883+
mockClientFetch
884+
.mockReturnValueOnce(new Promise(() => {}))
885+
.mockResolvedValue({ signedInSessions: [], lastActiveSessionId: null });
883886

884887
const sut = new Clerk(productionPublishableKey);
885888
await pumpUntilSettled(sut.load());
886889

887890
expect(sut.status).toBe('degraded');
888891
expect(stopPollSpy).toHaveBeenCalled();
889892
expect(startPollSpy).toHaveBeenCalled();
890-
// The timed-out /client request gets aborted instead of being left in flight.
891893
expect(mockClientFetch.mock.calls[0]?.[0]?.abortSignal?.aborted).toBe(true);
892894
});
893895

packages/clerk-js/src/core/clerk.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,19 +3272,18 @@ export class Clerk implements ClerkInterface {
32723272
this.#authService?.stopPollingForToken();
32733273

32743274
try {
3275-
const jwtInCookie = this.#authService?.getSessionCookie();
3276-
const session = this.#defaultSession(createClientFromJwt(jwtInCookie));
3275+
const session = this.#defaultSession(createClientFromJwt(this.#authService?.getSessionCookie()));
32773276

32783277
if (session) {
32793278
// Prefer minting a fresh token as its claims are fresher than the cookie's
32803279
session.clearCache();
32813280
const jwt = await timeLimit(session.getToken(), INITIALIZATION_TIMEOUT_MS).catch(() => {
32823281
session.clearCache();
3283-
return jwtInCookie;
3282+
return this.#authService?.getSessionCookie();
32843283
});
32853284
this.updateClient(createClientFromJwt(jwt));
32863285
} else {
3287-
this.updateClient(createClientFromJwt(jwtInCookie));
3286+
this.updateClient(createClientFromJwt(this.#authService?.getSessionCookie()));
32883287
}
32893288
} finally {
32903289
this.#authService?.startPollingForToken();

0 commit comments

Comments
 (0)