diff --git a/sonar-project.properties b/sonar-project.properties index dbeda9a..ec62924 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,4 +1,4 @@ sonar.projectKey=speedandfunction_automatization sonar.organization=speedandfunction sonar.javascript.lcov.reportPaths=workers/main/coverage/lcov.info -sonar.exclusions=**/src/__tests__/**,**/src/dist/** +sonar.exclusions=**/*.test.ts,**/src/dist/** diff --git a/workers/main/src/__tests__/index.test.ts b/workers/main/src/__tests__/index.test.ts deleted file mode 100644 index b03f648..0000000 --- a/workers/main/src/__tests__/index.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { vi } from 'vitest'; - -import * as utils from '../../../common/utils'; -import { handleRunError, run } from '../index'; - -vi.mock('@temporalio/worker', () => ({ - DefaultLogger: class { - error() {} - }, - NativeConnection: { - connect: vi.fn().mockResolvedValue({ close: vi.fn() }), - }, - Worker: { - create: vi - .fn() - .mockResolvedValue({ run: vi.fn().mockResolvedValue(undefined) }), - }, -})); - -describe('run', () => { - it('should return true', async () => { - await expect(run()).resolves.toBeUndefined(); - }); -}); - -describe('handleRunError', () => { - it('should log the error and throw the error', () => { - const logSpy = vi - .spyOn(utils, 'logWorkerError') - .mockImplementation(() => {}); - const error = new Error('test error'); - - expect(() => handleRunError(error)).toThrow(error); - expect(logSpy).toHaveBeenCalledWith('main', error); - logSpy.mockRestore(); - }); -}); diff --git a/workers/main/src/index.test.ts b/workers/main/src/index.test.ts new file mode 100644 index 0000000..81fcac8 --- /dev/null +++ b/workers/main/src/index.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it, vi } from 'vitest'; + +import { handleRunError, logger } from './index'; + +describe('handleRunError', () => { + 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}`, + ); + logSpy.mockRestore(); + }); + + it('should log when error is a string', () => { + const error = 'string error'; + const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {}); + + handleRunError(error); + expect(logSpy).toHaveBeenCalledWith(`Error in main worker: ${error}`); + logSpy.mockRestore(); + }); + + it('should log when error is an Error object', () => { + const error = new Error('error from Error object'); + const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {}); + + handleRunError(error); + expect(logSpy).toHaveBeenCalledWith( + `Error in main worker: ${error.message}`, + ); + logSpy.mockRestore(); + }); +}); diff --git a/workers/main/src/index.ts b/workers/main/src/index.ts index 178664b..49aba97 100644 --- a/workers/main/src/index.ts +++ b/workers/main/src/index.ts @@ -1,6 +1,6 @@ import { DefaultLogger, NativeConnection, Worker } from '@temporalio/worker'; -import { logWorkerError, validateEnv } from '../../common/utils'; +import { validateEnv } from './common/utils'; import { temporalConfig } from './configs/temporal'; import { workerConfig } from './configs/worker'; @@ -37,10 +37,11 @@ export async function run(): Promise { } } -export function handleRunError(err: unknown): never { - logWorkerError('main', err); +export function handleRunError(error: unknown): void { + logger.error( + `Error in main worker: ${error instanceof Error ? error.message : String(error)}`, + ); setTimeout(() => process.exit(1), 100); - throw err; } export function mainEntry() { diff --git a/workers/main/vitest.config.ts b/workers/main/vitest.config.ts index 7f365f6..0415bc7 100644 --- a/workers/main/vitest.config.ts +++ b/workers/main/vitest.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ test: { globals: true, environment: 'node', - include: ['src/__tests__/**/*.test.ts'], + include: ['src/**/*.test.ts'], coverage: { provider: 'v8', reporter: ['text', 'lcov'],