Skip to content

Commit

Permalink
fix: display undefined in error reports
Browse files Browse the repository at this point in the history
  • Loading branch information
wankdanker authored and jwerle committed May 15, 2023
1 parent b5e2fc2 commit 113c0b9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ class Test {
report(' ---')
report(` operator: ${operator}`)

let ex = JSON.stringify(expected, null, ' ') || 'undefined'
let ac = JSON.stringify(actual, null, ' ') || 'undefined'
let ex = toJSON(expected)
let ac = toJSON(actual)
if (Math.max(ex.length, ac.length) > 65) {
ex = ex.replace(NEW_LINE_REGEX, '\n ')
ac = ac.replace(NEW_LINE_REGEX, '\n ')
Expand Down Expand Up @@ -533,3 +533,18 @@ function rethrowImmediate (err) {
*/
function rethrow () { throw err }
}

/**
* JSON.stringify `thing` while preserving `undefined` values in
* the output.
*
* @param {unknown} thing
* @returns {string}
*/
function toJSON (thing) {
/** @type {(_k: string, v: unknown) => unknown} */
const replacer = (_k, v) => (v === undefined) ? '_tz_undefined_tz_' : v

const json = JSON.stringify(thing, replacer, ' ') || 'undefined'
return json.replace(/"_tz_undefined_tz_"/g, 'undefined')
}

0 comments on commit 113c0b9

Please sign in to comment.