import { casefy } from '../core/casefy.core'; import { CasefyService } from '../services/casefy.service'; import { CasefyOptions } from '../types/case.types'; describe('Error Handling Branches for Branch Coverage', () => { describe('Core function error handling branches', () => { it('should cover error handling branch in casefy.core.ts (line 62)', () => { // Test the error handling path that's hard to reach const options: CasefyOptions = { from: 'camelCase', to: 'snake_case', }; // Test with various inputs that might cause internal errors const problematicInputs = [ // Circular reference (() => { const obj: any = { test: 'value' }; obj.self = obj; return obj; })(), // Very deep nesting that might cause stack overflow (() => { let deep: any = { level: 0 }; for (let i = 1; i < 1000; i++) { deep = { level: i, nested: deep }; } return deep; })(), // Large object that might cause memory issues (() => { const large: any = {}; for (let i = 0; i < 10000; i++) { large[`key${i}`] = `value${i}`; } return large; })(), ]; problematicInputs.forEach((input) => { // These should not throw errors and should return gracefully const result = casefy(input, options); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); expect(result).toHaveProperty('from'); expect(result).toHaveProperty('to'); expect(result).toHaveProperty('deep'); expect(result).toHaveProperty('arrays'); }); }); it('should cover specific error types in core function', () => { const options: CasefyOptions = { from: 'camelCase', to: 'snake_case', }; // Test with inputs that might cause specific error types const errorInputs = [ new Error('test error'), new Date(), new RegExp('test'), new Map(), new Set(), Symbol('test'), BigInt(123), () => {}, class TestClass {}, new Array(1000000).fill('test'), ]; errorInputs.forEach((input) => { const result = casefy(input, options); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); }); }); }); describe('Service error handling branches', () => { it('should cover error handling branch in casefy.service.ts (line 63)', () => { const service = new CasefyService('camelCase', 'snake_case'); // Test with inputs that might cause transformation errors const errorInputs = [ // Circular reference (() => { const obj: any = { test: 'value' }; obj.self = obj; return obj; })(), // Very deep nesting (() => { let deep: any = { level: 0 }; for (let i = 1; i < 100; i++) { deep = { level: i, nested: deep }; } return deep; })(), // Large arrays new Array(10000).fill({ test: 'value' }), // Complex nested structures { level1: { level2: { level3: { level4: { level5: { test: 'deep value', }, }, }, }, }, }, ]; errorInputs.forEach((input) => { const result = service.transform(input); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); expect(result).toHaveProperty('fromCase'); expect(result).toHaveProperty('toCase'); }); }); it('should cover service error handling with complex options', () => { const service = new CasefyService('snake_case', 'camelCase', { deep: true, arrays: true, preserveTypes: false, fieldMappings: { user_id: 'id', profile_data: 'profile', }, excludeFields: ['secret'], includeFields: ['name', 'email'], }); // Test with complex inputs that might cause errors const complexInputs = [ { user_id: 123, user_name: 'John', user_email: 'john@example.com', secret: 'hidden', profile_data: { first_name: 'John', last_name: 'Doe', settings: { theme: 'dark', notifications: true, }, }, tags: ['admin', 'user'], metadata: { created_at: new Date(), updated_at: new Date(), version: 1.0, }, }, [ { user_id: 1, name: 'User 1' }, { user_id: 2, name: 'User 2' }, { user_id: 3, name: 'User 3' }, ], { data: { users: [ { id: 1, profile: { name: 'John' } }, { id: 2, profile: { name: 'Jane' } }, ], }, }, ]; complexInputs.forEach((input) => { const result = service.transform(input); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); }); }); }); describe('Type checking error branches', () => { it('should cover type checking branches in transformers', () => { const service = new CasefyService('camelCase', 'snake_case'); // Test with various types that might cause type checking issues const typeInputs = [ null, undefined, true, false, 0, 123, -123, Infinity, -Infinity, NaN, '', 'string', [], {}, new Date(), new RegExp('test'), new Map(), new Set(), Symbol('test'), BigInt(123), () => {}, class TestClass {}, new Error('test'), new Array(1000).fill('test'), ]; typeInputs.forEach((input) => { const result = service.transform(input); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); }); }); }); describe('Field mapping error branches', () => { it('should cover field mapping error branches', () => { const service = new CasefyService('snake_case', 'camelCase', { fieldMappings: { user_id: 'id', profile_data: 'profile', nested_field: 'nestedField', }, }); // Test with inputs that might cause field mapping errors const mappingInputs = [ { user_id: 123, profile_data: { first_name: 'John', last_name: 'Doe', }, nested_field: { deep_nested: 'value', }, }, { user_id: null, profile_data: undefined, nested_field: [], }, { user_id: new Date(), profile_data: new RegExp('test'), nested_field: new Map(), }, ]; mappingInputs.forEach((input) => { const result = service.transform(input); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); }); }); }); describe('Include/Exclude field error branches', () => { it('should cover include/exclude field error branches', () => { const service = new CasefyService('snake_case', 'camelCase', { includeFields: ['user_name', 'user_email'], excludeFields: ['secret', 'internal_data'], }); const fieldInputs = [ { user_name: 'John', user_email: 'john@example.com', secret: 'hidden', internal_data: 'confidential', other_field: 'value', }, { user_name: null, user_email: undefined, secret: '', internal_data: 0, other_field: false, }, ]; fieldInputs.forEach((input) => { const result = service.transform(input); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); }); }); }); describe('Deep transformation error branches', () => { it('should cover deep transformation error branches', () => { const service = new CasefyService('snake_case', 'camelCase', { deep: true, arrays: true, preserveTypes: true, }); const deepInputs = [ { level1: { level2: { level3: { level4: { level5: { test: 'deep value', }, }, }, }, }, }, { array: [{ nested: { value: 1 } }, { nested: { value: 2 } }, { nested: { value: 3 } }], }, { mixed: { primitive: 'value', array: [1, 2, 3], object: { nested: 'value' }, null: null, undefined: undefined, }, }, ]; deepInputs.forEach((input) => { const result = service.transform(input); expect(result).toHaveProperty('data'); expect(result).toHaveProperty('transformedKeys'); }); }); }); });