This commit is contained in:
46
tests/generator.test.ts
Normal file
46
tests/generator.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user