Skip to content

Commit 154d4be

Browse files
committed
Add a module with utilities to work with node:test generated errors
1 parent 2b8a092 commit 154d4be

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

v-next/hardhat-node-test-reporter/src/error-formatting.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ import { inspect } from "node:util";
44
import chalk from "chalk";
55
import { diff } from "jest-diff";
66

7+
import {
8+
cleanupTestFailError,
9+
isCancelledByParentError,
10+
} from "./node-test-error-utils.js";
11+
712
// TODO: Clean up the node internal fames from the stack trace
813
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+
);
2322
}
2423

24+
error = cleanupTestFailError(error);
25+
2526
const defaultFormat = inspect(error);
2627
const indexOfMessage = defaultFormat.indexOf(error.message);
2728

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

v-next/hardhat-node-test-reporter/src/reporter.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
formatSlowTestInfo,
1212
Failure,
1313
} from "./formatting.js";
14+
import { isSubtestFailedError } from "./node-test-error-utils.js";
1415
import {
1516
TestEventData,
1617
TestEventSource,
@@ -56,14 +57,7 @@ export default async function* customReporter(
5657
// If a suite failed for a reason other than a subtest failing, we
5758
// want to print its failure, so we push it to the failures array.
5859
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)) {
6761
preFormattedFailureReasons.push(
6862
formatFailureReason({
6963
index: preFormattedFailureReasons.length,

0 commit comments

Comments
 (0)