Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: add extra fields in AssertionError YAML #44952

Merged
merged 1 commit into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/internal/test_runner/tap_stream.js
Expand Up @@ -179,17 +179,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 @@ -201,6 +214,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 @@ -229,4 +250,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_describe_it.out
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
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