This commit is contained in:
70
tests/core.test.ts
Normal file
70
tests/core.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import * as path from 'path';
|
||||
import { parseEnvFile } from '../src/core/parser';
|
||||
import { validateEnv, parseSchemaFromEnv } from '../src/core/validator';
|
||||
|
||||
describe('Parser', () => {
|
||||
const fixturesDir = path.join(__dirname, 'fixtures');
|
||||
|
||||
test('parses .env file correctly', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
const result = parseEnvFile(envPath);
|
||||
|
||||
expect(result.flat).toBeDefined();
|
||||
expect(result.flat.PORT).toBe('3000');
|
||||
expect(result.flat.NODE_ENV).toBe('development');
|
||||
expect(result.flat.DEBUG).toBe('true');
|
||||
});
|
||||
|
||||
test('creates nested structure for underscore-prefixed variables', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
const result = parseEnvFile(envPath);
|
||||
|
||||
expect(result.nested.DB).toBeDefined();
|
||||
expect((result.nested.DB as Record<string, unknown>).HOST).toBe('localhost');
|
||||
expect((result.nested.DB as Record<string, unknown>).PORT).toBe('5432');
|
||||
expect((result.nested.DB as Record<string, unknown>).USER).toBe('admin');
|
||||
});
|
||||
|
||||
test('throws error for non-existent file', () => {
|
||||
expect(() => parseEnvFile('/non/existent/file.env')).toThrow('not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Validator', () => {
|
||||
const fixturesDir = path.join(__dirname, 'fixtures');
|
||||
|
||||
test('validates correct .env against schema', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
const parsed = parseEnvFile(envPath);
|
||||
const schemaPath = path.join(fixturesDir, 'schema.json');
|
||||
const schema = JSON.parse(require('fs').readFileSync(schemaPath, 'utf-8'));
|
||||
|
||||
const result = validateEnv(parsed.flat, schema);
|
||||
|
||||
expect(result.valid).toBe(true);
|
||||
expect(result.errors).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('fails validation for invalid values', () => {
|
||||
const envPath = path.join(fixturesDir, 'invalid.env');
|
||||
const parsed = parseEnvFile(envPath);
|
||||
const schemaPath = path.join(fixturesDir, 'schema.json');
|
||||
const schema = JSON.parse(require('fs').readFileSync(schemaPath, 'utf-8'));
|
||||
|
||||
const result = validateEnv(parsed.flat, schema);
|
||||
|
||||
expect(result.valid).toBe(false);
|
||||
expect(result.errors.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test('parses schema from .env file', () => {
|
||||
const envPath = path.join(fixturesDir, '.env');
|
||||
const parsed = parseEnvFile(envPath);
|
||||
const schema = parseSchemaFromEnv(parsed.flat);
|
||||
|
||||
expect(schema.variables).toBeDefined();
|
||||
expect(schema.variables.MAX_RETRIES.type).toBe('int');
|
||||
expect(schema.variables.NODE_ENV.type).toBe('string');
|
||||
expect(schema.variables.DEBUG.type).toBe('boolean');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user