Skip to content

Commit

Permalink
test_runner: unwrap error message in TAP reporter
Browse files Browse the repository at this point in the history
The TAP reporter unwraps errors that come from user code. It
was not properly unwrapping the error message when the failure
originated from a test hook. This lead to difficult to debug
errors when TAP output was used. This commit updates the TAP
reporter to properly unwrap the error message.

Fixes: nodejs#48941
PR-URL: nodejs#48942
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
cjihrig authored and RafaelGSS committed Aug 15, 2023
1 parent 2489588 commit 9326f0b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
7 changes: 3 additions & 4 deletions lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function jsToYaml(indent, name, value, seen) {
}

if (isErrorObj) {
const { kTestCodeFailure, kUnwrapErrors } = lazyLoadTest();
const { kUnwrapErrors } = lazyLoadTest();
const {
cause,
code,
Expand All @@ -198,15 +198,14 @@ function jsToYaml(indent, name, value, seen) {
errStack = cause?.stack ?? errStack;
errCode = cause?.code ?? errCode;
errName = cause?.name ?? errName;
errMsg = cause?.message ?? errMsg;

if (isAssertionLike(cause)) {
errExpected = cause.expected;
errActual = cause.actual;
errOperator = cause.operator ?? errOperator;
errIsAssertion = true;
}
if (failureType === kTestCodeFailure) {
errMsg = cause?.message ?? errMsg;
}
}

result += jsToYaml(indent, 'error', errMsg, seen);
Expand Down
28 changes: 14 additions & 14 deletions test/fixtures/test-runner/output/hooks.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ not ok 2 - before throws
duration_ms: *
type: 'suite'
failureType: 'hookFailed'
error: 'failed running before hook'
error: 'before'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down Expand Up @@ -86,7 +86,7 @@ not ok 3 - after throws
duration_ms: *
type: 'suite'
failureType: 'hookFailed'
error: 'failed running after hook'
error: 'after'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand All @@ -105,7 +105,7 @@ not ok 3 - after throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running beforeEach hook'
error: 'beforeEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand All @@ -124,7 +124,7 @@ not ok 3 - after throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running beforeEach hook'
error: 'beforeEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down Expand Up @@ -153,7 +153,7 @@ not ok 4 - beforeEach throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
error: 'afterEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand All @@ -172,7 +172,7 @@ not ok 4 - beforeEach throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
error: 'afterEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down Expand Up @@ -254,7 +254,7 @@ not ok 6 - afterEach when test fails
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
error: 'afterEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down Expand Up @@ -315,7 +315,7 @@ ok 8 - test hooks
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running before hook'
error: 'before'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand All @@ -334,7 +334,7 @@ ok 8 - test hooks
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running before hook'
error: 'before'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down Expand Up @@ -362,7 +362,7 @@ not ok 9 - t.before throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running beforeEach hook'
error: 'beforeEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand All @@ -381,7 +381,7 @@ not ok 9 - t.before throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running beforeEach hook'
error: 'beforeEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down Expand Up @@ -409,7 +409,7 @@ not ok 10 - t.beforeEach throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
error: 'afterEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand All @@ -428,7 +428,7 @@ not ok 10 - t.beforeEach throws
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
error: 'afterEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down Expand Up @@ -506,7 +506,7 @@ not ok 12 - afterEach when test fails
---
duration_ms: *
failureType: 'hookFailed'
error: 'failed running afterEach hook'
error: 'afterEach'
code: 'ERR_TEST_FAILURE'
stack: |-
*
Expand Down

0 comments on commit 9326f0b

Please sign in to comment.