fix: Enhance error handling tests and add service status checks
Some checks failed
Docker Build and Push / build-and-push (push) Failing after 38s
LabFusion CI/CD Pipeline / service-adapters (push) Successful in 54s
LabFusion CI/CD Pipeline / api-gateway (push) Failing after 1m19s
LabFusion CI/CD Pipeline / api-docs (push) Successful in 1m46s
Frontend (React) / test (16) (push) Failing after 1m38s
Frontend (React) / test (18) (push) Failing after 1m45s
Frontend (React) / test (20) (push) Failing after 1m35s
Frontend (React) / build (push) Has been skipped
Frontend (React) / lighthouse (push) Has been skipped
LabFusion CI/CD Pipeline / frontend (push) Failing after 4m35s
LabFusion CI/CD Pipeline / integration-tests (push) Has been skipped
Integration Tests / integration-tests (push) Failing after 2m37s
Integration Tests / performance-tests (push) Has been skipped

### Summary of Changes
- Introduced new tests for `handleRequestError` to cover various error scenarios including connection timeouts and service errors.
- Added tests for `determineServiceStatus` to verify service availability states.
- Updated `formatServiceData` and `formatEventData` tests to ensure correct formatting and handling of invalid data.

### Expected Results
- Improved coverage and reliability of error handling utilities tests, ensuring accurate error responses and service status checks.
- Enhanced maintainability of test code by applying clean code principles and better organization.
This commit is contained in:
GSRN
2025-09-15 22:37:57 +02:00
parent fed58f2282
commit 1f98e03c02
2 changed files with 67 additions and 23 deletions

View File

@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';

View File

@@ -1,29 +1,51 @@
import { formatError, formatServiceData, formatEventData } from './errorHandling' import { handleRequestError, determineServiceStatus, formatServiceData, formatEventData } from './errorHandling'
describe('Error Handling Utils', () => { describe('Error Handling Utils', () => {
describe('formatError', () => { describe('handleRequestError', () => {
it('should format error objects correctly', () => { it('should handle connection timeout errors', () => {
const error = new Error('Test error message') const error = { code: 'ECONNABORTED' }
const formatted = formatError(error) const result = handleRequestError(error)
expect(formatted).toHaveProperty('message', 'Test error message') expect(result).toHaveProperty('error')
expect(formatted).toHaveProperty('type', 'Error') expect(result.error).toContain('Request timeout')
}) })
it('should handle string errors', () => { it('should handle response errors', () => {
const error = 'Simple string error' const error = { response: { status: 500 } }
const formatted = formatError(error) const result = handleRequestError(error)
expect(formatted).toHaveProperty('message', 'Simple string error') expect(result).toHaveProperty('error')
expect(formatted).toHaveProperty('type', 'string') expect(result.error).toContain('Service error')
}) })
it('should handle unknown error types', () => { it('should handle request errors', () => {
const error = { request: {} }
const result = handleRequestError(error)
expect(result).toHaveProperty('error')
expect(result.error).toContain('Service unavailable')
})
it('should handle unknown errors', () => {
const error = { someProperty: 'value' } const error = { someProperty: 'value' }
const formatted = formatError(error) const result = handleRequestError(error)
expect(formatted).toHaveProperty('message', 'Unknown error occurred') expect(result).toHaveProperty('error')
expect(formatted).toHaveProperty('type', 'unknown') expect(result.error).toContain('Unknown error')
})
})
describe('determineServiceStatus', () => {
it('should return offline when no services available', () => {
expect(determineServiceStatus(0, 3)).toBe('offline')
})
it('should return online when all services available', () => {
expect(determineServiceStatus(3, 3)).toBe('online')
})
it('should return partial when some services available', () => {
expect(determineServiceStatus(2, 3)).toBe('partial')
}) })
}) })
@@ -31,21 +53,31 @@ describe('Error Handling Utils', () => {
it('should format service data correctly', () => { it('should format service data correctly', () => {
const rawData = { const rawData = {
'api-gateway': { 'api-gateway': {
name: 'API Gateway',
status: 'healthy', status: 'healthy',
lastCheck: '2024-01-01T00:00:00.000Z' responseTime: '1d 2h'
} }
} }
const formatted = formatServiceData(rawData) const formatted = formatServiceData(rawData)
expect(formatted).toHaveProperty('api-gateway') expect(Array.isArray(formatted)).toBe(true)
expect(formatted['api-gateway']).toHaveProperty('status', 'healthy') expect(formatted).toHaveLength(1)
expect(formatted['api-gateway']).toHaveProperty('lastCheck') expect(formatted[0]).toHaveProperty('name', 'API Gateway')
expect(formatted[0]).toHaveProperty('status', 'online')
expect(formatted[0]).toHaveProperty('uptime', '1d 2h')
}) })
it('should handle empty data', () => { it('should handle empty data', () => {
const formatted = formatServiceData({}) const formatted = formatServiceData({})
expect(formatted).toEqual({}) expect(Array.isArray(formatted)).toBe(true)
expect(formatted).toHaveLength(0)
})
it('should handle invalid data', () => {
const formatted = formatServiceData(null)
expect(Array.isArray(formatted)).toBe(true)
expect(formatted).toHaveLength(0)
}) })
}) })
@@ -53,7 +85,6 @@ describe('Error Handling Utils', () => {
it('should format event data correctly', () => { it('should format event data correctly', () => {
const rawEvents = [ const rawEvents = [
{ {
id: '1',
timestamp: '2024-01-01T00:00:00.000Z', timestamp: '2024-01-01T00:00:00.000Z',
service: 'api-gateway', service: 'api-gateway',
event_type: 'health_check' event_type: 'health_check'
@@ -63,7 +94,9 @@ describe('Error Handling Utils', () => {
const formatted = formatEventData(rawEvents) const formatted = formatEventData(rawEvents)
expect(Array.isArray(formatted)).toBe(true) expect(Array.isArray(formatted)).toBe(true)
expect(formatted[0]).toHaveProperty('id', '1') expect(formatted).toHaveLength(1)
expect(formatted[0]).toHaveProperty('time')
expect(formatted[0]).toHaveProperty('event', 'health_check from api-gateway')
expect(formatted[0]).toHaveProperty('service', 'api-gateway') expect(formatted[0]).toHaveProperty('service', 'api-gateway')
}) })
@@ -72,5 +105,11 @@ describe('Error Handling Utils', () => {
expect(Array.isArray(formatted)).toBe(true) expect(Array.isArray(formatted)).toBe(true)
expect(formatted).toHaveLength(0) expect(formatted).toHaveLength(0)
}) })
it('should handle invalid data', () => {
const formatted = formatEventData(null)
expect(Array.isArray(formatted)).toBe(true)
expect(formatted).toHaveLength(0)
})
}) })
}) })