From c1b95df9c3f15e669d0567aef94afcf1e17ded30 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 27 Feb 2026 23:14:12 +0100 Subject: [PATCH] test: add afterEach cleanup to all test files Add afterEach with mock.restoreAll() to 27 test files. Ensures 100% coverage for mock cleanup (63/63 files). Prevents test pollution between test runs. Follows TEST_STYLE_GUIDE.md standards. --- tests/ChargingStationFactory.test.ts | 6 +++++- .../ConfigurationKeyUtils.test.ts | 5 ++++- ...ngRequestService-CertificateSigned.test.ts | 6 +++++- ...0IncomingRequestService-ClearCache.test.ts | 10 ++++++++-- ...ngRequestService-DeleteCertificate.test.ts | 6 +++++- ...Service-GetInstalledCertificateIds.test.ts | 20 ++++++++++++++----- ...gRequestService-InstallCertificate.test.ts | 9 +++++++-- ...P20RequestService-BootNotification.test.ts | 6 +++++- .../OCPP20RequestService-HeartBeat.test.ts | 5 ++++- .../2.0/OCPP20RequestService-ISO15118.test.ts | 5 ++++- .../OCPP20RequestService-NotifyReport.test.ts | 9 +++++---- ...PP20RequestService-SignCertificate.test.ts | 5 ++++- ...0RequestService-StatusNotification.test.ts | 5 ++++- .../ocpp/auth/types/AuthTypes.test.ts | 5 ++++- .../ocpp/auth/utils/AuthHelpers.test.ts | 5 ++++- .../ocpp/auth/utils/AuthValidators.test.ts | 5 ++++- .../ocpp/auth/utils/ConfigValidator.test.ts | 5 ++++- .../ui-server/UIHttpServer.test.ts | 5 ++++- .../ui-server/UIServerSecurity.test.ts | 5 ++++- .../ui-server/UIWebSocketServer.test.ts | 5 ++++- .../ui-services/AbstractUIService.test.ts | 5 ++++- tests/exception/BaseError.test.ts | 5 ++++- tests/exception/OCPPError.test.ts | 6 +++++- tests/helpers/TestLifecycleHelpers.ts | 5 +---- tests/types/ConfigurationData.test.ts | 6 +++++- tests/utils/AsyncLock.test.ts | 5 ++++- tests/utils/ElectricUtils.test.ts | 5 ++++- tests/utils/StatisticUtils.test.ts | 5 ++++- 28 files changed, 134 insertions(+), 40 deletions(-) diff --git a/tests/ChargingStationFactory.test.ts b/tests/ChargingStationFactory.test.ts index a5ba713a..80a03920 100644 --- a/tests/ChargingStationFactory.test.ts +++ b/tests/ChargingStationFactory.test.ts @@ -3,13 +3,17 @@ * @description Unit tests for charging station factory utilities and OCPP service mocking */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { getHashId } from '../src/charging-station/Helpers.js' import { AvailabilityType, ConnectorStatusEnum, OCPPVersion } from '../src/types/index.js' import { createChargingStation, createChargingStationTemplate } from './ChargingStationFactory.js' await describe('ChargingStationFactory', async () => { + afterEach(() => { + mock.restoreAll() + }) + await describe('OCPP Service Mocking', async () => { await it('should throw error when OCPPRequestService.requestHandler is not mocked', async () => { const station = createChargingStation({ connectorsCount: 1 }) diff --git a/tests/charging-station/ConfigurationKeyUtils.test.ts b/tests/charging-station/ConfigurationKeyUtils.test.ts index 031f92a6..a9479c62 100644 --- a/tests/charging-station/ConfigurationKeyUtils.test.ts +++ b/tests/charging-station/ConfigurationKeyUtils.test.ts @@ -3,7 +3,7 @@ * @description Unit tests for OCPP configuration key management utilities */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import type { ChargingStationOcppConfiguration } from '../../src/types/index.js' @@ -22,6 +22,9 @@ const VALUE_A = 'ValueA' const VALUE_B = 'ValueB' await describe('ConfigurationKeyUtils test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await describe('getConfigurationKey()', async () => { await it('should return undefined when configurationKey array is missing', () => { const cs = createChargingStation() diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CertificateSigned.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CertificateSigned.test.ts index 3dff62be..2227f8c5 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CertificateSigned.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CertificateSigned.test.ts @@ -4,7 +4,7 @@ */ import { expect } from '@std/expect' -import { describe, it, mock, type Mock } from 'node:test' +import { afterEach, describe, it, mock, type Mock } from 'node:test' import type { ChargingStation } from '../../../../src/charging-station/index.js' import type { @@ -105,6 +105,10 @@ const createMockCertificateManager = ( }) await describe('I04 - CertificateSigned', async () => { + afterEach(() => { + mock.restoreAll() + }) + const mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 3, diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-ClearCache.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-ClearCache.test.ts index f99573fa..b28fa9b4 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-ClearCache.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-ClearCache.test.ts @@ -4,7 +4,7 @@ */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { createTestableIncomingRequestService } from '../../../../src/charging-station/ocpp/2.0/__testable__/index.js' import { OCPP20IncomingRequestService } from '../../../../src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.js' @@ -15,6 +15,10 @@ import { createChargingStation } from '../../../ChargingStationFactory.js' import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConstants.js' await describe('C11 - Clear Authorization Data in Authorization Cache', async () => { + afterEach(() => { + mock.restoreAll() + }) + const mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 3, @@ -86,7 +90,9 @@ await describe('C11 - Clear Authorization Data in Authorization Cache', async () await it('should NOT call idTagsCache.deleteIdTags() on ClearCache request', async () => { // Verify that IdTagsCache is not touched let deleteIdTagsCalled = false - const originalDeleteIdTags = mockChargingStation.idTagsCache.deleteIdTags.bind(mockChargingStation.idTagsCache) + const originalDeleteIdTags = mockChargingStation.idTagsCache.deleteIdTags.bind( + mockChargingStation.idTagsCache + ) Object.assign(mockChargingStation.idTagsCache, { deleteIdTags: () => { diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-DeleteCertificate.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-DeleteCertificate.test.ts index 696133fb..5bd120b6 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-DeleteCertificate.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-DeleteCertificate.test.ts @@ -4,7 +4,7 @@ */ import { expect } from '@std/expect' -import { describe, it, mock } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import type { ChargingStationWithCertificateManager } from '../../../../src/charging-station/ocpp/2.0/OCPP20CertificateManager.js' @@ -54,6 +54,10 @@ const createMockCertificateManager = ( }) await describe('I04 - DeleteCertificate', async () => { + afterEach(() => { + mock.restoreAll() + }) + const mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 3, diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetInstalledCertificateIds.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetInstalledCertificateIds.test.ts index ea1374c5..8eb24291 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetInstalledCertificateIds.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetInstalledCertificateIds.test.ts @@ -4,7 +4,7 @@ */ import { expect } from '@std/expect' -import { describe, it, mock } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import type { ChargingStationWithCertificateManager } from '../../../../src/charging-station/ocpp/2.0/OCPP20CertificateManager.js' @@ -59,6 +59,10 @@ const createMockCertificateManager = ( }) await describe('I04 - GetInstalledCertificateIds', async () => { + afterEach(() => { + mock.restoreAll() + }) + const mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 3, @@ -72,7 +76,8 @@ await describe('I04 - GetInstalledCertificateIds', async () => { }) // Cast to allow setting certificateManager property - const stationWithCertManager = mockChargingStation as unknown as ChargingStationWithCertificateManager + const stationWithCertManager = + mockChargingStation as unknown as ChargingStationWithCertificateManager stationWithCertManager.certificateManager = createMockCertificateManager() const incomingRequestService = new OCPP20IncomingRequestService() @@ -255,13 +260,18 @@ await describe('I04 - GetInstalledCertificateIds', async () => { }) // Explicitly set to null/undefined - const stationNoCertManager = stationWithoutCertManager as unknown as ChargingStationWithCertificateManager - stationNoCertManager.certificateManager = null as unknown as ChargingStationWithCertificateManager['certificateManager'] + const stationNoCertManager = + stationWithoutCertManager as unknown as ChargingStationWithCertificateManager + stationNoCertManager.certificateManager = + null as unknown as ChargingStationWithCertificateManager['certificateManager'] const request: OCPP20GetInstalledCertificateIdsRequest = {} const response: OCPP20GetInstalledCertificateIdsResponse = - await testableService.handleRequestGetInstalledCertificateIds(stationWithoutCertManager, request) + await testableService.handleRequestGetInstalledCertificateIds( + stationWithoutCertManager, + request + ) expect(response).toBeDefined() expect(response.status).toBe(GetInstalledCertificateStatusEnumType.NotFound) diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-InstallCertificate.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-InstallCertificate.test.ts index 64137e8d..6d47f906 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-InstallCertificate.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-InstallCertificate.test.ts @@ -4,7 +4,7 @@ */ import { expect } from '@std/expect' -import { describe, it, mock } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import type { ChargingStationWithCertificateManager } from '../../../../src/charging-station/ocpp/2.0/OCPP20CertificateManager.js' @@ -68,6 +68,10 @@ const createMockCertificateManager = ( }) await describe('I03 - InstallCertificate', async () => { + afterEach(() => { + mock.restoreAll() + }) + const mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 3, @@ -81,7 +85,8 @@ await describe('I03 - InstallCertificate', async () => { }) // Cast to allow setting certificateManager property - const stationWithCertManager = mockChargingStation as unknown as ChargingStationWithCertificateManager + const stationWithCertManager = + mockChargingStation as unknown as ChargingStationWithCertificateManager stationWithCertManager.certificateManager = createMockCertificateManager() const incomingRequestService = new OCPP20IncomingRequestService() diff --git a/tests/charging-station/ocpp/2.0/OCPP20RequestService-BootNotification.test.ts b/tests/charging-station/ocpp/2.0/OCPP20RequestService-BootNotification.test.ts index 8476227d..5835ba62 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20RequestService-BootNotification.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20RequestService-BootNotification.test.ts @@ -8,7 +8,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { OCPP20RequestService } from '../../../../src/charging-station/ocpp/2.0/OCPP20RequestService.js' import { OCPP20ResponseService } from '../../../../src/charging-station/ocpp/2.0/OCPP20ResponseService.js' @@ -30,6 +30,10 @@ import { } from '../../ChargingStationTestConstants.js' await describe('B01 - Cold Boot Charging Station', async () => { + afterEach(() => { + mock.restoreAll() + }) + const mockResponseService = new OCPP20ResponseService() const requestService = new OCPP20RequestService(mockResponseService) diff --git a/tests/charging-station/ocpp/2.0/OCPP20RequestService-HeartBeat.test.ts b/tests/charging-station/ocpp/2.0/OCPP20RequestService-HeartBeat.test.ts index 8b19aa04..33c096a0 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20RequestService-HeartBeat.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20RequestService-HeartBeat.test.ts @@ -8,7 +8,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { OCPP20RequestService } from '../../../../src/charging-station/ocpp/2.0/OCPP20RequestService.js' import { OCPP20ResponseService } from '../../../../src/charging-station/ocpp/2.0/OCPP20ResponseService.js' @@ -28,6 +28,9 @@ import { } from '../../ChargingStationTestConstants.js' await describe('G02 - Heartbeat', async () => { + afterEach(() => { + mock.restoreAll() + }) const mockResponseService = new OCPP20ResponseService() const requestService = new OCPP20RequestService(mockResponseService) diff --git a/tests/charging-station/ocpp/2.0/OCPP20RequestService-ISO15118.test.ts b/tests/charging-station/ocpp/2.0/OCPP20RequestService-ISO15118.test.ts index 65eac6bd..506ad930 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20RequestService-ISO15118.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20RequestService-ISO15118.test.ts @@ -5,7 +5,7 @@ /* cspell:ignore Bvbn NQIF CBCYX */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { createTestableRequestService } from '../../../../src/charging-station/ocpp/2.0/__testable__/index.js' import { @@ -42,6 +42,9 @@ const createMockOCSPRequestData = (): OCSPRequestDataType => ({ }) await describe('M02 - Get15118EVCertificate Request', async () => { + afterEach(() => { + mock.restoreAll() + }) const mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 3, diff --git a/tests/charging-station/ocpp/2.0/OCPP20RequestService-NotifyReport.test.ts b/tests/charging-station/ocpp/2.0/OCPP20RequestService-NotifyReport.test.ts index 45a0ca67..6b55e969 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20RequestService-NotifyReport.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20RequestService-NotifyReport.test.ts @@ -4,11 +4,9 @@ */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' -import { - createTestableRequestService, -} from '../../../../src/charging-station/ocpp/2.0/__testable__/index.js' +import { createTestableRequestService } from '../../../../src/charging-station/ocpp/2.0/__testable__/index.js' import { AttributeEnumType, DataEnumType, @@ -31,6 +29,9 @@ import { } from '../../ChargingStationTestConstants.js' await describe('B07/B08 - NotifyReport', async () => { + afterEach(() => { + mock.restoreAll() + }) const { service: testableService } = createTestableRequestService() const mockChargingStation = createChargingStation({ diff --git a/tests/charging-station/ocpp/2.0/OCPP20RequestService-SignCertificate.test.ts b/tests/charging-station/ocpp/2.0/OCPP20RequestService-SignCertificate.test.ts index 1a8b0506..67532861 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20RequestService-SignCertificate.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20RequestService-SignCertificate.test.ts @@ -4,7 +4,7 @@ */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { createTestableRequestService } from '../../../../src/charging-station/ocpp/2.0/__testable__/index.js' import { @@ -22,6 +22,9 @@ import { TEST_CHARGING_STATION_BASE_NAME } from '../../ChargingStationTestConsta const MOCK_ORGANIZATION_NAME = 'Test Organization Inc.' await describe('I02 - SignCertificate Request', async () => { + afterEach(() => { + mock.restoreAll() + }) const mockChargingStation = createChargingStation({ baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 3, diff --git a/tests/charging-station/ocpp/2.0/OCPP20RequestService-StatusNotification.test.ts b/tests/charging-station/ocpp/2.0/OCPP20RequestService-StatusNotification.test.ts index 9bd0b808..cd410cdf 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20RequestService-StatusNotification.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20RequestService-StatusNotification.test.ts @@ -8,7 +8,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { OCPP20RequestService } from '../../../../src/charging-station/ocpp/2.0/OCPP20RequestService.js' import { OCPP20ResponseService } from '../../../../src/charging-station/ocpp/2.0/OCPP20ResponseService.js' @@ -29,6 +29,9 @@ import { } from '../../ChargingStationTestConstants.js' await describe('G01 - Status Notification', async () => { + afterEach(() => { + mock.restoreAll() + }) const mockResponseService = new OCPP20ResponseService() const requestService = new OCPP20RequestService(mockResponseService) diff --git a/tests/charging-station/ocpp/auth/types/AuthTypes.test.ts b/tests/charging-station/ocpp/auth/types/AuthTypes.test.ts index b8661050..0f84170c 100644 --- a/tests/charging-station/ocpp/auth/types/AuthTypes.test.ts +++ b/tests/charging-station/ocpp/auth/types/AuthTypes.test.ts @@ -3,7 +3,7 @@ * @description Unit tests for authentication type definitions and mappings */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { AuthContext, @@ -31,6 +31,9 @@ import { import { OCPPVersion } from '../../../../../src/types/ocpp/OCPPVersion.js' await describe('AuthTypes', async () => { + afterEach(() => { + mock.restoreAll() + }) await describe('IdentifierTypeGuards', async () => { await it('should correctly identify OCPP 1.6 types', () => { expect(isOCPP16Type(IdentifierType.ID_TAG)).toBe(true) diff --git a/tests/charging-station/ocpp/auth/utils/AuthHelpers.test.ts b/tests/charging-station/ocpp/auth/utils/AuthHelpers.test.ts index c4d4a849..e17dbd29 100644 --- a/tests/charging-station/ocpp/auth/utils/AuthHelpers.test.ts +++ b/tests/charging-station/ocpp/auth/utils/AuthHelpers.test.ts @@ -3,7 +3,7 @@ * @description Unit tests for authentication helper utilities */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { AuthContext, @@ -17,6 +17,9 @@ import { AuthHelpers } from '../../../../../src/charging-station/ocpp/auth/utils import { OCPPVersion } from '../../../../../src/types/ocpp/OCPPVersion.js' await describe('AuthHelpers', async () => { + afterEach(() => { + mock.restoreAll() + }) await describe('calculateTTL', async () => { await it('should return undefined for undefined expiry date', () => { const result = AuthHelpers.calculateTTL(undefined) diff --git a/tests/charging-station/ocpp/auth/utils/AuthValidators.test.ts b/tests/charging-station/ocpp/auth/utils/AuthValidators.test.ts index 0b39ba03..5fbf2da5 100644 --- a/tests/charging-station/ocpp/auth/utils/AuthValidators.test.ts +++ b/tests/charging-station/ocpp/auth/utils/AuthValidators.test.ts @@ -3,7 +3,7 @@ * @description Unit tests for authentication validation utilities */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { type AuthConfiguration, @@ -15,6 +15,9 @@ import { AuthValidators } from '../../../../../src/charging-station/ocpp/auth/ut import { OCPPVersion } from '../../../../../src/types/ocpp/OCPPVersion.js' await describe('AuthValidators', async () => { + afterEach(() => { + mock.restoreAll() + }) await describe('isValidCacheTTL', async () => { await it('should return true for undefined TTL', () => { expect(AuthValidators.isValidCacheTTL(undefined)).toBe(true) diff --git a/tests/charging-station/ocpp/auth/utils/ConfigValidator.test.ts b/tests/charging-station/ocpp/auth/utils/ConfigValidator.test.ts index 1dd09d88..a9c7b3ce 100644 --- a/tests/charging-station/ocpp/auth/utils/ConfigValidator.test.ts +++ b/tests/charging-station/ocpp/auth/utils/ConfigValidator.test.ts @@ -5,7 +5,7 @@ // Copyright Jerome Benoit. 2021-2025. All Rights Reserved. import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { type AuthConfiguration, @@ -15,6 +15,9 @@ import { import { AuthConfigValidator } from '../../../../../src/charging-station/ocpp/auth/utils/ConfigValidator.js' await describe('AuthConfigValidator', async () => { + afterEach(() => { + mock.restoreAll() + }) await describe('validate', async () => { await it('should accept valid configuration', () => { const config: AuthConfiguration = { diff --git a/tests/charging-station/ui-server/UIHttpServer.test.ts b/tests/charging-station/ui-server/UIHttpServer.test.ts index aaf148bc..c3ab62c7 100644 --- a/tests/charging-station/ui-server/UIHttpServer.test.ts +++ b/tests/charging-station/ui-server/UIHttpServer.test.ts @@ -5,7 +5,7 @@ // Copyright Jerome Benoit. 2024-2025. All Rights Reserved. import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { gunzipSync } from 'node:zlib' import type { UUIDv4 } from '../../../src/types/index.js' @@ -47,6 +47,9 @@ const createLargePayload = (status: ResponseStatus = ResponseStatus.SUCCESS) => }) await describe('UIHttpServer test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await it('should delete response handler after successful send', () => { const server = new TestableUIHttpServer(createHttpServerConfig()) const res = new MockServerResponse() diff --git a/tests/charging-station/ui-server/UIServerSecurity.test.ts b/tests/charging-station/ui-server/UIServerSecurity.test.ts index 3c851d04..5312bd94 100644 --- a/tests/charging-station/ui-server/UIServerSecurity.test.ts +++ b/tests/charging-station/ui-server/UIServerSecurity.test.ts @@ -5,7 +5,7 @@ // Copyright Jerome Benoit. 2024-2025. All Rights Reserved. import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { createBodySizeLimiter, @@ -19,6 +19,9 @@ import { waitForStreamFlush } from './UIServerTestUtils.js' const RATE_WINDOW_EXPIRY_DELAY_MS = 110 await describe('UIServerSecurity test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await describe('isValidCredential()', async () => { await it('should return true for matching credentials', () => { expect(isValidCredential('myPassword123', 'myPassword123')).toBe(true) diff --git a/tests/charging-station/ui-server/UIWebSocketServer.test.ts b/tests/charging-station/ui-server/UIWebSocketServer.test.ts index 3f546604..5e8bf64e 100644 --- a/tests/charging-station/ui-server/UIWebSocketServer.test.ts +++ b/tests/charging-station/ui-server/UIWebSocketServer.test.ts @@ -5,7 +5,7 @@ // Copyright Jerome Benoit. 2024-2025. All Rights Reserved. import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import type { UUIDv4 } from '../../../src/types/index.js' @@ -35,6 +35,9 @@ class TestableUIWebSocketServer extends UIWebSocketServer { } await describe('UIWebSocketServer test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await it('should delete response handler after successful send', () => { const config = createMockUIServerConfiguration() const server = new TestableUIWebSocketServer(config) diff --git a/tests/charging-station/ui-server/ui-services/AbstractUIService.test.ts b/tests/charging-station/ui-server/ui-services/AbstractUIService.test.ts index 58081dd8..a1eadcd0 100644 --- a/tests/charging-station/ui-server/ui-services/AbstractUIService.test.ts +++ b/tests/charging-station/ui-server/ui-services/AbstractUIService.test.ts @@ -5,7 +5,7 @@ // Copyright Jerome Benoit. 2024-2025. All Rights Reserved. import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { UIWebSocketServer } from '../../../../src/charging-station/ui-server/UIWebSocketServer.js' import { ProcedureName, ProtocolVersion, ResponseStatus } from '../../../../src/types/index.js' @@ -27,6 +27,9 @@ class TestableUIWebSocketServer extends UIWebSocketServer { } await describe('AbstractUIService test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await it('should check response handler existence before sending', () => { const config = createMockUIServerConfiguration() const server = new TestableUIWebSocketServer(config) diff --git a/tests/exception/BaseError.test.ts b/tests/exception/BaseError.test.ts index 1b505abb..a715a7dd 100644 --- a/tests/exception/BaseError.test.ts +++ b/tests/exception/BaseError.test.ts @@ -3,11 +3,14 @@ * @description Unit tests for base error class functionality */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { BaseError } from '../../src/exception/BaseError.js' await describe('BaseError test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await it('should create instance with default values', () => { const baseError = new BaseError() expect(baseError).toBeInstanceOf(BaseError) diff --git a/tests/exception/OCPPError.test.ts b/tests/exception/OCPPError.test.ts index fef65af8..4a5ff487 100644 --- a/tests/exception/OCPPError.test.ts +++ b/tests/exception/OCPPError.test.ts @@ -3,13 +3,17 @@ * @description Unit tests for OCPP-specific error class */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { OCPPError } from '../../src/exception/OCPPError.js' import { ErrorType } from '../../src/types/index.js' import { Constants } from '../../src/utils/Constants.js' await describe('OCPPError test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) + await it('should create instance with error code and default values', () => { const ocppError = new OCPPError(ErrorType.GENERIC_ERROR, '') expect(ocppError).toBeInstanceOf(OCPPError) diff --git a/tests/helpers/TestLifecycleHelpers.ts b/tests/helpers/TestLifecycleHelpers.ts index 075e0960..4de31796 100644 --- a/tests/helpers/TestLifecycleHelpers.ts +++ b/tests/helpers/TestLifecycleHelpers.ts @@ -76,10 +76,7 @@ export interface TimerHelperOptions { */ interface MockContext { mock: { - method: ( - object: object, - methodName: string - ) => { mock: { calls: unknown[][] } } + method: (object: object, methodName: string) => { mock: { calls: unknown[][] } } } } diff --git a/tests/types/ConfigurationData.test.ts b/tests/types/ConfigurationData.test.ts index 62ce324b..979d707c 100644 --- a/tests/types/ConfigurationData.test.ts +++ b/tests/types/ConfigurationData.test.ts @@ -3,7 +3,7 @@ * @description Unit tests for configuration data types and enumerations */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { ApplicationProtocolVersion, @@ -12,6 +12,10 @@ import { } from '../../src/types/ConfigurationData.js' await describe('ConfigurationData test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) + await it('should define ConfigurationSection enumeration values', () => { expect(ConfigurationSection.log).toBe('log') expect(ConfigurationSection.performanceStorage).toBe('performanceStorage') diff --git a/tests/utils/AsyncLock.test.ts b/tests/utils/AsyncLock.test.ts index 9f91d5ff..0d812577 100644 --- a/tests/utils/AsyncLock.test.ts +++ b/tests/utils/AsyncLock.test.ts @@ -4,11 +4,14 @@ */ import { expect } from '@std/expect' import { randomInt } from 'node:crypto' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { AsyncLock, AsyncLockType } from '../../src/utils/AsyncLock.js' await describe('AsyncLock test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await it('should run synchronous functions exclusively in sequence', () => { const runs = 10 const executed: number[] = [] diff --git a/tests/utils/ElectricUtils.test.ts b/tests/utils/ElectricUtils.test.ts index e61c09d0..11940222 100644 --- a/tests/utils/ElectricUtils.test.ts +++ b/tests/utils/ElectricUtils.test.ts @@ -3,11 +3,14 @@ * @description Unit tests for electrical calculations (AC/DC power, amperage) */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { ACElectricUtils, DCElectricUtils } from '../../src/utils/ElectricUtils.js' await describe('ElectricUtils test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await it('should calculate DC power from voltage and current', () => { expect(DCElectricUtils.power(230, 1)).toBe(230) }) diff --git a/tests/utils/StatisticUtils.test.ts b/tests/utils/StatisticUtils.test.ts index 2cbfacc0..1b231a44 100644 --- a/tests/utils/StatisticUtils.test.ts +++ b/tests/utils/StatisticUtils.test.ts @@ -3,11 +3,14 @@ * @description Unit tests for statistical calculation utilities */ import { expect } from '@std/expect' -import { describe, it } from 'node:test' +import { afterEach, describe, it, mock } from 'node:test' import { average, max, median, min, percentile, std } from '../../src/utils/StatisticUtils.js' await describe('StatisticUtils test suite', async () => { + afterEach(() => { + mock.restoreAll() + }) await it('should calculate arithmetic mean of array values', () => { expect(average([])).toBe(0) expect(average([0.08])).toBe(0.08) -- 2.43.0