File tree 3 files changed +58
-22
lines changed
v-next/hardhat-node-test-reporter/src
3 files changed +58
-22
lines changed Original file line number Diff line number Diff line change @@ -4,24 +4,25 @@ import { inspect } from "node:util";
4
4
import chalk from "chalk" ;
5
5
import { diff } from "jest-diff" ;
6
6
7
+ import {
8
+ cleanupTestFailError ,
9
+ isCancelledByParentError ,
10
+ } from "./node-test-error-utils.js" ;
11
+
7
12
// TODO: Clean up the node internal fames from the stack trace
8
13
export function formatError ( error : Error ) : string {
9
- if ( "code" in error && error . code === "ERR_TEST_FAILURE" ) {
10
- if ( error . cause instanceof Error ) {
11
- error = error . cause ;
12
- }
13
-
14
- if ( "failureType" in error && error . failureType === "cancelledByParent" ) {
15
- return (
16
- chalk . red ( "Test cancelled by parent error" ) +
17
- "\n" +
18
- chalk . gray (
19
- " This test was cancelled due to an error in its parent suite/it or test/it, or in one of its before/beforeEach" ,
20
- )
21
- ) ;
22
- }
14
+ if ( isCancelledByParentError ( error ) ) {
15
+ return (
16
+ chalk . red ( "Test cancelled by parent error" ) +
17
+ "\n" +
18
+ chalk . gray (
19
+ " This test was cancelled due to an error in its parent suite/it or test/it, or in one of its before/beforeEach" ,
20
+ )
21
+ ) ;
23
22
}
24
23
24
+ error = cleanupTestFailError ( error ) ;
25
+
25
26
const defaultFormat = inspect ( error ) ;
26
27
const indexOfMessage = defaultFormat . indexOf ( error . message ) ;
27
28
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Returns true if the error represents that a test/suite failed because one of
3
+ * its subtests failed.
4
+ */
5
+ export function isSubtestFailedError ( error : Error ) : boolean {
6
+ return (
7
+ "code" in error &&
8
+ "failureType" in error &&
9
+ error . code === "ERR_TEST_FAILURE" &&
10
+ error . failureType === "subtestsFailed"
11
+ ) ;
12
+ }
13
+
14
+ /**
15
+ * Returns true if the error represents that a test was cancelled because its
16
+ * parent failed.
17
+ */
18
+ export function isCancelledByParentError ( error : Error ) : boolean {
19
+ return (
20
+ "code" in error &&
21
+ "failureType" in error &&
22
+ error . code === "ERR_TEST_FAILURE" &&
23
+ error . failureType === "cancelledByParent"
24
+ ) ;
25
+ }
26
+
27
+ /**
28
+ * Cleans the test:fail event error, as it's usually wrapped by a node:test
29
+ * error.
30
+ */
31
+ export function cleanupTestFailError ( error : Error ) : Error {
32
+ if (
33
+ "code" in error &&
34
+ error . code === "ERR_TEST_FAILURE" &&
35
+ error . cause instanceof Error
36
+ ) {
37
+ return error . cause ;
38
+ }
39
+
40
+ return error ;
41
+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
11
11
formatSlowTestInfo ,
12
12
Failure ,
13
13
} from "./formatting.js" ;
14
+ import { isSubtestFailedError } from "./node-test-error-utils.js" ;
14
15
import {
15
16
TestEventData ,
16
17
TestEventSource ,
@@ -56,14 +57,7 @@ export default async function* customReporter(
56
57
// If a suite failed for a reason other than a subtest failing, we
57
58
// want to print its failure, so we push it to the failures array.
58
59
if ( event . type === "test:fail" ) {
59
- if (
60
- ! (
61
- "code" in event . data . details . error &&
62
- "failureType" in event . data . details . error &&
63
- event . data . details . error . code === "ERR_TEST_FAILURE" &&
64
- event . data . details . error . failureType === "subtestsFailed"
65
- )
66
- ) {
60
+ if ( ! isSubtestFailedError ( event . data . details . error ) ) {
67
61
preFormattedFailureReasons . push (
68
62
formatFailureReason ( {
69
63
index : preFormattedFailureReasons . length ,
You can’t perform that action at this time.
0 commit comments