From 73e00624abc5c79a056f90bd7cdeeac178b3ab28 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Tue, 10 Sep 2024 00:46:28 +0800 Subject: [PATCH] WIP1-4 --- commitlint/fpHelpers.ts | 3 +++ commitlint/tests/testHelpers.ts | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/commitlint/fpHelpers.ts b/commitlint/fpHelpers.ts index 5af094d2..477cc350 100644 --- a/commitlint/fpHelpers.ts +++ b/commitlint/fpHelpers.ts @@ -53,6 +53,9 @@ export class OptionStatic { export class TypeHelpers { // because instanceof doesn't work with primitive types (e.g. String), taken from https://stackoverflow.com/a/58184883/544947 public static IsInstanceOf(variable: any, type: any) { + if (variable === null || variable === undefined) { + throw new Error ("Invalid 'variable' parameter passed in: null or undefined"); + } let res: boolean = false; if (typeof type == "string") { res = typeof variable == type.toLowerCase(); diff --git a/commitlint/tests/testHelpers.ts b/commitlint/tests/testHelpers.ts index c2a05026..e8d2f228 100644 --- a/commitlint/tests/testHelpers.ts +++ b/commitlint/tests/testHelpers.ts @@ -89,6 +89,17 @@ test("testing TypeHelpers.IsInstanceOf", () => { expect(TypeHelpers.IsInstanceOf(bar, Foo)).toBe(false); }); +test("testing TypeHelpers.IsInstanceOf exceptions", () => { + let strNull = null; + expect(() => TypeHelpers.IsInstanceOf(strNull, String)).toThrowError("Invalid"); + expect(() => TypeHelpers.IsInstanceOf(strNull, String)).toThrowError("parameter"); + expect(() => TypeHelpers.IsInstanceOf(strNull, String)).toThrowError("null"); + let strUndefined = undefined; + expect(() => TypeHelpers.IsInstanceOf(strUndefined, String)).toThrowError("Invalid"); + expect(() => TypeHelpers.IsInstanceOf(strUndefined, String)).toThrowError("parameter"); + expect(() => TypeHelpers.IsInstanceOf(strUndefined, String)).toThrowError("undefined"); +}); + export function runCommitLintOnMsg(inputMsg: string) { // FIXME: should we .lowerCase().startsWith("win") in case it starts // returning Win64 in the future? thing is, our CI doesn't like this