From 121cda271a1fbdd5da89b2e05059fc04a0c51f7c Mon Sep 17 00:00:00 2001 From: "anatoly.shipitz" Date: Mon, 2 Jun 2025 19:30:20 +0200 Subject: [PATCH 1/4] feat: Update SonarQube exclusions and add unit tests for main worker functions - Modified `sonar-project.properties` to exclude test files with the pattern `**/*.test.ts` instead of `**/src/__tests__/**`. - Introduced a new test file `index.test.ts` containing unit tests for the `run` and `handleRunError` functions, ensuring proper error handling and logging. These changes improve the project's test coverage and ensure that SonarQube analysis focuses on relevant source files. --- sonar-project.properties | 2 +- workers/main/src/{__tests__ => }/index.test.ts | 11 +++++------ workers/main/src/index.ts | 10 ++++++---- 3 files changed, 12 insertions(+), 11 deletions(-) rename workers/main/src/{__tests__ => }/index.test.ts (74%) diff --git a/sonar-project.properties b/sonar-project.properties index dbeda9a..94582fa 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=docker-compose.yml,**/*.test.ts,**/src/dist/** diff --git a/workers/main/src/__tests__/index.test.ts b/workers/main/src/index.test.ts similarity index 74% rename from workers/main/src/__tests__/index.test.ts rename to workers/main/src/index.test.ts index b03f648..3c6022e 100644 --- a/workers/main/src/__tests__/index.test.ts +++ b/workers/main/src/index.test.ts @@ -1,8 +1,7 @@ import { describe, expect, it } from 'vitest'; import { vi } from 'vitest'; -import * as utils from '../../../common/utils'; -import { handleRunError, run } from '../index'; +import { handleRunError, logger, run } from './index'; vi.mock('@temporalio/worker', () => ({ DefaultLogger: class { @@ -26,13 +25,13 @@ describe('run', () => { describe('handleRunError', () => { it('should log the error and throw the error', () => { - const logSpy = vi - .spyOn(utils, 'logWorkerError') - .mockImplementation(() => {}); const error = new Error('test error'); + const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {}); expect(() => handleRunError(error)).toThrow(error); - expect(logSpy).toHaveBeenCalledWith('main', 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..e62eeca 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,12 @@ export async function run(): Promise { } } -export function handleRunError(err: unknown): never { - logWorkerError('main', err); +export function handleRunError(error: unknown): never { + logger.error( + `Error in main worker: ${error instanceof Error ? error.message : String(error)}`, + ); setTimeout(() => process.exit(1), 100); - throw err; + throw error; } export function mainEntry() { From a93b05467e661e8d8b401f1998b0435ce85af60b Mon Sep 17 00:00:00 2001 From: "anatoly.shipitz" Date: Mon, 2 Jun 2025 19:36:44 +0200 Subject: [PATCH 2/4] fix: Update SonarQube exclusions in sonar-project.properties - Modified the `sonar-project.properties` file to remove the exclusion of `docker-compose.yml`, ensuring that only test files and distribution files are excluded from analysis. This change refines the SonarQube configuration to focus on relevant source files while maintaining coverage on necessary project components. --- sonar-project.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 94582fa..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=docker-compose.yml,**/*.test.ts,**/src/dist/** +sonar.exclusions=**/*.test.ts,**/src/dist/** From 0b9dd3d20f6358a55bd870e75847dffcf9926a76 Mon Sep 17 00:00:00 2001 From: "anatoly.shipitz" Date: Tue, 3 Jun 2025 15:51:13 +0200 Subject: [PATCH 3/4] fix: Update Vitest configuration and enhance error handling tests - Modified `vitest.config.ts` to include all test files in the `src` directory. - Refactored `index.test.ts` to improve error handling tests for the `handleRunError` function, ensuring proper logging for both string and Error object inputs. - Updated the `handleRunError` function in `index.ts` to remove the throw statement, aligning with the new logging behavior. These changes improve test coverage and ensure consistent error logging in the main worker functions. --- workers/main/src/index.test.ts | 27 +++++++++++++++++++++++---- workers/main/src/index.ts | 3 +-- workers/main/vitest.config.ts | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/workers/main/src/index.test.ts b/workers/main/src/index.test.ts index 3c6022e..517e406 100644 --- a/workers/main/src/index.test.ts +++ b/workers/main/src/index.test.ts @@ -1,5 +1,4 @@ -import { describe, expect, it } from 'vitest'; -import { vi } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { handleRunError, logger, run } from './index'; @@ -24,11 +23,31 @@ describe('run', () => { }); describe('handleRunError', () => { - it('should log the error and throw the error', () => { + it('should log the error', () => { const error = new Error('test error'); const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {}); - expect(() => handleRunError(error)).toThrow(error); + 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}`, ); diff --git a/workers/main/src/index.ts b/workers/main/src/index.ts index e62eeca..49aba97 100644 --- a/workers/main/src/index.ts +++ b/workers/main/src/index.ts @@ -37,12 +37,11 @@ export async function run(): Promise { } } -export function handleRunError(error: unknown): never { +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 error; } 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'], From 950d8b470a1eb4186201a7eb33231a2d0108f166 Mon Sep 17 00:00:00 2001 From: "anatoly.shipitz" Date: Tue, 3 Jun 2025 15:56:12 +0200 Subject: [PATCH 4/4] refactor: Simplify index.test.ts by removing unnecessary mocks and tests - Removed mocked dependencies for the `run` function in `index.test.ts` to streamline the test setup. - Retained focus on testing the `handleRunError` function, ensuring clarity and maintainability of the test file. These changes enhance the readability of the test suite and maintain focus on relevant functionality. --- workers/main/src/index.test.ts | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/workers/main/src/index.test.ts b/workers/main/src/index.test.ts index 517e406..81fcac8 100644 --- a/workers/main/src/index.test.ts +++ b/workers/main/src/index.test.ts @@ -1,26 +1,6 @@ import { describe, expect, it, vi } from 'vitest'; -import { handleRunError, logger, 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(); - }); -}); +import { handleRunError, logger } from './index'; describe('handleRunError', () => { it('should log the error', () => {