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.
* @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 })
* @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'
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()
*/
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 {
})
await describe('I04 - CertificateSigned', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
+
const mockChargingStation = createChargingStation({
baseName: TEST_CHARGING_STATION_BASE_NAME,
connectorsCount: 3,
*/
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'
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,
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: () => {
*/
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'
})
await describe('I04 - DeleteCertificate', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
+
const mockChargingStation = createChargingStation({
baseName: TEST_CHARGING_STATION_BASE_NAME,
connectorsCount: 3,
*/
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'
})
await describe('I04 - GetInstalledCertificateIds', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
+
const mockChargingStation = createChargingStation({
baseName: TEST_CHARGING_STATION_BASE_NAME,
connectorsCount: 3,
})
// 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()
})
// 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)
*/
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'
})
await describe('I03 - InstallCertificate', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
+
const mockChargingStation = createChargingStation({
baseName: TEST_CHARGING_STATION_BASE_NAME,
connectorsCount: 3,
})
// 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()
/* 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'
} from '../../ChargingStationTestConstants.js'
await describe('B01 - Cold Boot Charging Station', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
+
const mockResponseService = new OCPP20ResponseService()
const requestService = new OCPP20RequestService(mockResponseService)
/* 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'
} from '../../ChargingStationTestConstants.js'
await describe('G02 - Heartbeat', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
const mockResponseService = new OCPP20ResponseService()
const requestService = new OCPP20RequestService(mockResponseService)
/* 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 {
})
await describe('M02 - Get15118EVCertificate Request', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
const mockChargingStation = createChargingStation({
baseName: TEST_CHARGING_STATION_BASE_NAME,
connectorsCount: 3,
*/
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,
} from '../../ChargingStationTestConstants.js'
await describe('B07/B08 - NotifyReport', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
const { service: testableService } = createTestableRequestService()
const mockChargingStation = createChargingStation({
*/
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 {
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,
/* 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'
} from '../../ChargingStationTestConstants.js'
await describe('G01 - Status Notification', async () => {
+ afterEach(() => {
+ mock.restoreAll()
+ })
const mockResponseService = new OCPP20ResponseService()
const requestService = new OCPP20RequestService(mockResponseService)
* @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,
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)
* @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,
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)
* @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,
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)
// 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,
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 = {
// 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'
})
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()
// 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,
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)
// 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'
}
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)
// 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'
}
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)
* @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)
* @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)
*/
interface MockContext {
mock: {
- method: (
- object: object,
- methodName: string
- ) => { mock: { calls: unknown[][] } }
+ method: (object: object, methodName: string) => { mock: { calls: unknown[][] } }
}
}
* @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,
} 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')
*/
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[] = []
* @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)
})
* @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)