-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitest.setup.ts
26 lines (24 loc) · 948 Bytes
/
vitest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { expect } from "vitest";
import type { NewPlugin } from "@vitest/pretty-format";
import { z } from "zod";
import { InputValidationError, OutputValidationError } from "./src";
/** Takes cause and certain props of custom errors into account */
const errorSerializer: NewPlugin = {
test: (subject) => subject instanceof Error,
serialize: (error: Error, config, indentation, depth, refs, printer) => {
const { name, message, cause } = error;
const { originalError } =
error instanceof InputValidationError ||
error instanceof OutputValidationError
? error
: {};
const { issues } = error instanceof z.ZodError ? error : {};
const obj = Object.assign(
issues ? { issues } : { message },
cause && { cause },
originalError && { originalError },
);
return `${name}(${printer(obj, config, indentation, depth, refs)})`;
},
};
expect.addSnapshotSerializer(errorSerializer);