Skip to content

Commit

Permalink
mv assertion to existing test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Aug 27, 2024
1 parent 83828f3 commit 3b3af19
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions src/__tests__/unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,29 @@ test("return valid over invalid", () => {
});
});

test("return dirty result over aborted", () => {
test("union with dirty result", () => {
const result = z
.union([z.number(), z.string().refine(() => false)])
.safeParse("a");
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues).toEqual([
expect(result.error.issues).toMatchObject([
{
code: "custom",
message: "Invalid input",
path: [],
code: "invalid_union",
unionErrors: [
expect.objectContaining({
code: "invalid_type",
expected: "number",
received: "string",
path: [],
message: "Expected number, received string",
}),
expect.objectContaining({
code: "custom",
message: "Invalid input",
path: [],
}),
],
},
]);
}
Expand All @@ -62,23 +74,3 @@ test("readonly union", async () => {
union.parse("asdf");
union.parse(12);
});

test("union of strict objects", async () => {
// ensure bug in 3.23.8 is fixed, where "dirty" parse failures would mean reporting the first error, rather than a proper `invalid_union` with `unionErrors`.
// Demo of bug: https://stackblitz.com/edit/stackblitz-starters-swdj3e?file=index.test.js&startScript=test
const union = z.union([
z.object({ x: z.number() }).strict(), //
z.object({ y: z.number() }).strict(),
]);
expect(union.safeParse({ x: 1, y: 1 })).toMatchObject({
success: false,
error: {
issues: [
{
code: "invalid_union",
unionErrors: [expect.any(z.ZodError), expect.any(z.ZodError)],
},
],
},
});
});

0 comments on commit 3b3af19

Please sign in to comment.