Skip to content

Commit c80f36d

Browse files
feat: Update SonarQube exclusions and add unit tests for main worker functions (#38)
- 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.
1 parent 9c998b6 commit c80f36d

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
sonar.projectKey=speedandfunction_automatization
22
sonar.organization=speedandfunction
33
sonar.javascript.lcov.reportPaths=workers/main/coverage/lcov.info
4-
sonar.exclusions=**/src/__tests__/**,**/src/dist/**
4+
sonar.exclusions=**/*.test.ts,**/src/dist/**

workers/main/src/__tests__/index.test.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

workers/main/src/index.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
3+
import { handleRunError, logger } from './index';
4+
5+
describe('handleRunError', () => {
6+
it('should log the error', () => {
7+
const error = new Error('test error');
8+
const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {});
9+
10+
handleRunError(error);
11+
expect(logSpy).toHaveBeenCalledWith(
12+
`Error in main worker: ${error.message}`,
13+
);
14+
logSpy.mockRestore();
15+
});
16+
17+
it('should log when error is a string', () => {
18+
const error = 'string error';
19+
const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {});
20+
21+
handleRunError(error);
22+
expect(logSpy).toHaveBeenCalledWith(`Error in main worker: ${error}`);
23+
logSpy.mockRestore();
24+
});
25+
26+
it('should log when error is an Error object', () => {
27+
const error = new Error('error from Error object');
28+
const logSpy = vi.spyOn(logger, 'error').mockImplementation(() => {});
29+
30+
handleRunError(error);
31+
expect(logSpy).toHaveBeenCalledWith(
32+
`Error in main worker: ${error.message}`,
33+
);
34+
logSpy.mockRestore();
35+
});
36+
});

workers/main/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DefaultLogger, NativeConnection, Worker } from '@temporalio/worker';
22

3-
import { logWorkerError, validateEnv } from '../../common/utils';
3+
import { validateEnv } from './common/utils';
44
import { temporalConfig } from './configs/temporal';
55
import { workerConfig } from './configs/worker';
66

@@ -37,10 +37,11 @@ export async function run(): Promise<void> {
3737
}
3838
}
3939

40-
export function handleRunError(err: unknown): never {
41-
logWorkerError('main', err);
40+
export function handleRunError(error: unknown): void {
41+
logger.error(
42+
`Error in main worker: ${error instanceof Error ? error.message : String(error)}`,
43+
);
4244
setTimeout(() => process.exit(1), 100);
43-
throw err;
4445
}
4546

4647
export function mainEntry() {

workers/main/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineConfig({
77
test: {
88
globals: true,
99
environment: 'node',
10-
include: ['src/__tests__/**/*.test.ts'],
10+
include: ['src/**/*.test.ts'],
1111
coverage: {
1212
provider: 'v8',
1313
reporter: ['text', 'lcov'],

0 commit comments

Comments
 (0)