From 42c6a0accf463a8f1fc8fcffe4d5fef34a207e39 Mon Sep 17 00:00:00 2001 From: Igor Ramadas Date: Fri, 5 May 2023 17:10:13 +0200 Subject: [PATCH] Updating tests. --- package.json | 1 - src/index.ts | 8 +++++--- test/test-a.ts | 13 +++++++++++++ test/test-parser.ts | 12 +++++++++++- test/test-uncaught.ts | 2 ++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index efab871..c0f253c 100755 --- a/package.json +++ b/package.json @@ -81,7 +81,6 @@ "nyc": { "extends": "@istanbuljs/nyc-config-typescript", "temp-dir": "./coverage", - "all": true, "reporter": [ "lcov", "text" diff --git a/src/index.ts b/src/index.ts index cabb5c5..cbb02b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -355,20 +355,22 @@ class Anyhow { * @param newOptions New options object to be applied. */ private applyOptions = (newOptions: AnyhowOptions): void => { - if (newOptions.levels && newOptions.levels.length > 0) { + if (newOptions.levels?.length > 0) { newOptions.levels = dedupArray(newOptions.levels) } else { newOptions.levels = [] } - if (newOptions.preprocessors && newOptions.preprocessors.length > 0) { + if (newOptions.preprocessors?.length > 0) { newOptions.preprocessors = dedupArray(newOptions.preprocessors) } else { newOptions.preprocessors = [] } - if (newOptions.preprocessorOptions && newOptions.preprocessorOptions.maskedFields && newOptions.preprocessorOptions.maskedFields.length > 0) { + if (newOptions.preprocessorOptions?.maskedFields?.length > 0) { newOptions.preprocessorOptions.maskedFields = dedupArray(newOptions.preprocessorOptions.maskedFields) + } else if (newOptions.preprocessorOptions) { + newOptions.preprocessorOptions.maskedFields = [] } if (newOptions.styles) { diff --git a/test/test-a.ts b/test/test-a.ts index 7c9f300..900843c 100644 --- a/test/test-a.ts +++ b/test/test-a.ts @@ -38,6 +38,17 @@ describe("Anyhow Main Tests", function () { } }) + it("Passing empty options should revert to the defaults", function (done) { + anyhow.options.appName = "another" + anyhow.options = null + + if (anyhow.options.appName == "another") { + done("Calling setOptions passing null should have reverted to the defaults") + } else { + done() + } + }) + it("Log info to console based on simple arguments", function (done) { anyhow.setup("console") anyhow.uncaughtExceptions = true @@ -284,6 +295,8 @@ describe("Anyhow Main Tests", function () { anyhow.setOptions({styles: null}) anyhow.info("Styles fully disabled") + anyhow.setOptions({styles: {info: ["yellow"]}}) + anyhow.info("Info should now be yellow") }) it("Do not debug log if debug is not part of levels", function (done) { diff --git a/test/test-parser.ts b/test/test-parser.ts index 875faad..d0b5fe7 100644 --- a/test/test-parser.ts +++ b/test/test-parser.ts @@ -279,6 +279,7 @@ describe("Anyhow Message Tests", function () { const obj = { level1: { level2: { + someDate: new Date(), level3: { level4: { level5: { @@ -297,7 +298,10 @@ describe("Anyhow Message Tests", function () { }, empty: {} }, - accessToken: "a12b3c" + accessToken: "a12b3c", + function(x) { + return x + } } obj.level1["circular"] = obj @@ -327,6 +331,12 @@ describe("Anyhow Message Tests", function () { anyhow.setOptions({preprocessors: ["cleanup", "friendlyErrors", "maskSecrets"], preprocessorOptions: {clone: false}}) parser.getMessage(arr2) + parser.getMessage(1) + parser.getMessage("a") + parser.getMessage(new Date()) + parser.getMessage(function (a) { + return a + }) const tLast = performance.now() diff --git a/test/test-uncaught.ts b/test/test-uncaught.ts index 332cb04..a644494 100644 --- a/test/test-uncaught.ts +++ b/test/test-uncaught.ts @@ -84,6 +84,7 @@ try { }) it("Catch and log uncaught exception", function (done) { + this.timeout(5000) anyhow.setOptions({uncaughtExceptions: true}) uncaughtDone = done @@ -97,6 +98,7 @@ try { }) it("Catch and log unhandled rejection", function (done) { + this.timeout(5000) anyhow.setOptions({unhandledRejections: true}) unhandledDone = done