]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix(tests): use async mock callbacks for all async method mocks
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 17 Apr 2026 16:16:51 +0000 (18:16 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 17 Apr 2026 16:17:29 +0000 (18:17 +0200)
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.

14 files changed:
tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-Firmware.test.ts
tests/charging-station/ocpp/1.6/OCPP16IncomingRequestService-RemoteStopUnlock.test.ts
tests/charging-station/ocpp/1.6/OCPP16Integration-Transactions.test.ts
tests/charging-station/ocpp/1.6/OCPP16RequestService-CallChain.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-CustomerInformation.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetBaseReport.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-GetLog.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-RequestStartTransaction.test.ts
tests/charging-station/ocpp/2.0/OCPP20IncomingRequestService-UpdateFirmware.test.ts
tests/charging-station/ocpp/2.0/OCPP20ResponseService-TransactionEvent.test.ts
tests/charging-station/ocpp/OCPPServiceOperations.test.ts
tests/charging-station/ui-server/UIMCPServer.test.ts
tests/performance/storage/MikroOrmStorage.test.ts
tests/performance/storage/MongoDBStorage.test.ts

index bc1646c33b10e07848cc7ce666a2720f3c3fe145..f981471960889bb56ab6bcf5e9240810e26a2495 100644 (file)
@@ -176,7 +176,7 @@ await describe('OCPP16IncomingRequestService — Firmware', async () => {
           updateFirmwareSimulation: (chargingStation: unknown) => Promise<void>
         },
         'updateFirmwareSimulation',
-        mock.fn(() => Promise.resolve())
+        mock.fn(async () => Promise.resolve())
       )
     })
 
@@ -249,7 +249,7 @@ await describe('OCPP16IncomingRequestService — Firmware', async () => {
           updateFirmwareSimulation: (chargingStation: unknown) => Promise<void>
         },
         'updateFirmwareSimulation',
-        mock.fn(() => Promise.reject(new Error('firmware simulation error')))
+        mock.fn(async () => Promise.reject(new Error('firmware simulation error')))
       )
 
       const request: OCPP16UpdateFirmwareRequest = {
index 09e304fb9e00750ede68c3664aae851677efb64a..62e192c661609afe99797917f4cb807ecf65113d 100644 (file)
@@ -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'))
       )
 
index ca4782451e068d98a59b49729966e75712319409..d7cac311d508248ad3889d7fb6b8537da796ec92 100644 (file)
@@ -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,
index ad2db1fb83b6cd95fec94e5fe1fd3541bf50da41..8d180eb9348f3ef6995c61e2ab225977798cc3fd 100644 (file)
@@ -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,
index be8c6d08b7c410ac312084effb8b73702eb628b0..ced41d158009705eb839a00d4bdb93d6679c455b 100644 (file)
@@ -174,7 +174,7 @@ await describe('N32 - CustomerInformation', async () => {
           ) => Promise<void>
         },
         'sendNotifyCustomerInformation',
-        () => Promise.resolve()
+        async () => Promise.resolve()
       )
     })
 
@@ -261,7 +261,7 @@ await describe('N32 - CustomerInformation', async () => {
           ) => Promise<void>
         },
         'sendNotifyCustomerInformation',
-        () => Promise.reject(new Error('notification error'))
+        async () => Promise.reject(new Error('notification error'))
       )
 
       const request: OCPP20CustomerInformationRequest = {
index e327a6bced223d7dc859065f64f3eff51677937b..f8f1054cb181e07abc61c9c95b80a9c7501cd7f5 100644 (file)
@@ -376,7 +376,7 @@ await describe('B07 - Get Base Report', async () => {
           ) => Promise<void>
         },
         'sendNotifyReportRequest',
-        () => Promise.resolve()
+        async () => Promise.resolve()
       )
     })
 
@@ -429,7 +429,7 @@ await describe('B07 - Get Base Report', async () => {
           ) => Promise<void>
         },
         'sendNotifyReportRequest',
-        () => Promise.reject(new Error('notify report error'))
+        async () => Promise.reject(new Error('notify report error'))
       )
 
       const request: OCPP20GetBaseReportRequest = {
index b5f709e414fa89667b7fa802f04505fa3568fd78..2f2bd02eb2131454e0ba779e14276cff794ab54f 100644 (file)
@@ -101,7 +101,7 @@ await describe('K01 - GetLog', async () => {
           ) => Promise<void>
         },
         'simulateLogUploadLifecycle',
-        () => Promise.resolve()
+        async () => Promise.resolve()
       )
     })
 
@@ -154,7 +154,7 @@ await describe('K01 - GetLog', async () => {
           ) => Promise<void>
         },
         'simulateLogUploadLifecycle',
-        () => Promise.reject(new Error('log upload error'))
+        async () => Promise.reject(new Error('log upload error'))
       )
 
       const request: OCPP20GetLogRequest = {
index f0ad2fbf577d673e65f27199f7ab8b6f36f889c5..d82bd18c96f82c0d1e1bd29059635dc4b92d9390 100644 (file)
@@ -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)
 
index 6a294c00e72c8b2692a3894cf8091774c551a1eb..b5d056f2fdf2b7bb87d8944f059cb478f8147f35 100644 (file)
@@ -236,7 +236,7 @@ await describe('L01/L02 - UpdateFirmware', async () => {
           ) => Promise<void>
         },
         'simulateFirmwareUpdateLifecycle',
-        () => Promise.resolve()
+        async () => Promise.resolve()
       )
     })
 
@@ -291,7 +291,7 @@ await describe('L01/L02 - UpdateFirmware', async () => {
           ) => Promise<void>
         },
         'simulateFirmwareUpdateLifecycle',
-        () => Promise.reject(new Error('firmware lifecycle error'))
+        async () => Promise.reject(new Error('firmware lifecycle error'))
       )
 
       const request: OCPP20UpdateFirmwareRequest = {
index 298eff55e7f5cb13806960deac89c51d83cfd062..0a7bff5141cdcc074cfe225578a88f7681bf400c 100644 (file)
@@ -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: {
index 439c6fc89838ce4b15f329ebc0a30f820f1bfe23..3b4b82d8e73aedf751bfcaa458cfa618401a3b6c 100644 (file)
@@ -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
index ce84b8b5affa568b30179272f19b1f867643ad8f..6b86bac100f3d00abb2b4bc8d50eb6b7234cc0c5 100644 (file)
@@ -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
index 5a0fc6d264e953bd09e4543d0a4e2a12d62cd19a..54b4375bebfa4f414035c4f375357c2fa4e1aade 100644 (file)
@@ -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)
 
index d365fce7e0ab5445bb29805e132c5030d47825fd..66fff99f1671b7f752eea3b1e15247f9e583a7ca 100644 (file)
@@ -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)