Files
dotenv-types/tests/generator.test.ts
7000pctAUTO 9cd8d65448
Some checks are pending
CI / test (push) Has started running
Initial upload with CI/CD workflow
2026-01-31 20:27:45 +00:00

47 lines
1.7 KiB
TypeScript

import * as path from 'path';
import { generateTypeScriptInterface } from '../src/utils/typeGenerator';
import { parseEnvFile } from '../src/core/parser';
import { parseSchemaFromEnv } from '../src/core/validator';
describe('TypeGenerator', () => {
const fixturesDir = path.join(__dirname, 'fixtures');
test('generates TypeScript interface from schema', () => {
const envPath = path.join(fixturesDir, '.env');
const parsed = parseEnvFile(envPath);
const schema = parseSchemaFromEnv(parsed.flat);
const result = generateTypeScriptInterface(schema, parsed, 'TestEnv');
expect(result).toContain('export interface TestEnv {');
expect(result).toContain('PORT: number;');
expect(result).toContain('NODE_ENV: string;');
expect(result).toContain('DEBUG: boolean;');
});
test('generates nested interfaces for underscore-prefixed variables', () => {
const envPath = path.join(fixturesDir, '.env');
const parsed = parseEnvFile(envPath);
const schema = parseSchemaFromEnv(parsed.flat);
const result = generateTypeScriptInterface(schema, parsed, 'TestEnv');
expect(result).toContain('export interface Db {');
expect(result).toContain('HOST?: string;');
expect(result).toContain('PORT?: string;');
expect(result).toContain('USER?: string;');
});
test('generates proper types for different value types', () => {
const envPath = path.join(fixturesDir, '.env');
const parsed = parseEnvFile(envPath);
const schema = parseSchemaFromEnv(parsed.flat);
const result = generateTypeScriptInterface(schema, parsed, 'TestEnv');
expect(result).toMatch(/PORT(?:\?|): number/);
expect(result).toMatch(/NODE_ENV(?:\?|): string/);
expect(result).toMatch(/DEBUG(?:\?|): boolean/);
});
});