Skip to content

Commit

Permalink
test: improve error serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Sep 20, 2024
1 parent a1aefed commit f903957
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/tools/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ describe("Base Tool", () => {
const tool = new ComplexTool();

await expect(tool.run("fail" as any)).rejects.toThrowErrorMatchingInlineSnapshot(`
[ToolInputValidationError: The received tool input does not match the expected schema.
ToolInputValidationError: The received tool input does not match the expected schema.
Input Schema: "{"type":"object","properties":{"foo":{"type":"string"},"bar":{"type":"string"}},"required":["foo","bar"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}"
Validation Errors: [{"instancePath":"","schemaPath":"#/type","keyword":"type","params":{"type":"object"},"message":"must be object"}]]
Validation Errors: [{"instancePath":"","schemaPath":"#/type","keyword":"type","params":{"type":"object"},"message":"must be object"}]
`);
});
});
Expand Down
14 changes: 13 additions & 1 deletion tests/e2e/tools/arxiv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ describe("Arxiv", () => {
instance.run({
ids: ["xx"],
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`[ToolError: Request to ArXiv API has failed!]`);
).rejects.toThrowErrorMatchingInlineSnapshot(`
ToolError: Request to ArXiv API has failed!
Error: {
"id": "http://arxiv.org/api/errors#incorrect_id_format_for_xx",
"title": "Error",
"summary": "incorrect id format for xx",
"updated": "2024-09-20T00:00:00-04:00",
"link": "",
"author": {
"name": "arXiv api core"
}
}
`);
});
});
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 util from "util";
import { FrameworkError } from "@/errors.js";
import { hasProps } from "@/internals/helpers/object.js";
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 f903957

Please sign in to comment.