Skip to content

Commit 11b4838

Browse files
Copilotmondoreale
andcommitted
Add comprehensive test coverage for executeSafePromise error handling
Co-authored-by: mondoreale <[email protected]>
1 parent 78ece6d commit 11b4838

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

packages/utils/test/executeSafePromise.test.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,108 @@ describe('executeSafePromise', () => {
99
})
1010
expect(result).toBe(123)
1111
})
12+
13+
describe('error handling', () => {
14+
describe('Node/Electron environment', () => {
15+
const originalProcessExit = process.exit
16+
17+
beforeEach(() => {
18+
// Mock process.exit to prevent actual exit during tests
19+
process.exit = jest.fn() as any
20+
})
21+
22+
afterEach(() => {
23+
// Restore original process.exit
24+
process.exit = originalProcessExit
25+
})
26+
27+
it('should call process.exit(1) when promise rejects', async () => {
28+
const testError = new Error('Test error')
29+
30+
await executeSafePromise(async () => {
31+
throw testError
32+
})
33+
34+
expect(process.exit).toHaveBeenCalledWith(1)
35+
})
36+
37+
it('should log fatal error before exiting', async () => {
38+
// We can't easily test the logger output without mocking it,
39+
// but we can verify process.exit is called
40+
const testError = new Error('Fatal test error')
41+
42+
await executeSafePromise(async () => {
43+
throw testError
44+
})
45+
46+
expect(process.exit).toHaveBeenCalled()
47+
})
48+
})
49+
50+
describe('browser environment', () => {
51+
const originalProcess = global.process
52+
53+
beforeEach(() => {
54+
// Simulate browser environment by removing process.exit
55+
// @ts-expect-error - intentionally deleting for test
56+
delete (global as any).process.exit
57+
})
58+
59+
afterEach(() => {
60+
// Restore process
61+
global.process = originalProcess
62+
})
63+
64+
it('should throw error with proper error chaining when promise rejects', async () => {
65+
const testError = new Error('Test error in browser')
66+
67+
await expect(executeSafePromise(async () => {
68+
throw testError
69+
})).rejects.toThrow('executeSafePromise: Assertion failure!')
70+
})
71+
72+
it('should chain the original error as cause', async () => {
73+
const originalError = new Error('Original error')
74+
75+
try {
76+
await executeSafePromise(async () => {
77+
throw originalError
78+
})
79+
fail('Should have thrown an error')
80+
} catch (err: any) {
81+
expect(err.message).toBe('executeSafePromise: Assertion failure!')
82+
expect(err.cause).toBe(originalError)
83+
}
84+
})
85+
})
86+
87+
describe('environment detection', () => {
88+
it('should detect Node environment when process.exit is defined', async () => {
89+
const mockExit = jest.fn() as any
90+
const originalProcessExit = process.exit
91+
process.exit = mockExit
92+
93+
await executeSafePromise(async () => {
94+
throw new Error('Test')
95+
})
96+
97+
expect(mockExit).toHaveBeenCalled()
98+
// eslint-disable-next-line require-atomic-updates
99+
process.exit = originalProcessExit
100+
})
101+
102+
it('should detect browser environment when process.exit is undefined', async () => {
103+
const originalProcess = global.process
104+
// @ts-expect-error - intentionally setting to undefined for test
105+
delete (global as any).process.exit
106+
107+
await expect(executeSafePromise(async () => {
108+
throw new Error('Test')
109+
})).rejects.toThrow('executeSafePromise: Assertion failure!')
110+
111+
// eslint-disable-next-line require-atomic-updates
112+
global.process = originalProcess
113+
})
114+
})
115+
})
12116
})

0 commit comments

Comments
 (0)