From 7bd734261cb86ceee70acf87146ec25ffdec072c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 27 Feb 2026 18:03:01 +0100 Subject: [PATCH] fix(tests): ensure proper test isolation and cleanup 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) --- .../OCPP20IncomingRequestService-GetBaseReport.test.ts | 7 ++++++- .../2.0/OCPP20IncomingRequestService-GetVariables.test.ts | 8 +++++++- .../2.0/OCPP20IncomingRequestService-SetVariables.test.ts | 8 +++++++- .../ocpp/2.0/OCPP20VariableManager.test.ts | 7 ++++++- .../ocpp/auth/services/OCPPAuthServiceFactory.test.ts | 7 ++++++- 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts index 6626981d..8ad2278f 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts @@ -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 = { diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetVariables.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetVariables.test.ts index 296be18b..59366030 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetVariables.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetVariables.test.ts @@ -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 = { diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-SetVariables.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-SetVariables.test.ts index f739bb70..56495150 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-SetVariables.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-SetVariables.test.ts @@ -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 = { diff --git a/tests/charging-station/ocpp/2.0/OCPP20VariableManager.test.ts b/tests/charging-station/ocpp/2.0/OCPP20VariableManager.test.ts index d52ac643..bf75a4ef 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20VariableManager.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20VariableManager.test.ts @@ -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() diff --git a/tests/charging-station/ocpp/auth/services/OCPPAuthServiceFactory.test.ts b/tests/charging-station/ocpp/auth/services/OCPPAuthServiceFactory.test.ts index 48cb27df..0e75d64d 100644 --- a/tests/charging-station/ocpp/auth/services/OCPPAuthServiceFactory.test.ts +++ b/tests/charging-station/ocpp/auth/services/OCPPAuthServiceFactory.test.ts @@ -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') -- 2.53.0