Skip to content

Commit

Permalink
test_runner: add extra fields in AssertionError YAML
Browse files Browse the repository at this point in the history
Many TAP reporters offer special-case handling of YAML objects
containing `expected`, `actual`, and `operator` fields, as produced by
`AssertionError` and similar userland Error classes.

PR-URL: nodejs/node#44952
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
(cherry picked from commit e260b373f13c150eb5bdf4c336d4b6b764b59c8e)
  • Loading branch information
bengl authored and MoLow committed Feb 2, 2023
1 parent 15b9cf6 commit 5fb0ffe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/internal/test_runner/tap_stream.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/cb7e0c59df10a42cd6930ca7f99d3acee1ce7627/lib/internal/test_runner/tap_stream.js
// https://github.com/nodejs/node/blob/e260b373f13c150eb5bdf4c336d4b6b764b59c8e/lib/internal/test_runner/tap_stream.js

'use strict'

Expand Down Expand Up @@ -183,17 +183,30 @@ function jsToYaml (indent, name, value) {
code,
failureType,
message,
expected,
actual,
operator,
stack
} = value
let errMsg = message ?? '<unknown error>'
let errStack = stack
let errCode = code
let errExpected = expected
let errActual = actual
let errOperator = operator
let errIsAssertion = isAssertionLike(value)

// If the ERR_TEST_FAILURE came from an error provided by user code,
// then try to unwrap the original error message and stack.
if (code === 'ERR_TEST_FAILURE' && kUnwrapErrors.has(failureType)) {
errStack = cause?.stack ?? errStack
errCode = cause?.code ?? errCode
if (isAssertionLike(cause)) {
errExpected = cause.expected
errActual = cause.actual
errOperator = cause.operator ?? errOperator
errIsAssertion = true
}
if (failureType === kTestCodeFailure) {
errMsg = cause?.message ?? errMsg
}
Expand All @@ -205,6 +218,14 @@ function jsToYaml (indent, name, value) {
result += jsToYaml(indent, 'code', errCode)
}

if (errIsAssertion) {
result += jsToYaml(indent, 'expected', errExpected)
result += jsToYaml(indent, 'actual', errActual)
if (errOperator) {
result += jsToYaml(indent, 'operator', errOperator)
}
}

if (typeof errStack === 'string') {
const frames = []

Expand Down Expand Up @@ -233,4 +254,8 @@ function jsToYaml (indent, name, value) {
return result
}

function isAssertionLike (value) {
return value && typeof value === 'object' && 'expected' in value && 'actual' in value
}

module.exports = { TapStream }
3 changes: 3 additions & 0 deletions test/message/test_runner_desctibe_it.out
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ not ok 13 - async assertion fail
true !== false

code: 'ERR_ASSERTION'
expected: false
actual: true
operator: 'strictEqual'
stack: |-
*
*
Expand Down
3 changes: 3 additions & 0 deletions test/message/test_runner_output.out
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ not ok 13 - async assertion fail
true !== false

code: 'ERR_ASSERTION'
expected: false
actual: true
operator: 'strictEqual'
stack: |-
*
*
Expand Down

0 comments on commit 5fb0ffe

Please sign in to comment.