|
| 1 | +import * as getEnvVariableModule from '@clerk/shared/getEnvVariable'; |
| 2 | +import type { IsomorphicClerkOptions } from '@clerk/shared/types'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { mergeWithEnv } from '../envVariables'; |
| 6 | + |
| 7 | +// Mock getEnvVariable to control env var behavior in tests |
| 8 | +vi.mock('@clerk/shared/getEnvVariable', () => ({ |
| 9 | + getEnvVariable: vi.fn(() => ''), |
| 10 | +})); |
| 11 | + |
| 12 | +describe('mergeWithEnv', () => { |
| 13 | + const mockedGetEnvVariable = vi.mocked(getEnvVariableModule.getEnvVariable); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('returns passed-in publishableKey when provided', () => { |
| 20 | + mockedGetEnvVariable.mockReturnValue('should_not_be_used'); |
| 21 | + |
| 22 | + const options: IsomorphicClerkOptions = { |
| 23 | + publishableKey: 'pk_test_explicit', |
| 24 | + }; |
| 25 | + |
| 26 | + const result = mergeWithEnv(options); |
| 27 | + |
| 28 | + expect(result.publishableKey).toBe('pk_test_explicit'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('falls back to VITE_CLERK_PUBLISHABLE_KEY env var when option is undefined', () => { |
| 32 | + mockedGetEnvVariable.mockImplementation((name: string) => { |
| 33 | + if (name === 'VITE_CLERK_PUBLISHABLE_KEY') { |
| 34 | + return 'pk_test_vite'; |
| 35 | + } |
| 36 | + return ''; |
| 37 | + }); |
| 38 | + |
| 39 | + const result = mergeWithEnv({} as any); |
| 40 | + |
| 41 | + expect(result.publishableKey).toBe('pk_test_vite'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('falls back to CLERK_PUBLISHABLE_KEY when VITE_ prefixed not set', () => { |
| 45 | + mockedGetEnvVariable.mockImplementation((name: string) => { |
| 46 | + if (name === 'CLERK_PUBLISHABLE_KEY') { |
| 47 | + return 'pk_test_node'; |
| 48 | + } |
| 49 | + return ''; |
| 50 | + }); |
| 51 | + |
| 52 | + const result = mergeWithEnv({} as any); |
| 53 | + |
| 54 | + expect(result.publishableKey).toBe('pk_test_node'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('prioritizes VITE_ prefixed env var over non-prefixed', () => { |
| 58 | + mockedGetEnvVariable.mockImplementation((name: string) => { |
| 59 | + const envVars: Record<string, string> = { |
| 60 | + VITE_CLERK_PUBLISHABLE_KEY: 'pk_test_vite', |
| 61 | + CLERK_PUBLISHABLE_KEY: 'pk_test_node', |
| 62 | + }; |
| 63 | + return envVars[name] || ''; |
| 64 | + }); |
| 65 | + |
| 66 | + const result = mergeWithEnv({} as any); |
| 67 | + |
| 68 | + expect(result.publishableKey).toBe('pk_test_vite'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('does NOT fall back when publishableKey is empty string (framework SDK behavior)', () => { |
| 72 | + mockedGetEnvVariable.mockReturnValue('pk_test_vite'); |
| 73 | + |
| 74 | + const result = mergeWithEnv({ |
| 75 | + publishableKey: '', |
| 76 | + }); |
| 77 | + |
| 78 | + // Should preserve empty string, not fall back to env var |
| 79 | + expect(result.publishableKey).toBe(''); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns undefined publishableKey when neither option nor env var is set', () => { |
| 83 | + mockedGetEnvVariable.mockReturnValue(''); |
| 84 | + |
| 85 | + const result = mergeWithEnv({} as any); |
| 86 | + |
| 87 | + // When env var is not set, we don't add the property |
| 88 | + expect(result.publishableKey).toBeUndefined(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('preserves other options that are not env-var backed', () => { |
| 92 | + mockedGetEnvVariable.mockReturnValue(''); |
| 93 | + |
| 94 | + const options: IsomorphicClerkOptions = { |
| 95 | + publishableKey: 'pk_test', |
| 96 | + appearance: { variables: { colorPrimary: 'red' } }, |
| 97 | + localization: { signIn: { start: { title: 'Hello' } } }, |
| 98 | + signInUrl: '/custom-sign-in', |
| 99 | + signUpUrl: '/custom-sign-up', |
| 100 | + }; |
| 101 | + |
| 102 | + const result = mergeWithEnv(options); |
| 103 | + |
| 104 | + expect(result.publishableKey).toBe('pk_test'); |
| 105 | + expect(result.appearance).toEqual({ variables: { colorPrimary: 'red' } }); |
| 106 | + expect(result.localization).toEqual({ signIn: { start: { title: 'Hello' } } }); |
| 107 | + expect(result.signInUrl).toBe('/custom-sign-in'); |
| 108 | + expect(result.signUpUrl).toBe('/custom-sign-up'); |
| 109 | + }); |
| 110 | +}); |
0 commit comments