refactor: cleanup default params in error handlers
[e-mobility-charging-stations-simulator.git] / tests / utils / ErrorUtils.test.ts
index 59c455f94df9f7ae4aa9a70efda1acb1ab19b45e..7bbf4db2d700dc0b7effac670eb4e14668e598b1 100644 (file)
@@ -1,3 +1,4 @@
+/* eslint-disable @typescript-eslint/no-unsafe-member-access */
 import { describe, it } from 'node:test'
 
 import { expect } from 'expect'
@@ -13,9 +14,9 @@ import {
 import {
   handleFileException,
   handleIncomingRequestError,
-  handleSendMessageError,
-  setDefaultErrorParams
+  handleSendMessageError
 } from '../../src/utils/ErrorUtils.js'
+import { logger } from '../../src/utils/Logger.js'
 
 await describe('ErrorUtils test suite', async () => {
   // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
@@ -23,7 +24,11 @@ await describe('ErrorUtils test suite', async () => {
     logPrefix: () => 'CS-TEST |'
   } as ChargingStation
 
-  await it('Verify handleFileException()', () => {
+  await it('Verify handleFileException()', t => {
+    t.mock.method(console, 'warn')
+    t.mock.method(console, 'error')
+    t.mock.method(logger, 'warn')
+    t.mock.method(logger, 'error')
     const error = new Error()
     error.code = 'ENOENT'
     expect(() => {
@@ -34,9 +39,26 @@ await describe('ErrorUtils test suite', async () => {
         throwError: false
       })
     }).not.toThrow()
+    expect(logger.warn.mock.calls.length).toBe(1)
+    expect(logger.error.mock.calls.length).toBe(1)
+    expect(() => {
+      handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
+        consoleOut: true
+      })
+    }).toThrow(error)
+    expect(() => {
+      handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
+        throwError: false,
+        consoleOut: true
+      })
+    }).not.toThrow()
+    expect(console.warn.mock.calls.length).toBe(1)
+    expect(console.error.mock.calls.length).toBe(1)
   })
 
-  await it('Verify handleSendMessageError()', () => {
+  await it('Verify handleSendMessageError()', t => {
+    t.mock.method(logger, 'error')
+    t.mock.method(chargingStation, 'logPrefix')
     const error = new Error()
     expect(() => {
       handleSendMessageError(
@@ -55,9 +77,13 @@ await describe('ErrorUtils test suite', async () => {
         { throwError: true }
       )
     }).toThrow(error)
+    expect(chargingStation.logPrefix.mock.calls.length).toBe(2)
+    expect(logger.error.mock.calls.length).toBe(2)
   })
 
-  await it('Verify handleIncomingRequestError()', () => {
+  await it('Verify handleIncomingRequestError()', t => {
+    t.mock.method(logger, 'error')
+    t.mock.method(chargingStation, 'logPrefix')
     const error = new Error()
     expect(() => {
       handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error)
@@ -76,25 +102,7 @@ await describe('ErrorUtils test suite', async () => {
         errorResponse
       })
     ).toStrictEqual(errorResponse)
-  })
-
-  await it('Verify setDefaultErrorParams()', () => {
-    expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false })
-    expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({
-      throwError: false,
-      consoleOut: false
-    })
-    expect(setDefaultErrorParams({ throwError: false, consoleOut: true })).toStrictEqual({
-      throwError: false,
-      consoleOut: true
-    })
-    expect(setDefaultErrorParams({ throwError: true, consoleOut: true })).toStrictEqual({
-      throwError: true,
-      consoleOut: true
-    })
-    expect(setDefaultErrorParams({}, { throwError: false, consoleOut: false })).toStrictEqual({
-      throwError: false,
-      consoleOut: false
-    })
+    expect(chargingStation.logPrefix.mock.calls.length).toBe(3)
+    expect(logger.error.mock.calls.length).toBe(3)
   })
 })