Brak opisu

ErrorUtils.test.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* eslint-disable @typescript-eslint/no-unsafe-member-access */
  2. import { expect } from '@std/expect'
  3. import { describe, it } from 'node:test'
  4. import type { ChargingStation } from '../../src/charging-station/index.js'
  5. import {
  6. FileType,
  7. GenericStatus,
  8. IncomingRequestCommand,
  9. MessageType,
  10. RequestCommand,
  11. } from '../../src/types/index.js'
  12. import {
  13. handleFileException,
  14. handleIncomingRequestError,
  15. handleSendMessageError,
  16. } from '../../src/utils/ErrorUtils.js'
  17. import { logger } from '../../src/utils/Logger.js'
  18. await describe('ErrorUtils test suite', async () => {
  19. const chargingStation = {
  20. logPrefix: () => 'CS-TEST |',
  21. } as ChargingStation
  22. await it('Verify handleFileException()', t => {
  23. t.mock.method(console, 'warn')
  24. t.mock.method(console, 'error')
  25. t.mock.method(logger, 'warn')
  26. t.mock.method(logger, 'error')
  27. const error = new Error()
  28. error.code = 'ENOENT'
  29. expect(() => {
  30. handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {})
  31. }).toThrow(error)
  32. expect(() => {
  33. handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
  34. throwError: false,
  35. })
  36. }).not.toThrow()
  37. expect(logger.warn.mock.calls.length).toBe(1)
  38. expect(logger.error.mock.calls.length).toBe(1)
  39. expect(() => {
  40. handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
  41. consoleOut: true,
  42. })
  43. }).toThrow(error)
  44. expect(() => {
  45. handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix |', {
  46. consoleOut: true,
  47. throwError: false,
  48. })
  49. }).not.toThrow()
  50. expect(console.warn.mock.calls.length).toBe(1)
  51. expect(console.error.mock.calls.length).toBe(1)
  52. })
  53. await it('Verify handleSendMessageError()', t => {
  54. t.mock.method(logger, 'error')
  55. t.mock.method(chargingStation, 'logPrefix')
  56. const error = new Error()
  57. expect(() => {
  58. handleSendMessageError(
  59. chargingStation,
  60. RequestCommand.BOOT_NOTIFICATION,
  61. MessageType.CALL_MESSAGE,
  62. error
  63. )
  64. }).not.toThrow()
  65. expect(() => {
  66. handleSendMessageError(
  67. chargingStation,
  68. RequestCommand.BOOT_NOTIFICATION,
  69. MessageType.CALL_MESSAGE,
  70. error,
  71. { throwError: true }
  72. )
  73. }).toThrow(error)
  74. expect(chargingStation.logPrefix.mock.calls.length).toBe(2)
  75. expect(logger.error.mock.calls.length).toBe(2)
  76. })
  77. await it('Verify handleIncomingRequestError()', t => {
  78. t.mock.method(logger, 'error')
  79. t.mock.method(chargingStation, 'logPrefix')
  80. const error = new Error()
  81. expect(() => {
  82. handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error)
  83. }).toThrow(error)
  84. expect(() => {
  85. handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, {
  86. throwError: false,
  87. })
  88. }).not.toThrow()
  89. const errorResponse = {
  90. status: GenericStatus.Rejected,
  91. }
  92. expect(
  93. handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, {
  94. errorResponse,
  95. throwError: false,
  96. })
  97. ).toStrictEqual(errorResponse)
  98. expect(chargingStation.logPrefix.mock.calls.length).toBe(3)
  99. expect(logger.error.mock.calls.length).toBe(3)
  100. })
  101. })