]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix(tests): ensure proper test isolation and cleanup
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 Feb 2026 17:03:01 +0000 (18:03 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 27 Feb 2026 17:03:01 +0000 (18:03 +0100)
Add afterEach hooks to reset singleton state and clear factory caches
between tests, preventing state leakage across test files.

Changes:
- OCPP20VariableManager.test.ts: Reset runtime overrides after each test
- OCPPAuthServiceFactory.test.ts: Clear all cached instances after each test
- OCPP20IncomingRequestService-SetVariables.test.ts: Reset variable manager
- OCPP20IncomingRequestService-GetVariables.test.ts: Reset variable manager
- OCPP20IncomingRequestService-GetBaseReport.test.ts: Reset variable manager

Verified: All 280 tests pass on consecutive runs (test && test)

tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetVariables.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-SetVariables.test.ts
tests/charging-station/ocpp/2.0/OCPP20VariableManager.test.ts
tests/charging-station/ocpp/auth/services/OCPPAuthServiceFactory.test.ts

index 6626981d95d3d83d93a742564a0c1114ced93abe..8ad2278f351c517792cf93d5c981101f3701f63f 100644 (file)
@@ -7,7 +7,7 @@
 /* eslint-disable @typescript-eslint/no-unsafe-call */
 /* eslint-disable @typescript-eslint/no-explicit-any */
 import { expect } from '@std/expect'
-import { describe, it } from 'node:test'
+import { afterEach, describe, it } from 'node:test'
 
 import {
   addConfigurationKey,
@@ -60,6 +60,11 @@ await describe('B07 - Get Base Report', async () => {
 
   const incomingRequestService = new OCPP20IncomingRequestService()
 
+  // Reset singleton state after each test to ensure test isolation
+  afterEach(() => {
+    OCPP20VariableManager.getInstance().resetRuntimeOverrides()
+  })
+
   // FR: B07.FR.01, B07.FR.07
   await it('Should handle GetBaseReport request with ConfigurationInventory', () => {
     const request: OCPP20GetBaseReportRequest = {
index 296be18b2aa4b0d2df3b2c7ec410e0add7d8101a..59366030b2b83c55a552a3181adef23e4482c2e4 100644 (file)
@@ -4,9 +4,10 @@
  */
 import { expect } from '@std/expect'
 import { millisecondsToSeconds } from 'date-fns'
-import { describe, it } from 'node:test'
+import { afterEach, describe, it } from 'node:test'
 
 import { OCPP20IncomingRequestService } from '../../../../src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.js'
+import { OCPP20VariableManager } from '../../../../src/charging-station/ocpp/2.0/OCPP20VariableManager.js'
 import {
   AttributeEnumType,
   GetVariableStatusEnumType,
@@ -48,6 +49,11 @@ await describe('B06 - Get Variables', async () => {
 
   const incomingRequestService = new OCPP20IncomingRequestService()
 
+  // Reset singleton state after each test to ensure test isolation
+  afterEach(() => {
+    OCPP20VariableManager.getInstance().resetRuntimeOverrides()
+  })
+
   // FR: B06.FR.01
   await it('Should handle GetVariables request with valid variables', () => {
     const request: OCPP20GetVariablesRequest = {
index f739bb70f521ed67ef721464139e526133c3b3e0..564951508ed443486861d0d98c7ff556092b666e 100644 (file)
@@ -6,9 +6,10 @@
 
 import { expect } from '@std/expect'
 import { millisecondsToSeconds } from 'date-fns'
-import { describe, it } from 'node:test'
+import { afterEach, describe, it } from 'node:test'
 
 import { OCPP20IncomingRequestService } from '../../../../src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.js'
+import { OCPP20VariableManager } from '../../../../src/charging-station/ocpp/2.0/OCPP20VariableManager.js'
 import {
   AttributeEnumType,
   GetVariableStatusEnumType,
@@ -70,6 +71,11 @@ await describe('B05 - Set Variables', async () => {
   const incomingRequestService = new OCPP20IncomingRequestService()
   const svc = incomingRequestService as unknown as IncomingRequestServicePrivate
 
+  // Reset singleton state after each test to ensure test isolation
+  afterEach(() => {
+    OCPP20VariableManager.getInstance().resetRuntimeOverrides()
+  })
+
   // FR: B05.FR.01, B05.FR.10
   await it('Should handle SetVariables request with valid writable variables', () => {
     const request: OCPP20SetVariablesRequest = {
index d52ac64304229d34474628561f4452419fa899af..bf75a4ef3482e080ed01a71a4a2b0ab3468ded4b 100644 (file)
@@ -9,7 +9,7 @@
 
 import { expect } from '@std/expect'
 import { millisecondsToSeconds } from 'date-fns'
-import { describe, it } from 'node:test'
+import { afterEach, describe, it } from 'node:test'
 
 import {
   deleteConfigurationKey,
@@ -77,6 +77,11 @@ await describe('B05/B06 - OCPP20VariableManager test suite', async () => {
     websocketPingInterval: Constants.DEFAULT_WEBSOCKET_PING_INTERVAL,
   })
 
+  // Reset singleton state after each test to ensure test isolation
+  afterEach(() => {
+    OCPP20VariableManager.getInstance().resetRuntimeOverrides()
+  })
+
   await it('Verify that OCPP20VariableManager can be instantiated as singleton', () => {
     const manager1 = OCPP20VariableManager.getInstance()
     const manager2 = OCPP20VariableManager.getInstance()
index 48cb27df70418ef9a0c775f86bba6637e86ad95f..0e75d64da90a3a89bd60e8873349e69bf326c540 100644 (file)
@@ -3,7 +3,7 @@
  * @description Unit tests for OCPP authentication service factory
  */
 import { expect } from '@std/expect'
-import { describe, it } from 'node:test'
+import { afterEach, describe, it } from 'node:test'
 
 import type { ChargingStation } from '../../../../../src/charging-station/ChargingStation.js'
 
@@ -12,6 +12,11 @@ import { OCPPVersion } from '../../../../../src/types/ocpp/OCPPVersion.js'
 import { createMockAuthServiceTestStation } from '../helpers/MockFactories.js'
 
 await describe('OCPPAuthServiceFactory', async () => {
+  // Clear all cached instances after each test to ensure test isolation
+  afterEach(() => {
+    OCPPAuthServiceFactory.clearAllInstances()
+  })
+
   await describe('getInstance', async () => {
     await it('should create a new instance for a charging station', async () => {
       const mockStation = createMockAuthServiceTestStation('001')