import { env } from 'node:process'; import { describe, it, expect } from 'vitest'; import { validateEnvVars } from '../src/index.js'; describe('validateEnvVars', () => { it('should read string variables from the environment', () => { env.TEST_VAR_1 = 'success'; env.TEST_VAR_2 = 'also success'; env.TEST_VAR_3 = 'overly complicated success'; const { TEST_VAR_1, TEST_VAR_2, TEST_VAR_3 } = validateEnvVars( 'TEST_VAR_1', 'TEST_VAR_2', ['TEST_VAR_3', 'string'] ); expect(TEST_VAR_1).toBe(env.TEST_VAR_1); expect(TEST_VAR_2).toBe(env.TEST_VAR_2); expect(TEST_VAR_3).toBe(env.TEST_VAR_3); }); it('should read number variables from the environment', () => { env.TEST_VAR_1 = '6'; env.TEST_VAR_2 = '7'; const { TEST_VAR_1, TEST_VAR_2 } = validateEnvVars( ['TEST_VAR_1', 'number'], ['TEST_VAR_2', 'number'] ); expect(TEST_VAR_1).toBe(6); expect(TEST_VAR_2).toBe(7); }); it('should read boolean variables from the environment', () => { env.TEST_VAR_1 = 'true'; env.TEST_VAR_2 = 'True'; env.TEST_VAR_3 = 'TRUE'; env.TEST_VAR_4 = 'false'; env.TEST_VAR_5 = 'False'; env.TEST_VAR_6 = 'FALSE'; const { TEST_VAR_1, TEST_VAR_2, TEST_VAR_3, TEST_VAR_4, TEST_VAR_5, TEST_VAR_6 } = validateEnvVars( ['TEST_VAR_1', 'boolean'], ['TEST_VAR_2', 'boolean'], ['TEST_VAR_3', 'boolean'], ['TEST_VAR_4', 'boolean'], ['TEST_VAR_5', 'boolean'], ['TEST_VAR_6', 'boolean'] ); expect(TEST_VAR_1).toBe(true); expect(TEST_VAR_2).toBe(true); expect(TEST_VAR_3).toBe(true); expect(TEST_VAR_4).toBe(false); expect(TEST_VAR_5).toBe(false); expect(TEST_VAR_6).toBe(false); }); it('should read mixed variables from the environment', () => { env.TEST_VAR_1 = 'true'; env.TEST_VAR_2 = '21'; env.TEST_VAR_3 = 'real string'; const { TEST_VAR_1, TEST_VAR_2, TEST_VAR_3 } = validateEnvVars( ['TEST_VAR_1', 'boolean'], ['TEST_VAR_2', 'number'], 'TEST_VAR_3' ); expect(TEST_VAR_1).toBe(true); expect(TEST_VAR_2).toBe(21); expect(TEST_VAR_3).toBe(env.TEST_VAR_3); }); it('should error on missing string variable', () => { expect(() => validateEnvVars('MISSING_TEST_VAR')).toThrow(); }); it('should error on blank string variable', () => { env.TEST_VAR_1 = ''; expect(() => validateEnvVars('TEST_VAR_1')).toThrow(); }); it('should error on invalid number variable', () => { env.TEST_VAR_1 = 'not a number'; expect(() => validateEnvVars(['TEST_VAR_1', 'number'])).toThrow(); }); it('should error on invalid boolean variable', () => { env.TEST_VAR_1 = 'not a boolean'; expect(() => validateEnvVars(['TEST_VAR_1', 'boolean'])).toThrow(); }); });