Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

env.test.ts

28 lines943 Bytes

1import { describe, it, expect, vi, afterEach } from 'vitest'
2import { getApiKeyFromEnv } from '../src/env'
3​
4describe('getApiKeyFromEnv', () => {
5 afterEach(() => {
6 vi.unstubAllEnvs()
7 })
8​
9 it('should return the API key from process.env', () => {
10 vi.stubEnv('TEST_API_KEY', 'sk-test-123')
11 expect(getApiKeyFromEnv('TEST_API_KEY')).toBe('sk-test-123')
12 })
13​
14 it('should throw if the env var is not set', () => {
15 const missingKey = `__AI_UTILS_TEST_MISSING_${Date.now()}__`
16 expect(() => getApiKeyFromEnv(missingKey)).toThrow(missingKey)
17 })
18​
19 it('should throw if the env var is empty string', () => {
20 vi.stubEnv('EMPTY_KEY', '')
21 expect(() => getApiKeyFromEnv('EMPTY_KEY')).toThrow('EMPTY_KEY')
22 })
23​
24 it('should include the env var name in the error message', () => {
25 const providerKey = `__AI_UTILS_TEST_PROVIDER_${Date.now()}__`
26 expect(() => getApiKeyFromEnv(providerKey)).toThrow(providerKey)
27 })
28})