From 422c08e73486e84c7ba182b6cc2b622eac3586c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 17 Apr 2026 18:16:51 +0200 Subject: [PATCH] fix(tests): use async mock callbacks for all async method mocks Sync functions returning Promises are not behaviorally equivalent to async functions. Align all 40 mock callbacks across 14 test files to use async when the real method is async. --- ...PP16IncomingRequestService-Firmware.test.ts | 4 ++-- ...mingRequestService-RemoteStopUnlock.test.ts | 6 +++--- .../1.6/OCPP16Integration-Transactions.test.ts | 2 +- .../1.6/OCPP16RequestService-CallChain.test.ts | 2 +- ...gRequestService-CustomerInformation.test.ts | 4 ++-- ...ncomingRequestService-GetBaseReport.test.ts | 4 ++-- ...OCPP20IncomingRequestService-GetLog.test.ts | 4 ++-- ...uestService-RequestStartTransaction.test.ts | 2 +- ...comingRequestService-UpdateFirmware.test.ts | 4 ++-- ...P20ResponseService-TransactionEvent.test.ts | 18 +++++++++--------- .../ocpp/OCPPServiceOperations.test.ts | 2 +- .../ui-server/UIMCPServer.test.ts | 8 ++++---- .../storage/MikroOrmStorage.test.ts | 16 ++++++++-------- .../performance/storage/MongoDBStorage.test.ts | 18 +++++++++--------- 14 files changed, 47 insertions(+), 47 deletions(-) diff --git a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Firmware.test.ts b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Firmware.test.ts index bc1646c3..f9814719 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Firmware.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Firmware.test.ts @@ -176,7 +176,7 @@ await describe('OCPP16IncomingRequestService — Firmware', async () => { updateFirmwareSimulation: (chargingStation: unknown) => Promise }, 'updateFirmwareSimulation', - mock.fn(() => Promise.resolve()) + mock.fn(async () => Promise.resolve()) ) }) @@ -249,7 +249,7 @@ await describe('OCPP16IncomingRequestService — Firmware', async () => { updateFirmwareSimulation: (chargingStation: unknown) => Promise }, 'updateFirmwareSimulation', - mock.fn(() => Promise.reject(new Error('firmware simulation error'))) + mock.fn(async () => Promise.reject(new Error('firmware simulation error'))) ) const request: OCPP16UpdateFirmwareRequest = { diff --git a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStopUnlock.test.ts b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStopUnlock.test.ts index 09e304fb..62e192c6 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStopUnlock.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStopUnlock.test.ts @@ -198,7 +198,7 @@ await describe('OCPP16IncomingRequestService — RemoteStopTransaction and Unloc // Arrange setupConnectorWithTransaction(listenerStation, 1, { transactionId: 42 }) - const mockRemoteStop = mock.method(OCPP16ServiceUtils, 'remoteStopTransaction', () => + const mockRemoteStop = mock.method(OCPP16ServiceUtils, 'remoteStopTransaction', async () => Promise.resolve({ status: GenericStatus.Accepted } satisfies GenericResponse) ) @@ -224,7 +224,7 @@ await describe('OCPP16IncomingRequestService — RemoteStopTransaction and Unloc await it('should NOT call remoteStopTransaction when response is Rejected', () => { // Arrange - const mockRemoteStop = mock.method(OCPP16ServiceUtils, 'remoteStopTransaction', () => + const mockRemoteStop = mock.method(OCPP16ServiceUtils, 'remoteStopTransaction', async () => Promise.resolve({ status: GenericStatus.Rejected } satisfies GenericResponse) ) @@ -247,7 +247,7 @@ await describe('OCPP16IncomingRequestService — RemoteStopTransaction and Unloc // Arrange setupConnectorWithTransaction(listenerStation, 1, { transactionId: 77 }) - mock.method(OCPP16ServiceUtils, 'remoteStopTransaction', () => + mock.method(OCPP16ServiceUtils, 'remoteStopTransaction', async () => Promise.reject(new Error('remoteStopTransaction failed')) ) diff --git a/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts b/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts index ca478245..d7cac311 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts @@ -56,7 +56,7 @@ function createIntegrationContext (): { baseName: TEST_CHARGING_STATION_BASE_NAME, connectorsCount: 2, ocppRequestService: { - requestHandler: () => Promise.resolve({}), + requestHandler: async () => Promise.resolve({}), }, stationInfo: { ocppStrictCompliance: false, diff --git a/tests/charging-station/ocpp/1.6/OCPP16RequestService-CallChain.test.ts b/tests/charging-station/ocpp/1.6/OCPP16RequestService-CallChain.test.ts index ad2db1fb..8d180eb9 100644 --- a/tests/charging-station/ocpp/1.6/OCPP16RequestService-CallChain.test.ts +++ b/tests/charging-station/ocpp/1.6/OCPP16RequestService-CallChain.test.ts @@ -34,7 +34,7 @@ await describe('OCPP 1.6 Request Call Chain — requestHandler → buildRequestP const responseService = new OCPP16ResponseService() requestService = new OCPP16RequestService(responseService) - sendMessageMock = mock.fn(() => Promise.resolve({} as JsonType)) + sendMessageMock = mock.fn(async () => Promise.resolve({} as JsonType)) Object.defineProperty(requestService, 'sendMessage', { configurable: true, value: sendMessageMock, diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CustomerInformation.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CustomerInformation.test.ts index be8c6d08..ced41d15 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CustomerInformation.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CustomerInformation.test.ts @@ -174,7 +174,7 @@ await describe('N32 - CustomerInformation', async () => { ) => Promise }, 'sendNotifyCustomerInformation', - () => Promise.resolve() + async () => Promise.resolve() ) }) @@ -261,7 +261,7 @@ await describe('N32 - CustomerInformation', async () => { ) => Promise }, 'sendNotifyCustomerInformation', - () => Promise.reject(new Error('notification error')) + async () => Promise.reject(new Error('notification error')) ) const request: OCPP20CustomerInformationRequest = { 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 e327a6bc..f8f1054c 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts @@ -376,7 +376,7 @@ await describe('B07 - Get Base Report', async () => { ) => Promise }, 'sendNotifyReportRequest', - () => Promise.resolve() + async () => Promise.resolve() ) }) @@ -429,7 +429,7 @@ await describe('B07 - Get Base Report', async () => { ) => Promise }, 'sendNotifyReportRequest', - () => Promise.reject(new Error('notify report error')) + async () => Promise.reject(new Error('notify report error')) ) const request: OCPP20GetBaseReportRequest = { diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetLog.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetLog.test.ts index b5f709e4..2f2bd02e 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetLog.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetLog.test.ts @@ -101,7 +101,7 @@ await describe('K01 - GetLog', async () => { ) => Promise }, 'simulateLogUploadLifecycle', - () => Promise.resolve() + async () => Promise.resolve() ) }) @@ -154,7 +154,7 @@ await describe('K01 - GetLog', async () => { ) => Promise }, 'simulateLogUploadLifecycle', - () => Promise.reject(new Error('log upload error')) + async () => Promise.reject(new Error('log upload error')) ) const request: OCPP20GetLogRequest = { diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts index f0ad2fbf..d82bd18c 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts @@ -438,7 +438,7 @@ await describe('F01 & F02 - Remote Start Transaction', async () => { // Arrange const stationId = mockStation.stationInfo?.chargingStationId ?? 'unknown' const throwingAuthService = createMockAuthService({ - authorize: () => Promise.reject(new Error('Auth service unavailable')), + authorize: async () => Promise.reject(new Error('Auth service unavailable')), }) OCPPAuthServiceFactory.setInstanceForTesting(stationId, throwingAuthService) diff --git a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-UpdateFirmware.test.ts b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-UpdateFirmware.test.ts index 6a294c00..b5d056f2 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-UpdateFirmware.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-UpdateFirmware.test.ts @@ -236,7 +236,7 @@ await describe('L01/L02 - UpdateFirmware', async () => { ) => Promise }, 'simulateFirmwareUpdateLifecycle', - () => Promise.resolve() + async () => Promise.resolve() ) }) @@ -291,7 +291,7 @@ await describe('L01/L02 - UpdateFirmware', async () => { ) => Promise }, 'simulateFirmwareUpdateLifecycle', - () => Promise.reject(new Error('firmware lifecycle error')) + async () => Promise.reject(new Error('firmware lifecycle error')) ) const request: OCPP20UpdateFirmwareRequest = { diff --git a/tests/charging-station/ocpp/2.0/OCPP20ResponseService-TransactionEvent.test.ts b/tests/charging-station/ocpp/2.0/OCPP20ResponseService-TransactionEvent.test.ts index 298eff55..0a7bff51 100644 --- a/tests/charging-station/ocpp/2.0/OCPP20ResponseService-TransactionEvent.test.ts +++ b/tests/charging-station/ocpp/2.0/OCPP20ResponseService-TransactionEvent.test.ts @@ -110,7 +110,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { idTokenInfo: { @@ -131,7 +131,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { idTokenInfo: { @@ -155,7 +155,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { idTokenInfo: { @@ -177,7 +177,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { chargingPriority: 5, @@ -196,7 +196,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = {} const requestPayload = buildTransactionEventRequest(TEST_TRANSACTION_UUID) @@ -213,7 +213,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { idTokenInfo: { @@ -234,7 +234,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { idTokenInfo: { @@ -255,7 +255,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { totalCost: 12.5, @@ -301,7 +301,7 @@ await describe('D01 - TransactionEvent Response', async () => { const mockDeauthTransaction = mock.method( OCPP20ServiceUtils, 'requestDeauthorizeTransaction', - () => Promise.resolve({} as OCPP20TransactionEventResponse) + async () => Promise.resolve({} as OCPP20TransactionEventResponse) ) const payload: OCPP20TransactionEventResponse = { idTokenInfo: { diff --git a/tests/charging-station/ocpp/OCPPServiceOperations.test.ts b/tests/charging-station/ocpp/OCPPServiceOperations.test.ts index 439c6fc8..3b4b82d8 100644 --- a/tests/charging-station/ocpp/OCPPServiceOperations.test.ts +++ b/tests/charging-station/ocpp/OCPPServiceOperations.test.ts @@ -490,7 +490,7 @@ await describe('OCPPServiceOperations', async () => { // Arrange const { station } = createMockChargingStation() injectMockAuthService(station, { - authorize: () => Promise.reject(new Error('Test auth service error')), + authorize: async () => Promise.reject(new Error('Test auth service error')), }) // Act diff --git a/tests/charging-station/ui-server/UIMCPServer.test.ts b/tests/charging-station/ui-server/UIMCPServer.test.ts index ce84b8b5..6b86bac1 100644 --- a/tests/charging-station/ui-server/UIMCPServer.test.ts +++ b/tests/charging-station/ui-server/UIMCPServer.test.ts @@ -417,7 +417,7 @@ await describe('UIMCPServer', async () => { await it('should return error response when both ocpp16Payload and ocpp20Payload are provided', async () => { const mockService = { - requestHandler: () => Promise.resolve(undefined), + requestHandler: async () => Promise.resolve(undefined), } const input = { ocpp16Payload: { idTag: 'TAG1' }, @@ -436,7 +436,7 @@ await describe('UIMCPServer', async () => { createMockChargingStationDataWithVersion(TEST_HASH_ID, OCPPVersion.VERSION_20) ) const mockService = { - requestHandler: () => Promise.resolve(undefined), + requestHandler: async () => Promise.resolve(undefined), } const input = { hashIds: [TEST_HASH_ID], @@ -478,7 +478,7 @@ await describe('UIMCPServer', async () => { await it('should return error response when service throws', async () => { const mockService = { - requestHandler: () => Promise.reject(new Error('Service failure')), + requestHandler: async () => Promise.reject(new Error('Service failure')), } const result = await server.callInvokeProcedure( @@ -494,7 +494,7 @@ await describe('UIMCPServer', async () => { await withMockTimers(t, ['setTimeout'], async () => { // Arrange - service returns undefined (broadcast/async) and never resolves const mockService = { - requestHandler: () => Promise.resolve(undefined), + requestHandler: async () => Promise.resolve(undefined), } // Act diff --git a/tests/performance/storage/MikroOrmStorage.test.ts b/tests/performance/storage/MikroOrmStorage.test.ts index 5a0fc6d2..54b4375b 100644 --- a/tests/performance/storage/MikroOrmStorage.test.ts +++ b/tests/performance/storage/MikroOrmStorage.test.ts @@ -69,10 +69,10 @@ function buildMockOrm (): { mockOrm: MockOrm; upsertCalls: unknown[] } { }, } const mockOrm: MockOrm = { - close: () => Promise.resolve(), + close: async () => Promise.resolve(), em: mockEm, schema: { - updateSchema: () => Promise.resolve(), + updateSchema: async () => Promise.resolve(), }, } return { mockOrm, upsertCalls } @@ -146,9 +146,9 @@ await describe('MikroOrmStorage', async () => { // Arrange const errorMock = t.mock.method(logger, 'error') const failingOrm: MockOrm = { - close: () => Promise.reject(new Error('close failed')), - em: { fork: () => ({}) as MockEntityManager, upsert: () => Promise.resolve({}) }, - schema: { updateSchema: () => Promise.resolve() }, + close: async () => Promise.reject(new Error('close failed')), + em: { fork: () => ({}) as MockEntityManager, upsert: async () => Promise.resolve({}) }, + schema: { updateSchema: async () => Promise.resolve() }, } storage.setOrm(failingOrm) @@ -291,12 +291,12 @@ await describe('MikroOrmStorage', async () => { const errorMock = t.mock.method(logger, 'error') const failingEm: MockEntityManager = { fork: () => failingEm, - upsert: () => Promise.reject(new Error('upsert failed')), + upsert: async () => Promise.reject(new Error('upsert failed')), } const failingOrm: MockOrm = { - close: () => Promise.resolve(), + close: async () => Promise.resolve(), em: failingEm, - schema: { updateSchema: () => Promise.resolve() }, + schema: { updateSchema: async () => Promise.resolve() }, } storage.setOrm(failingOrm) diff --git a/tests/performance/storage/MongoDBStorage.test.ts b/tests/performance/storage/MongoDBStorage.test.ts index d365fce7..66fff99f 100644 --- a/tests/performance/storage/MongoDBStorage.test.ts +++ b/tests/performance/storage/MongoDBStorage.test.ts @@ -84,8 +84,8 @@ function buildMockMongoClient (): { }, } const mockClient: MockMongoClient = { - close: () => Promise.resolve(), - connect: () => Promise.resolve(), + close: async () => Promise.resolve(), + connect: async () => Promise.resolve(), db: (_name: string) => mockDb, } return { collectionCalls, mockClient, replaceOneCalls } @@ -178,8 +178,8 @@ await describe('MongoDBStorage', async () => { // Arrange const errorMock = t.mock.method(logger, 'error') const failingClient: MockMongoClient = { - close: () => Promise.reject(new Error('close failed')), - connect: () => Promise.resolve(), + close: async () => Promise.reject(new Error('close failed')), + connect: async () => Promise.resolve(), db: () => ({}) as MockDb, } storage.setClient(failingClient) @@ -226,8 +226,8 @@ await describe('MongoDBStorage', async () => { // Arrange const errorMock = t.mock.method(logger, 'error') const failingClient: MockMongoClient = { - close: () => Promise.resolve(), - connect: () => Promise.reject(new Error('connect failed')), + close: async () => Promise.resolve(), + connect: async () => Promise.reject(new Error('connect failed')), db: () => ({}) as MockDb, } storage.setClient(failingClient) @@ -390,11 +390,11 @@ await describe('MongoDBStorage', async () => { // Arrange const errorMock = t.mock.method(logger, 'error') const failingCollection: MockCollection = { - replaceOne: () => Promise.reject(new Error('replaceOne failed')), + replaceOne: async () => Promise.reject(new Error('replaceOne failed')), } const failingClient: MockMongoClient = { - close: () => Promise.resolve(), - connect: () => Promise.resolve(), + close: async () => Promise.resolve(), + connect: async () => Promise.resolve(), db: () => ({ collection: () => failingCollection }) as unknown as MockDb, } storage.setClient(failingClient) -- 2.43.0