Skip to content

Commit

Permalink
Merge pull request #8 from i-am-bee/feat/7-vitest-logging
Browse files Browse the repository at this point in the history
test: improve vitest logging
  • Loading branch information
grahamwhiteuk authored Sep 20, 2024
2 parents 4447c22 + 308a870 commit c5e4a3d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/

import dotenv from "dotenv";
import { FrameworkError } from "bee-agent-framework/errors";
import * as util from "node:util";
import { hasProps } from "bee-agent-framework/internals/helpers/object";
dotenv.config();
dotenv.config({
path: ".env.test",
Expand All @@ -24,3 +27,43 @@ dotenv.config({
path: ".env.test.local",
override: true,
});

function isFrameworkErrorLike(error: unknown): error is Record<keyof FrameworkError, any> {
const keys = ["errors", "context", "isRetryable", "isFatal"] as (keyof FrameworkError)[];
return hasProps(keys)(error as Record<keyof FrameworkError, any>);
}

afterEach(() => {
onTestFailed((testCase) => {
const errors = testCase.errors ?? [];
for (const error of errors) {
if (isFrameworkErrorLike(error)) {
error.message = util
.inspect(
{
message: error.message,
context: error.context,
cause: error.cause,
isFatal: error.isFatal,
isRetryable: error.isRetryable,
errors: error.errors,
},
{
compact: false,
depth: Infinity,
},
)
.replaceAll("[Object: null prototype]", "");
}
}
});
});

expect.addSnapshotSerializer({
serialize(val: FrameworkError): string {
return val.explain();
},
test(val): boolean {
return val && val instanceof FrameworkError;
},
});

0 comments on commit c5e4a3d

Please sign in to comment.