diff --git a/commitlint/fpHelpers.ts b/commitlint/fpHelpers.ts index 5af094d2..27db8b12 100644 --- a/commitlint/fpHelpers.ts +++ b/commitlint/fpHelpers.ts @@ -53,6 +53,11 @@ 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..5c55bd48 100644 --- a/commitlint/tests/testHelpers.ts +++ b/commitlint/tests/testHelpers.ts @@ -89,6 +89,29 @@ 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