Skip to content

Commit

Permalink
wip: explore custom error formatting in node test reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Aug 28, 2024
1 parent b8d9b45 commit e94c4bf
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion config-v-next/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"declarationMap": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"isolatedDeclarations": true,
"isolatedDeclarations": false,
"noEmitOnError": true,
"noImplicitOverride": true,
"skipDefaultLibCheck": true,
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions v-next/example-project/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ const config: HardhatUserConfig = {
],
plugins: [
pluginExample,
HardhatMochaTestRunner,
// HardhatMochaTestRunner,
// if testing node plugin, use the following line instead
// HardhatNodeTestRunner,
HardhatNodeTestRunner,
],
privateKey: configVariable("privateKey"),
paths: {
tests: "test/mocha",
// tests: "test/mocha",
// if testing node plugin, use the following line instead
// tests: "test/node",
tests: "test/node",
},
};

Expand Down
1 change: 1 addition & 0 deletions v-next/example-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.3",
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-node-test-runner": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
"@types/mocha": ">=9.1.0",
"@types/node": "^20.14.9",
"mocha": "^10.0.0",
Expand Down
6 changes: 3 additions & 3 deletions v-next/example-project/test/node/example-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from "node:assert/strict";
import { HardhatPluginError } from "@ignored/hardhat-vnext-errors";
import { describe, it } from "node:test";

describe("Example test", () => {
it("should work", () => {
assert.equal(1 + 1, 2);
it("should not work", () => {
throw new HardhatPluginError("HHE1", "Hello!");
});
});
6 changes: 6 additions & 0 deletions v-next/hardhat-errors/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ErrorDescriptor } from "./descriptors.js";

import { inspect } from "node:util";

import { CustomError } from "@ignored/hardhat-vnext-utils/error";
import { isObject } from "@ignored/hardhat-vnext-utils/lang";

Expand Down Expand Up @@ -211,6 +213,10 @@ export class HardhatPluginError extends CustomError {

return isHardhatPluginErrorProperty?.value === true;
}

public [inspect.custom]() {
return `This is a title of a HardhatPluginError!\n${this.message}\nThis is the stack of a HardhatPluginError!`
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions v-next/hardhat-node-test-reporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,13 @@
"@actions/core": "^1.10.1",
"chalk": "^5.3.0",
"jest-diff": "^29.7.0"
},
"peerDependencies": {
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2"
},
"peerDependenciesMeta": {
"@ignored/hardhat-vnext-errors": {
"optional": true
}
}
}
13 changes: 12 additions & 1 deletion v-next/hardhat-node-test-reporter/src/error-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "./node-test-error-utils.js";

// TODO: Clean up the node internal fames from the stack trace
export function formatError(error: Error): string {
export async function formatError(error: Error): Promise<string> {
if (isCancelledByParentError(error)) {
return (
chalk.red("Test cancelled by parent error") +
Expand All @@ -37,6 +37,17 @@ export function formatError(error: Error): string {

error = cleanupTestFailError(error);

if (error.name === "HardhatPluginError") {
try {
const { HardhatPluginError } = await import("@ignored/hardhat-vnext-errors");
if (HardhatPluginError.isHardhatPluginError(error)) {
error = new HardhatPluginError(error.pluginId as string, error.message, error.cause as Error | undefined);
}
} catch (e) {
console.debug(e);
}
}

const defaultFormat = inspect(error);
const indexOfMessage = defaultFormat.indexOf(error.message);

Expand Down
4 changes: 2 additions & 2 deletions v-next/hardhat-node-test-reporter/src/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export function formatTestFailure(failure: Failure): string {
);
}

export function formatFailureReason(failure: Failure): string {
export async function formatFailureReason(failure: Failure): Promise<string> {
return `${formatTestContext(
failure.contextStack,
`${formatFailureIndex(failure.index)}) `,
":",
)}
${indent(formatError(failure.testFail.details.error), 3)}`;
${indent(await formatError(failure.testFail.details.error), 3)}`;
}

export function formatSlowTestInfo(durationMs: number): string {
Expand Down
2 changes: 1 addition & 1 deletion v-next/hardhat-node-test-reporter/src/github-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function annotatePR(

const { default: core } = await import("@actions/core");

core.error(formatError(error), {
core.error(await formatError(error), {
file: location.file,
startLine: location.line,
startColumn: location.column,
Expand Down
4 changes: 2 additions & 2 deletions v-next/hardhat-node-test-reporter/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default async function* customReporter(
await annotatePR(event.data);

preFormattedFailureReasons.push(
formatFailureReason({
await formatFailureReason({
index: preFormattedFailureReasons.length,
testFail: event.data,
contextStack: stack,
Expand Down Expand Up @@ -165,7 +165,7 @@ export default async function* customReporter(

// We format the failure reason and store it in an array, so that we
// can output it at the end.
preFormattedFailureReasons.push(formatFailureReason(failure));
preFormattedFailureReasons.push(await formatFailureReason(failure));

await annotatePR(event.data);

Expand Down
1 change: 0 additions & 1 deletion v-next/hardhat-node-test-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
},
"dependencies": {
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
"tsx": "^4.11.0"
Expand Down

0 comments on commit e94c4bf

Please sign in to comment.