Skip to content

Commit 12e35f0

Browse files
Achieve 100% statement coverage with comprehensive error handling tests
- Add 3 edge case tests for Exception.ts stack capture error handling - Test error during stack capture (catch block coverage) - Test environments with no stack property (else branch coverage) - Test environments with empty stack (fallback path coverage) - Cover previously uncovered lines 62-65 in Exception.ts - Coverage improvement: 98.11% → 100% statements, 100% functions - Branch coverage improved to 96.29%
1 parent 0ca365e commit 12e35f0

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

tests/Exception.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,79 @@ describe('Exception', () => {
7777
// Should not be empty even in worst case
7878
expect(ex.stack!.length).toBeGreaterThan(10);
7979
});
80+
81+
it('should handle errors during stack capture', () => {
82+
// Create a scenario where stack capture might fail
83+
// We'll temporarily break Error constructor to trigger catch block
84+
const originalError = global.Error;
85+
86+
try {
87+
// Mock Error to throw during construction
88+
let mockCalled = false;
89+
global.Error = function(this: any) {
90+
mockCalled = true;
91+
throw new TypeError('Mocked error during stack capture');
92+
} as any;
93+
94+
const ex = new Exception('Error capture test');
95+
96+
// Should have fallback content even if stack capture fails
97+
expect(ex.stack).toBeTruthy();
98+
expect(ex.stack).toContain('Exception:');
99+
expect(ex.stack).toContain('Error capture test');
100+
101+
// Should contain the ultimate fallback message
102+
expect(ex.stack).toContain('(unable to capture stack trace)');
103+
104+
// Verify our mock was called
105+
expect(mockCalled).toBe(true);
106+
} finally {
107+
// Always restore original Error
108+
global.Error = originalError;
109+
}
110+
});
111+
112+
it('should handle environments with no stack property', () => {
113+
// Mock Error to return undefined stack
114+
const originalError = global.Error;
115+
116+
try {
117+
global.Error = function(this: any) {
118+
this.stack = undefined; // No stack property
119+
} as any;
120+
121+
const ex = new Exception('No stack test');
122+
123+
// Should use fallback message
124+
expect(ex.stack).toBeTruthy();
125+
expect(ex.stack).toContain('Exception:');
126+
expect(ex.stack).toContain('No stack test');
127+
expect(ex.stack).toContain('(stack trace not available in this environment)');
128+
} finally {
129+
global.Error = originalError;
130+
}
131+
});
132+
133+
it('should handle environments with empty stack', () => {
134+
// Mock Error to return empty stack
135+
const originalError = global.Error;
136+
137+
try {
138+
global.Error = function(this: any) {
139+
this.stack = ''; // Empty stack
140+
} as any;
141+
142+
const ex = new Exception('Empty stack test');
143+
144+
// Should use fallback message when stack is empty
145+
expect(ex.stack).toBeTruthy();
146+
expect(ex.stack).toContain('Exception:');
147+
expect(ex.stack).toContain('Empty stack test');
148+
expect(ex.stack).toContain('(stack trace not available in this environment)');
149+
} finally {
150+
global.Error = originalError;
151+
}
152+
});
80153
});
81154

82155
describe('ArgumentException', () => {

0 commit comments

Comments
 (0)