From f63bb9adb75e3aaecf4827c23008ee07d2a6578c Mon Sep 17 00:00:00 2001 From: Misha Kaletsky Date: Tue, 27 Aug 2024 08:58:01 -0400 Subject: [PATCH] mv assertion to existing test --- src/__tests__/unions.test.ts | 42 +++++++++++++++--------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/__tests__/unions.test.ts b/src/__tests__/unions.test.ts index 8713293ff..e3a7820cc 100644 --- a/src/__tests__/unions.test.ts +++ b/src/__tests__/unions.test.ts @@ -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: [ + { + code: "invalid_type", + expected: "number", + received: "string", + path: [], + message: "Expected number, received string", + }, + { + code: "custom", + message: "Invalid input", + path: [], + }, + ], }, ]); } @@ -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)], - }, - ], - }, - }); -});