Skip to content

Commit ff174b0

Browse files
benglRafaelGSS
authored andcommitted
test_runner: add extra fields in AssertionError YAML
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: #44952 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b39dcde commit ff174b0

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

lib/internal/test_runner/tap_stream.js

+25
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,30 @@ function jsToYaml(indent, name, value) {
179179
code,
180180
failureType,
181181
message,
182+
expected,
183+
actual,
184+
operator,
182185
stack,
183186
} = value;
184187
let errMsg = message ?? '<unknown error>';
185188
let errStack = stack;
186189
let errCode = code;
190+
let errExpected = expected;
191+
let errActual = actual;
192+
let errOperator = operator;
193+
let errIsAssertion = isAssertionLike(value);
187194

188195
// If the ERR_TEST_FAILURE came from an error provided by user code,
189196
// then try to unwrap the original error message and stack.
190197
if (code === 'ERR_TEST_FAILURE' && kUnwrapErrors.has(failureType)) {
191198
errStack = cause?.stack ?? errStack;
192199
errCode = cause?.code ?? errCode;
200+
if (isAssertionLike(cause)) {
201+
errExpected = cause.expected;
202+
errActual = cause.actual;
203+
errOperator = cause.operator ?? errOperator;
204+
errIsAssertion = true;
205+
}
193206
if (failureType === kTestCodeFailure) {
194207
errMsg = cause?.message ?? errMsg;
195208
}
@@ -201,6 +214,14 @@ function jsToYaml(indent, name, value) {
201214
result += jsToYaml(indent, 'code', errCode);
202215
}
203216

217+
if (errIsAssertion) {
218+
result += jsToYaml(indent, 'expected', errExpected);
219+
result += jsToYaml(indent, 'actual', errActual);
220+
if (errOperator) {
221+
result += jsToYaml(indent, 'operator', errOperator);
222+
}
223+
}
224+
204225
if (typeof errStack === 'string') {
205226
const frames = [];
206227

@@ -229,4 +250,8 @@ function jsToYaml(indent, name, value) {
229250
return result;
230251
}
231252

253+
function isAssertionLike(value) {
254+
return value && typeof value === 'object' && 'expected' in value && 'actual' in value;
255+
}
256+
232257
module.exports = { TapStream };

test/message/test_runner_describe_it.out

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ not ok 13 - async assertion fail
123123
true !== false
124124

125125
code: 'ERR_ASSERTION'
126+
expected: false
127+
actual: true
128+
operator: 'strictEqual'
126129
stack: |-
127130
*
128131
*

test/message/test_runner_output.out

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ not ok 13 - async assertion fail
133133
true !== false
134134

135135
code: 'ERR_ASSERTION'
136+
expected: false
137+
actual: true
138+
operator: 'strictEqual'
136139
stack: |-
137140
*
138141
*

0 commit comments

Comments
 (0)