From 1cd0dbc9eb83141f35a1f82d4f13ca15c319f8b2 Mon Sep 17 00:00:00 2001 From: Eugene Terehov Date: Sat, 2 Jan 2021 09:44:11 +0100 Subject: [PATCH] Fix #78: Allow null and undefined parameters again --- src/LoggerHelper.ts | 4 ++- tests/parameter.test.ts | 57 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/parameter.test.ts diff --git a/src/LoggerHelper.ts b/src/LoggerHelper.ts index 2ef10e5f..52665e19 100644 --- a/src/LoggerHelper.ts +++ b/src/LoggerHelper.ts @@ -334,6 +334,8 @@ export class LoggerHelper { return value; }; - return LoggerHelper.cloneObjectRecursively(obj, maskValuesFn); + return obj != null + ? LoggerHelper.cloneObjectRecursively(obj, maskValuesFn) + : obj; } } diff --git a/tests/parameter.test.ts b/tests/parameter.test.ts new file mode 100644 index 00000000..a7bd68f4 --- /dev/null +++ b/tests/parameter.test.ts @@ -0,0 +1,57 @@ +import "ts-jest"; +import { IStd, Logger } from "../src"; +import { doesLogContain } from "./helper"; +import exp = require("constants"); + +const stdOut: string[] = []; +const stdErr: string[] = []; + +const logger: Logger = new Logger({ + stdOut: { + write: (print: string) => { + stdOut.push(print); + }, + }, + stdErr: { + write: (print: string) => { + stdErr.push(print); + }, + }, +}); + +describe("Logger: Parameter", () => { + beforeEach(() => { + stdOut.length = 0; + stdErr.length = 0; + }); + + test("undefined", (): void => { + logger.silly(undefined); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "undefined")).toBeTruthy(); + }); + + test("null", (): void => { + logger.silly(null); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "null")).toBeTruthy(); + }); + + test("boolean", (): void => { + logger.silly(true); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "true")).toBeTruthy(); + }); + + test("number", (): void => { + logger.silly(0); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "0")).toBeTruthy(); + }); + + test("string", (): void => { + logger.silly("string"); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "string")).toBeTruthy(); + }); +});