diff --git a/workers/main/src/index.test.ts b/workers/main/src/index.test.ts index a42c242..4f5af83 100644 --- a/workers/main/src/index.test.ts +++ b/workers/main/src/index.test.ts @@ -6,14 +6,20 @@ describe('handleRunError', () => { let processExitSpy: ReturnType; 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', () => { @@ -21,6 +27,7 @@ describe('handleRunError', () => { const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {}); handleRunError(error); + expect(logSpy).toHaveBeenCalledWith( `Error in main worker: ${error.message}`, ); @@ -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(); + }); });