]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(tests): remove eslint-disable and fix type safety issues
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 28 Feb 2026 14:06:42 +0000 (15:06 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 28 Feb 2026 14:34:49 +0000 (15:34 +0100)
- 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

tests/charging-station/Helpers.test.ts
tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Offline.test.ts
tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent-Periodic.test.ts
tests/charging-station/ocpp/2.0/OCPP20ServiceUtils-TransactionEvent.test.ts

index a7b9f028e8a4823e7a36775e2745354f6a297f6c..341c4c7e688d74ab81fde71a2680400bca3b8a2a 100644 (file)
@@ -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'))
index d201956ff435f57e831e340d4d58e68e7f4cb2ad..653f7d6b78a661e1e9affd9e9ebdc2178100cb6c 100644 (file)
@@ -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<typeof mock.fn>
-  let sentRequests: any[]
+  interface SentRequest {
+    command: string
+    payload: Record<string, unknown>
+  }
+  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<string, unknown>) => {
+        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 () => {
index 53de24dc780fd77de0a74807d48ca9e84abb024a..1d2484ceea72dc847da7b61b4b0ecf431db99344 100644 (file)
@@ -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<typeof mock.fn>
-  let sentRequests: any[]
+  interface SentRequest {
+    command: string
+    payload: Record<string, unknown>
+  }
+  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<string, unknown>) => {
+        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')
       }
     })
   })
index 9752ea95c35a1165621a8c76c8020b092599da7c..a2f2d4382048ce5124152451c30a25e2ff6824ad 100644 (file)
@@ -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<typeof createMockOCPP20TransactionTestStation>
 
-  // 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')
         }
       })
     })