-
Notifications
You must be signed in to change notification settings - Fork 443
fix(testing): add retry logic for testing token fetch on 429/5xx #8138
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
65b0620
fix(testing): add retry logic for testing token fetch on 429/5xx
jacekradko cc9a364
Merge branch 'main' into jacek/retry-testing-token
jacekradko c23b2e4
fix(testing): add changeset and cap exponential backoff delay
jacekradko 131cff5
style(testing): format setup.ts with prettier
jacekradko 34b9896
Merge branch 'main' into jacek/retry-testing-token
jacekradko 1d71c8b
Merge branch 'main' into jacek/retry-testing-token
jacekradko f5a40c3
Merge branch 'main' into jacek/retry-testing-token
jacekradko 058d27f
fix(testing): retry on network errors and add 408/500 to retryable st…
jacekradko 4cf7693
style(testing): format setup.ts with prettier
jacekradko 777386e
test(testing): add unit tests for fetchWithRetry retry logic
jacekradko e27ba1a
style(testing): fix import sorting in setup tests
jacekradko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/testing": patch | ||
| --- | ||
|
|
||
| Add retry logic with exponential backoff for testing token fetch on 429 and 5xx responses. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import { ClerkAPIResponseError } from '@clerk/shared/error'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| // Re-export internals for testing by importing the module and testing through fetchEnvVars | ||
| // Since fetchWithRetry and isNetworkError are not exported, we test them indirectly through fetchEnvVars | ||
| // and also directly by extracting them via a test-specific import approach. | ||
|
|
||
| // We need to mock the dependencies before importing the module under test | ||
| vi.mock('@clerk/backend', () => ({ | ||
| createClerkClient: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('dotenv', () => ({ | ||
| default: { config: vi.fn() }, | ||
| })); | ||
|
|
||
| vi.mock('@clerk/shared/keys', () => ({ | ||
| parsePublishableKey: vi.fn(() => ({ frontendApi: 'clerk.test.lcl.dev' })), | ||
| })); | ||
|
|
||
| import { createClerkClient } from '@clerk/backend'; | ||
|
|
||
| import { fetchEnvVars } from '../setup'; | ||
|
|
||
| function createClerkAPIError(status: number, retryAfter?: number) { | ||
| return new ClerkAPIResponseError('API error', { | ||
| data: [], | ||
| status, | ||
| retryAfter, | ||
| }); | ||
| } | ||
|
|
||
| function createNetworkError(code: string) { | ||
| const err = new Error(`connect ${code}`); | ||
| (err as NodeJS.ErrnoException).code = code; | ||
| return err; | ||
| } | ||
|
|
||
| describe('fetchWithRetry (via fetchEnvVars)', () => { | ||
| const mockCreateTestingToken = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.stubEnv('CLERK_PUBLISHABLE_KEY', 'pk_test_abc'); | ||
| vi.stubEnv('CLERK_SECRET_KEY', 'sk_test_abc'); | ||
| delete process.env.CLERK_TESTING_TOKEN; | ||
|
|
||
| vi.mocked(createClerkClient).mockReturnValue({ | ||
| testingTokens: { createTestingToken: mockCreateTestingToken }, | ||
| } as any); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| vi.unstubAllEnvs(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('returns on first success without retrying', async () => { | ||
| mockCreateTestingToken.mockResolvedValueOnce({ token: 'test-token' }); | ||
|
|
||
| const result = await fetchEnvVars({ dotenv: false }); | ||
|
|
||
| expect(result.CLERK_TESTING_TOKEN).toBe('test-token'); | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('retries on 429 and succeeds', async () => { | ||
| mockCreateTestingToken | ||
| .mockRejectedValueOnce(createClerkAPIError(429)) | ||
| .mockResolvedValueOnce({ token: 'test-token' }); | ||
|
|
||
| const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| const promise = fetchEnvVars({ dotenv: false }); | ||
| await vi.advanceTimersByTimeAsync(30_000); | ||
| const result = await promise; | ||
|
|
||
| expect(result.CLERK_TESTING_TOKEN).toBe('test-token'); | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(2); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy.mock.calls[0][0]).toContain('[Retry] 429'); | ||
| expect(warnSpy.mock.calls[0][0]).toContain('attempt 1/5'); | ||
| }); | ||
|
|
||
| it.each([408, 500, 502, 503, 504])('retries on %i status code', async status => { | ||
| mockCreateTestingToken | ||
| .mockRejectedValueOnce(createClerkAPIError(status)) | ||
| .mockResolvedValueOnce({ token: 'test-token' }); | ||
|
|
||
| vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| const promise = fetchEnvVars({ dotenv: false }); | ||
| await vi.advanceTimersByTimeAsync(30_000); | ||
| const result = await promise; | ||
|
|
||
| expect(result.CLERK_TESTING_TOKEN).toBe('test-token'); | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('does not retry on non-retryable status codes', async () => { | ||
| mockCreateTestingToken.mockRejectedValueOnce(createClerkAPIError(401)); | ||
| vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
|
||
| await expect(fetchEnvVars({ dotenv: false })).rejects.toThrow('API error'); | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('throws after max retries exhausted', async () => { | ||
| mockCreateTestingToken.mockImplementation(() => Promise.reject(createClerkAPIError(429))); | ||
|
|
||
| vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
|
||
| const promise = fetchEnvVars({ dotenv: false }).catch(e => e); | ||
|
|
||
| await vi.runAllTimersAsync(); | ||
|
|
||
| const error = await promise; | ||
| expect(error).toBeInstanceOf(ClerkAPIResponseError); | ||
| expect(error.status).toBe(429); | ||
| // 1 initial + 5 retries = 6 total calls | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(6); | ||
| }); | ||
|
|
||
| it('uses retryAfter from error when available', async () => { | ||
| mockCreateTestingToken | ||
| .mockRejectedValueOnce(createClerkAPIError(429, 2)) | ||
| .mockResolvedValueOnce({ token: 'test-token' }); | ||
|
|
||
| const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| const promise = fetchEnvVars({ dotenv: false }); | ||
|
|
||
| // retryAfter is 2 seconds = 2000ms | ||
| await vi.advanceTimersByTimeAsync(2000); | ||
| const result = await promise; | ||
|
|
||
| expect(result.CLERK_TESTING_TOKEN).toBe('test-token'); | ||
| expect(warnSpy.mock.calls[0][0]).toContain('waiting 2000ms'); | ||
| }); | ||
|
|
||
| it('caps retryAfter delay at MAX_RETRY_DELAY_MS', async () => { | ||
| mockCreateTestingToken | ||
| .mockRejectedValueOnce(createClerkAPIError(429, 60)) | ||
| .mockResolvedValueOnce({ token: 'test-token' }); | ||
|
|
||
| const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| const promise = fetchEnvVars({ dotenv: false }); | ||
| await vi.advanceTimersByTimeAsync(30_000); | ||
| const result = await promise; | ||
|
|
||
| expect(result.CLERK_TESTING_TOKEN).toBe('test-token'); | ||
| // 60s * 1000 = 60000ms, capped to 30000ms | ||
| expect(warnSpy.mock.calls[0][0]).toContain('waiting 30000ms'); | ||
| }); | ||
|
|
||
| it.each(['ECONNREFUSED', 'ECONNRESET', 'ENOTFOUND', 'ETIMEDOUT', 'EAI_AGAIN'])( | ||
| 'retries on network error %s', | ||
| async code => { | ||
| mockCreateTestingToken | ||
| .mockRejectedValueOnce(createNetworkError(code)) | ||
| .mockResolvedValueOnce({ token: 'test-token' }); | ||
|
|
||
| const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| const promise = fetchEnvVars({ dotenv: false }); | ||
| await vi.advanceTimersByTimeAsync(30_000); | ||
| const result = await promise; | ||
|
|
||
| expect(result.CLERK_TESTING_TOKEN).toBe('test-token'); | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(2); | ||
| expect(warnSpy.mock.calls[0][0]).toContain(`[Retry] ${code}`); | ||
| }, | ||
| ); | ||
|
|
||
| it('does not retry on non-network errors', async () => { | ||
| mockCreateTestingToken.mockRejectedValueOnce(new TypeError('unexpected')); | ||
| vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
|
||
| await expect(fetchEnvVars({ dotenv: false })).rejects.toThrow('unexpected'); | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not retry when non-retryable error code is present', async () => { | ||
| const err = new Error('unknown'); | ||
| (err as NodeJS.ErrnoException).code = 'EPERM'; | ||
| vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
|
||
| mockCreateTestingToken.mockRejectedValueOnce(err); | ||
|
|
||
| await expect(fetchEnvVars({ dotenv: false })).rejects.toThrow('unknown'); | ||
| expect(mockCreateTestingToken).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('skips retry when CLERK_TESTING_TOKEN is already set', async () => { | ||
| vi.stubEnv('CLERK_TESTING_TOKEN', 'existing-token'); | ||
|
|
||
| const result = await fetchEnvVars({ dotenv: false }); | ||
|
|
||
| expect(result.CLERK_TESTING_TOKEN).toBe('existing-token'); | ||
| expect(mockCreateTestingToken).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| watch: false, | ||
| include: ['**/*.{test,spec}.{ts,tsx}'], | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.