From 3dcf7b670fa7a199022988902032d4225f27865e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 26 Jul 2023 23:52:26 +0200 Subject: [PATCH] test: code cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ocpp/OCPPServiceUtils.ts | 3 ++- test/utils/Utils.test.ts | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) 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); -- 2.34.1