From: Jérôme Benoit Date: Sat, 28 Feb 2026 14:06:42 +0000 (+0100) Subject: refactor(tests): remove eslint-disable and fix type safety issues X-Git-Tag: ocpp-server@v3.0.0~89 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7513f6339b33c863f11a0187dba41a5966789166;p=e-mobility-charging-stations-simulator.git refactor(tests): remove eslint-disable and fix type safety issues - Remove file-level eslint-disable comments from OCPP 2.0 transaction tests - Replace `as any` casts with proper TypeScript types (ChargingStation, SentRequest) - Fix test isolation by moving state reset to beforeEach hooks - Use `stationInfo = undefined` pattern instead of delete with any cast - Remove unnecessary async keywords and optional chains per lint rules --- diff --git a/tests/charging-station/Helpers.test.ts b/tests/charging-station/Helpers.test.ts index a7b9f028..341c4c7e 100644 --- a/tests/charging-station/Helpers.test.ts +++ b/tests/charging-station/Helpers.test.ts @@ -67,8 +67,7 @@ await describe('Helpers test suite', async () => { // For validation edge cases, we need to manually create invalid states // since the factory is designed to create valid configurations const stationNoInfo = createChargingStation({ baseName }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access - delete (stationNoInfo as any).stationInfo + stationNoInfo.stationInfo = undefined expect(() => { validateStationInfo(stationNoInfo) }).toThrow(new BaseError('Missing charging station information')) diff --git a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Offline.test.ts b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Offline.test.ts index d201956f..653f7d6b 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Offline.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Offline.test.ts @@ -2,15 +2,11 @@ * @file Tests for OCPP20ServiceUtils TransactionEvent Offline * @description Unit tests for OCPP 2.0 offline TransactionEvent queueing (E02) */ -/* eslint-disable @typescript-eslint/no-unsafe-member-access */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ -/* eslint-disable @typescript-eslint/no-unsafe-argument */ -/* eslint-disable @typescript-eslint/no-unsafe-call */ -/* eslint-disable @typescript-eslint/no-explicit-any */ import { expect } from '@std/expect' import { afterEach, beforeEach, describe, it, mock } from 'node:test' +import type { ChargingStation } from '../../../../src/charging-station/ChargingStation.js' import type { EmptyObject } from '../../../../src/types/index.js' import { OCPP20ServiceUtils } from '../../../../src/charging-station/ocpp/2.0/OCPP20ServiceUtils.js' @@ -20,23 +16,30 @@ import { OCPPVersion, } from '../../../../src/types/index.js' import { Constants, generateUUID } from '../../../../src/utils/index.js' +import { standardCleanup } from '../../../../tests/helpers/TestLifecycleHelpers.js' import { createChargingStation } from '../../../ChargingStationFactory.js' import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConstants.js' import { resetLimits } from './OCPP20TestUtils.js' await describe('E02 - OCPP 2.0.1 Offline TransactionEvent Queueing', async () => { - let mockChargingStation: any + let mockChargingStation: ChargingStation let requestHandlerMock: ReturnType - let sentRequests: any[] + interface SentRequest { + command: string + payload: Record + } + let sentRequests: SentRequest[] let isOnline: boolean beforeEach(() => { sentRequests = [] isOnline = true - requestHandlerMock = mock.fn(async (_station: any, command: string, payload: any) => { - sentRequests.push({ command, payload }) - return Promise.resolve({} as EmptyObject) - }) + requestHandlerMock = mock.fn( + async (_station: ChargingStation, command: string, payload: Record) => { + sentRequests.push({ command, payload }) + return Promise.resolve({} as EmptyObject) + } + ) mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, @@ -65,6 +68,7 @@ await describe('E02 - OCPP 2.0.1 Offline TransactionEvent Queueing', async () => connector.transactionEventQueue = undefined } } + standardCleanup() }) await describe('Queue formation when offline', async () => { diff --git a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Periodic.test.ts b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Periodic.test.ts index 53de24dc..1d2484ce 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Periodic.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Periodic.test.ts @@ -2,17 +2,11 @@ * @file Tests for OCPP20ServiceUtils TransactionEvent Periodic * @description Unit tests for OCPP 2.0 periodic TransactionEvent at TxUpdatedInterval (E02) */ -/* eslint-disable @typescript-eslint/no-unsafe-member-access */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ -/* eslint-disable @typescript-eslint/no-unsafe-argument */ -/* eslint-disable @typescript-eslint/no-unsafe-call */ -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/require-await */ -/* eslint-disable @typescript-eslint/no-unnecessary-condition */ import { expect } from '@std/expect' import { afterEach, beforeEach, describe, it, mock } from 'node:test' +import type { ChargingStation } from '../../../../src/charging-station/ChargingStation.js' import type { EmptyObject } from '../../../../src/types/index.js' import { OCPP20ServiceUtils } from '../../../../src/charging-station/ocpp/2.0/OCPP20ServiceUtils.js' @@ -22,21 +16,28 @@ import { OCPPVersion, } from '../../../../src/types/index.js' import { Constants, generateUUID } from '../../../../src/utils/index.js' +import { standardCleanup } from '../../../../tests/helpers/TestLifecycleHelpers.js' import { createChargingStation } from '../../../ChargingStationFactory.js' import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConstants.js' import { resetLimits } from './OCPP20TestUtils.js' await describe('E02 - OCPP 2.0.1 Periodic TransactionEvent at TxUpdatedInterval', async () => { - let mockChargingStation: any + let mockChargingStation: ChargingStation let requestHandlerMock: ReturnType - let sentRequests: any[] + interface SentRequest { + command: string + payload: Record + } + let sentRequests: SentRequest[] beforeEach(() => { sentRequests = [] - requestHandlerMock = mock.fn(async (_station: any, command: string, payload: any) => { - sentRequests.push({ command, payload }) - return Promise.resolve({} as EmptyObject) - }) + requestHandlerMock = mock.fn( + async (_station: ChargingStation, command: string, payload: Record) => { + sentRequests.push({ command, payload }) + return Promise.resolve({} as EmptyObject) + } + ) mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, @@ -65,9 +66,10 @@ await describe('E02 - OCPP 2.0.1 Periodic TransactionEvent at TxUpdatedInterval' const connector = mockChargingStation.getConnectorStatus(connectorId) if (connector?.transactionTxUpdatedSetInterval != null) { clearInterval(connector.transactionTxUpdatedSetInterval) - delete connector.transactionTxUpdatedSetInterval + connector.transactionTxUpdatedSetInterval = undefined } } + standardCleanup() }) await describe('startTxUpdatedInterval', async () => { @@ -81,7 +83,7 @@ await describe('E02 - OCPP 2.0.1 Periodic TransactionEvent at TxUpdatedInterval' }) // Call startTxUpdatedInterval on OCPP 1.6 station - ocpp16Station.startTxUpdatedInterval?.(1, 60000) + ocpp16Station.startTxUpdatedInterval(1, 60000) // Verify no timer was started (method should return early) const connector = ocpp16Station.getConnectorStatus(1) @@ -148,7 +150,7 @@ await describe('E02 - OCPP 2.0.1 Periodic TransactionEvent at TxUpdatedInterval' ) }) - await it('should increment seqNo for each periodic event', async () => { + await it('should increment seqNo for each periodic event', () => { const connectorId = 1 const transactionId = generateUUID() @@ -241,7 +243,7 @@ await describe('E02 - OCPP 2.0.1 Periodic TransactionEvent at TxUpdatedInterval' }) await describe('Timer lifecycle integration', async () => { - await it('should continue seqNo sequence across multiple periodic events', async () => { + await it('should continue seqNo sequence across multiple periodic events', () => { const connectorId = 1 const transactionId = generateUUID() @@ -282,7 +284,7 @@ await describe('E02 - OCPP 2.0.1 Periodic TransactionEvent at TxUpdatedInterval' expect(endEvent.seqNo).toBe(4) }) - await it('should handle multiple connectors with independent timers', async () => { + await it('should handle multiple connectors with independent timers', () => { const transactionId1 = generateUUID() const transactionId2 = generateUUID() @@ -368,8 +370,8 @@ await describe('E02 - OCPP 2.0.1 Periodic TransactionEvent at TxUpdatedInterval' transactionId ) throw new Error('Should have thrown network error') - } catch (error: any) { - expect(error.message).toContain('Network timeout') + } catch (error) { + expect((error as Error).message).toContain('Network timeout') } }) }) diff --git a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent.test.ts b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent.test.ts index 9752ea95..a2f2d438 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent.test.ts @@ -2,13 +2,9 @@ * @file Tests for OCPP20ServiceUtils TransactionEvent * @description Unit tests for OCPP 2.0 TransactionEvent building and trigger reasons (E01-E04) */ -/* eslint-disable @typescript-eslint/no-unsafe-member-access */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ - -/* eslint-disable @typescript-eslint/no-explicit-any */ import { expect } from '@std/expect' -import { afterEach, describe, it } from 'node:test' +import { afterEach, beforeEach, describe, it } from 'node:test' import { OCPP20ServiceUtils } from '../../../../src/charging-station/ocpp/2.0/OCPP20ServiceUtils.js' import { @@ -29,10 +25,12 @@ import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConsta import { createMockOCPP20TransactionTestStation, resetLimits } from './OCPP20TestUtils.js' await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async () => { - const mockChargingStation = createMockOCPP20TransactionTestStation() + let mockChargingStation: ReturnType - // Reset limits before tests - resetLimits(mockChargingStation) + beforeEach(() => { + mockChargingStation = createMockOCPP20TransactionTestStation() + resetLimits(mockChargingStation) + }) // Reset singleton state and timers after each test to ensure test isolation afterEach(() => { @@ -173,8 +171,8 @@ await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async () invalidTransactionId ) throw new Error('Should have thrown error for invalid identifier string') - } catch (error: any) { - expect(error.message).toContain('Invalid transaction ID format') + } catch (error) { + expect((error as Error).message).toContain('Invalid transaction ID format') expect(error.message).toContain('≤36 characters') } }) @@ -276,8 +274,8 @@ await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async () transactionId ) throw new Error('Should have thrown error') - } catch (error: any) { - expect(error.message).toContain('Network error') + } catch (error) { + expect((error as Error).message).toContain('Network error') } }) }) @@ -353,7 +351,7 @@ await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async () ] for (const field of requiredFields) { expect(transactionEvent).toHaveProperty(field) - expect((transactionEvent as any)[field]).toBeDefined() + expect(transactionEvent[field as keyof typeof transactionEvent]).toBeDefined() } // Validate field types match schema requirements @@ -707,7 +705,7 @@ await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async () await it('should fallback to Trigger for unknown context source', () => { const context: OCPP20TransactionContext = { - source: 'unknown_source' as any, // Invalid source to test fallback + source: 'unknown_source' as OCPP20TransactionContext['source'], // Invalid source to test fallback } const triggerReason = OCPP20ServiceUtils.selectTriggerReason( @@ -847,8 +845,8 @@ await describe('E01-E04 - OCPP 2.0.1 TransactionEvent Implementation', async () transactionId ) throw new Error('Should have thrown error') - } catch (error: any) { - expect(error.message).toContain('Context test error') + } catch (error) { + expect((error as Error).message).toContain('Context test error') } }) })