Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions packages/utils/test/executeSafePromise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,79 @@ describe('executeSafePromise', () => {
})
expect(result).toBe(123)
})

describe('error handling', () => {
describe('Node/Electron environment', () => {
const originalProcessExit = process.exit

beforeEach(() => {
// Mock process.exit to prevent actual exit during tests
process.exit = jest.fn() as any
})

afterEach(() => {
// Restore original process.exit
process.exit = originalProcessExit
})

it('should call process.exit(1) when promise rejects', async () => {
const testError = new Error('Test error')

await executeSafePromise(async () => {
throw testError
})

expect(process.exit).toHaveBeenCalledWith(1)
})

it('should log fatal error before exiting', async () => {
// We can't easily test the logger output without mocking it,
// but we can verify process.exit is called
const testError = new Error('Fatal test error')

await executeSafePromise(async () => {
throw testError
})

expect(process.exit).toHaveBeenCalled()
})
})

describe('browser environment', () => {
const originalProcess = global.process

beforeEach(() => {
// Simulate browser environment by removing process.exit
// @ts-expect-error - intentionally deleting for test
delete (global as any).process.exit
})

afterEach(() => {
// Restore process
global.process = originalProcess
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global process.exit remains deleted after browser tests

In the browser environment test block, process.exit is deleted from the global process object. Since originalProcess is a reference to the same object, assigning it back in afterEach fails to restore the deleted property. This leaves process.exit as undefined for subsequent tests, potentially breaking other parts of the test suite.

Fix in Cursor Fix in Web


it('should throw error with proper error chaining when promise rejects', async () => {
const testError = new Error('Test error in browser')

await expect(executeSafePromise(async () => {
throw testError
})).rejects.toThrow('executeSafePromise: Assertion failure!')
})

it('should chain the original error as cause', async () => {
const originalError = new Error('Original error')

try {
await executeSafePromise(async () => {
throw originalError
})
fail('Should have thrown an error')
} catch (err: any) {
expect(err.message).toBe('executeSafePromise: Assertion failure!')
expect(err.cause).toBe(originalError)
}
})
})
})
})