test: add tests
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 7 Jun 2024 12:04:44 +0000 (14:04 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 7 Jun 2024 12:04:44 +0000 (14:04 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
tests/exception/OCPPError.test.ts [new file with mode: 0644]
tests/utils/ErrorUtils.test.ts

diff --git a/tests/exception/OCPPError.test.ts b/tests/exception/OCPPError.test.ts
new file mode 100644 (file)
index 0000000..c6e4bc4
--- /dev/null
@@ -0,0 +1,23 @@
+import { describe, it } from 'node:test'
+
+import { expect } from 'expect'
+
+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 () => {
+  await it('Verify that OCPPError can be instantiated', () => {
+    const ocppError = new OCPPError(ErrorType.GENERIC_ERROR, '')
+    expect(ocppError).toBeInstanceOf(OCPPError)
+    expect(ocppError.name).toBe('OCPPError')
+    expect(ocppError.message).toBe('')
+    expect(ocppError.code).toBe(ErrorType.GENERIC_ERROR)
+    expect(ocppError.command).toBe(Constants.UNKNOWN_OCPP_COMMAND)
+    expect(ocppError.details).toBeUndefined()
+    expect(typeof ocppError.stack === 'string').toBe(true)
+    expect(ocppError.stack).not.toBe('')
+    expect(ocppError.cause).toBeUndefined()
+    expect(ocppError.date).toBeInstanceOf(Date)
+  })
+})
index fd932b41ab5ae31e1011e9efa5e6d8f593f97a7c..486a031635af4a0e00ae15957b9135a00265deb4 100644 (file)
@@ -2,9 +2,23 @@ import { describe, it } from 'node:test'
 
 import { expect } from 'expect'
 
-import { setDefaultErrorParams } from '../../src/utils/ErrorUtils.js'
+import { FileType } from '../../src/types/index.js'
+import { handleFileException, setDefaultErrorParams } from '../../src/utils/ErrorUtils.js'
 
 await describe('ErrorUtils test suite', async () => {
+  await it('Verify handleFileException()', () => {
+    const error = new Error()
+    error.code = 'ENOENT'
+    expect(() => {
+      handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix', {})
+    }).toThrow(error)
+    expect(() => {
+      handleFileException('path/to/module.js', FileType.Authorization, error, 'log prefix', {
+        throwError: false
+      })
+    }).not.toThrow()
+  })
+
   await it('Verify setDefaultErrorParams()', () => {
     expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false })
     expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({