From 308a8707e20890701c3c2b5d7e6ebaba1e57c921 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Fri, 20 Sep 2024 10:34:21 +0200 Subject: [PATCH] test: improve vitest logging Ref: #7 --- tests/setup.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/setup.ts b/tests/setup.ts index 3b44f14..2b943bc 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -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", @@ -24,3 +27,43 @@ dotenv.config({ path: ".env.test.local", override: true, }); + +function isFrameworkErrorLike(error: unknown): error is Record { + const keys = ["errors", "context", "isRetryable", "isFatal"] as (keyof FrameworkError)[]; + return hasProps(keys)(error as Record); +} + +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; + }, +});