Skip to content

Commit

Permalink
Updating tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
igoramadas committed May 5, 2023
1 parent 889fbfa commit 42c6a0a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
"temp-dir": "./coverage",
"all": true,
"reporter": [
"lcov",
"text"
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions test/test-a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion test/test-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ describe("Anyhow Message Tests", function () {
const obj = {
level1: {
level2: {
someDate: new Date(),
level3: {
level4: {
level5: {
Expand All @@ -297,7 +298,10 @@ describe("Anyhow Message Tests", function () {
},
empty: {}
},
accessToken: "a12b3c"
accessToken: "a12b3c",
function(x) {
return x
}
}

obj.level1["circular"] = obj
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions test/test-uncaught.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ try {
})

it("Catch and log uncaught exception", function (done) {
this.timeout(5000)
anyhow.setOptions({uncaughtExceptions: true})
uncaughtDone = done

Expand All @@ -97,6 +98,7 @@ try {
})

it("Catch and log unhandled rejection", function (done) {
this.timeout(5000)
anyhow.setOptions({unhandledRejections: true})
unhandledDone = done

Expand Down

0 comments on commit 42c6a0a

Please sign in to comment.