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
31 changes: 28 additions & 3 deletions workers/main/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ describe('handleRunError', () => {
let processExitSpy: ReturnType<typeof vi.spyOn>;

beforeEach(() => {
// Use fake timers to control setTimeout in handleRunError
vi.useFakeTimers();
// Mock process.exit to prevent actual process termination during tests
processExitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
throw new Error('process.exit called');
});
processExitSpy = vi
.spyOn(process, 'exit')
.mockImplementation((() => {}) as never);
});

afterEach(() => {
// Run all pending timers before restoring mocks to prevent race condition
vi.runAllTimers();
processExitSpy.mockRestore();
// Restore real timers
vi.useRealTimers();
});

it('should log the error', () => {
const error = new Error('test error');
const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {});

handleRunError(error);

expect(logSpy).toHaveBeenCalledWith(
`Error in main worker: ${error.message}`,
);
Expand All @@ -46,4 +53,22 @@ describe('handleRunError', () => {
);
logSpy.mockRestore();
});

it('should call process.exit with code 1 after timeout', () => {
const error = new Error('test error');
const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {});

handleRunError(error);

// Process.exit should not be called immediately
expect(processExitSpy).not.toHaveBeenCalled();

// Fast-forward time by 100ms
vi.advanceTimersByTime(100);

// Now process.exit should have been called
expect(processExitSpy).toHaveBeenCalledWith(1);

logSpy.mockRestore();
});
});