|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 | import { vi } from 'vitest'; |
3 | 3 |
|
4 | | -import { handleRunError, logger, run } from '../index'; |
| 4 | +import * as utils from '../../../common/utils'; |
| 5 | +import { handleRunError, run } from '../index'; |
| 6 | + |
| 7 | +vi.mock('@temporalio/worker', () => ({ |
| 8 | + DefaultLogger: class { |
| 9 | + error() {} |
| 10 | + }, |
| 11 | + NativeConnection: { |
| 12 | + connect: vi.fn().mockResolvedValue({ close: vi.fn() }), |
| 13 | + }, |
| 14 | + Worker: { |
| 15 | + create: vi |
| 16 | + .fn() |
| 17 | + .mockResolvedValue({ run: vi.fn().mockResolvedValue(undefined) }), |
| 18 | + }, |
| 19 | +})); |
5 | 20 |
|
6 | 21 | describe('run', () => { |
7 | 22 | it('should return true', async () => { |
8 | | - await expect(run()).resolves.toBe(true); |
| 23 | + await expect(run()).resolves.toBeUndefined(); |
9 | 24 | }); |
10 | 25 | }); |
11 | 26 |
|
12 | 27 | describe('handleRunError', () => { |
13 | | - it('should log error and exit process', () => { |
14 | | - vi.useFakeTimers(); |
15 | | - const error = new Error('test error'); |
16 | | - const loggerErrorSpy = vi |
17 | | - .spyOn(logger, 'error') |
| 28 | + it('should log the error and throw the error', () => { |
| 29 | + const logSpy = vi |
| 30 | + .spyOn(utils, 'logWorkerError') |
18 | 31 | .mockImplementation(() => {}); |
19 | | - const processExitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { |
20 | | - throw new Error('exit'); |
21 | | - }); |
| 32 | + const error = new Error('test error'); |
22 | 33 |
|
23 | 34 | expect(() => handleRunError(error)).toThrow(error); |
24 | | - expect(loggerErrorSpy).toHaveBeenCalledWith( |
25 | | - `Unhandled error in main: ${error.message}`, |
26 | | - ); |
27 | | - expect(processExitSpy).not.toHaveBeenCalled(); |
28 | | - expect(() => { |
29 | | - vi.runAllTimers(); |
30 | | - }).toThrow('exit'); |
31 | | - expect(processExitSpy).toHaveBeenCalledWith(1); |
32 | | - |
33 | | - loggerErrorSpy.mockRestore(); |
34 | | - processExitSpy.mockRestore(); |
35 | | - vi.useRealTimers(); |
| 35 | + expect(logSpy).toHaveBeenCalledWith('main', error); |
| 36 | + logSpy.mockRestore(); |
36 | 37 | }); |
37 | 38 | }); |
0 commit comments