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 20d913d
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 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([
{
code: "custom",
message: "Invalid input",
path: [],
code: "invalid_union",
unionErrors: [
{
code: "invalid_type",
expected: "number",
received: "string",
path: [],
message: "Expected number, received string",
},
{
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 20d913d

Please sign in to comment.