From fba0276c3dfb73f560af1433895052982d5c3ce3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 28 Jan 2024 16:14:48 +0100 Subject: [PATCH] test: add tests for BaseError custom error MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/exception/OCPPError.ts | 9 ++------- tests/exception/BaseError.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 tests/exception/BaseError.test.ts diff --git a/src/exception/OCPPError.ts b/src/exception/OCPPError.ts index 101bf5db..41414fe9 100644 --- a/src/exception/OCPPError.ts +++ b/src/exception/OCPPError.ts @@ -1,17 +1,12 @@ // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. import { BaseError } from './BaseError.js' -import { - type ErrorType, - type IncomingRequestCommand, - type JsonType, - type RequestCommand -} from '../types/index.js' +import type { ErrorType, IncomingRequestCommand, JsonType, RequestCommand } from '../types/index.js' import { Constants } from '../utils/index.js' export class OCPPError extends BaseError { code: ErrorType - command?: RequestCommand | IncomingRequestCommand + command: RequestCommand | IncomingRequestCommand details?: JsonType constructor ( diff --git a/tests/exception/BaseError.test.ts b/tests/exception/BaseError.test.ts new file mode 100644 index 00000000..947bac2b --- /dev/null +++ b/tests/exception/BaseError.test.ts @@ -0,0 +1,24 @@ +import { describe, it } from 'node:test' + +import { expect } from 'expect' + +import { BaseError } from '../../src/exception/BaseError.js' + +await describe('BaseError test suite', async () => { + await it('Verify that BaseError can be instantiated', () => { + const baseError = new BaseError() + expect(baseError).toBeInstanceOf(BaseError) + expect(baseError.name).toBe('BaseError') + expect(baseError.message).toBe('') + expect(typeof baseError.stack === 'string').toBe(true) + expect(baseError.stack).not.toBe('') + expect(baseError.cause).toBeUndefined() + expect(baseError.date).toBeInstanceOf(Date) + }) + + await it('Verify that BaseError can be instantiated with a message', () => { + const baseError = new BaseError('Test message') + expect(baseError).toBeInstanceOf(BaseError) + expect(baseError.message).toBe('Test message') + }) +}) -- 2.34.1