From: Jérôme Benoit Date: Wed, 26 Jul 2023 21:52:26 +0000 (+0200) Subject: test: code cleanup X-Git-Tag: v1.2.20~125 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3dcf7b670fa7a199022988902032d4225f27865e;p=e-mobility-charging-stations-simulator.git test: code cleanup Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 5dbe2fb4..d09d8b1a 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -3,6 +3,7 @@ import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv'; +import { isDate } from 'date-fns'; import { OCPP16Constants } from './1.6/OCPP16Constants'; import { OCPP20Constants } from './2.0/OCPP20Constants'; @@ -145,7 +146,7 @@ export class OCPPServiceUtils { public static convertDateToISOString(obj: T): void { for (const key in obj) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion - if (obj![key] instanceof Date) { + if (isDate(obj![key])) { // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion (obj![key] as string) = (obj![key] as Date).toISOString(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion diff --git a/test/utils/Utils.test.ts b/test/utils/Utils.test.ts index 67da558e..b75197c2 100644 --- a/test/utils/Utils.test.ts +++ b/test/utils/Utils.test.ts @@ -1,4 +1,4 @@ -import { hoursToMilliseconds, hoursToSeconds } from 'date-fns'; +import { hoursToMilliseconds, hoursToSeconds, isValid } from 'date-fns'; import { expect } from 'expect'; import { Constants } from '../../src/utils/Constants'; @@ -84,8 +84,7 @@ describe('Utils test suite', () => { it('Verify convertToDate()', () => { expect(convertToDate(undefined)).toBe(undefined); expect(convertToDate(null)).toBe(null); - const invalidDate = convertToDate(''); - expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false); + expect(isValid(convertToDate(''))).toBe(false); expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); const dateStr = '2020-01-01T00:00:00.000Z'; let date = convertToDate(dateStr);