Skip to content

Commit 79c2c99

Browse files
committed
count test failures and output
1 parent 6ef6bf3 commit 79c2c99

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

v-next/hardhat-node-test-runner/src/task-action.ts

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { HardhatConfig } from "@ignored/hardhat-vnext/types/config";
22
import type { NewTaskActionFunction } from "@ignored/hardhat-vnext/types/tasks";
33

4+
import { finished } from "node:stream/promises";
45
import { run } from "node:test";
56
import { fileURLToPath } from "node:url";
67

@@ -20,6 +21,15 @@ function isJavascriptFile(path: string): boolean {
2021
return /\.(js|cjs|mjs)$/i.test(path);
2122
}
2223

24+
function isSubtestFailedError(error: Error): boolean {
25+
return (
26+
"code" in error &&
27+
"failureType" in error &&
28+
error.code === "ERR_TEST_FAILURE" &&
29+
error.failureType === "subtestsFailed"
30+
);
31+
}
32+
2333
async function getTestFiles(
2434
testFiles: string[],
2535
config: HardhatConfig,
@@ -34,6 +44,9 @@ async function getTestFiles(
3444
);
3545
}
3646

47+
/**
48+
* Note that we are testing this manually for now as you can't run a node:test within a node:test
49+
*/
3750
const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
3851
{ testFiles, only },
3952
hre,
@@ -43,7 +56,37 @@ const testWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
4356
const tsx = fileURLToPath(import.meta.resolve("tsx/esm"));
4457
process.env.NODE_OPTIONS = `--import ${tsx}`;
4558

46-
run({ files, only }).compose(hardhatTestReporter).pipe(process.stdout);
59+
async function runTests(): Promise<number> {
60+
let failures = 0;
61+
const reporterStream = run({ files, only })
62+
.on("test:fail", (event) => {
63+
if (event.details.type === "suite") {
64+
// If a suite failed only because a subtest failed, we don't want to
65+
// count it as a failure since the subtest failure will be reported as well
66+
if (isSubtestFailedError(event.details.error)) {
67+
return;
68+
}
69+
}
70+
71+
failures++;
72+
})
73+
.compose(hardhatTestReporter);
74+
75+
reporterStream.pipe(process.stdout);
76+
77+
await finished(reporterStream);
78+
79+
return failures;
80+
}
81+
82+
const testFailures = await runTests();
83+
console.log("Failures: ", testFailures);
84+
85+
if (testFailures > 0) {
86+
process.exitCode = testFailures;
87+
}
88+
89+
return testFailures;
4790
};
4891

4992
export default testWithHardhat;

0 commit comments

Comments
 (0)