Skip to content

Commit d224363

Browse files
authored
test: add result verification to test cases (#5473)
Enhance test cases by adding proper assertions to verify parsed results instead of just calling parse without validation: - nan.test.ts: Verify parsed NaN values using Number.isNaN() - complex.test.ts: Add comprehensive assertions to verify all parsed fields match the input structure - function.test.ts: Verify function parsing returns correct values and verify params/returnType getter parse results - util.ts: Improve error message in assertNever for better debugging
1 parent a43295d commit d224363

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

packages/zod/src/v3/tests/complex.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from "vitest";
1+
import { expect, test } from "vitest";
22
import * as z from "zod/v3";
33

44
const crazySchema = z.object({
@@ -40,7 +40,7 @@ const crazySchema = z.object({
4040
// });
4141

4242
test("parse", () => {
43-
crazySchema.parse({
43+
const input = {
4444
tuple: ["asdf", 1234, true, null, undefined, "1234"],
4545
merged: { k1: "asdf", k2: 12 },
4646
union: ["asdf", 12, "asdf", 12, "asdf", 12],
@@ -52,5 +52,19 @@ test("parse", () => {
5252
nonstrict: { points: 1234 },
5353
numProm: Promise.resolve(12),
5454
lenfun: (x: string) => x.length,
55-
});
55+
};
56+
57+
const result = crazySchema.parse(input);
58+
59+
// Verify the parsed result structure
60+
expect(result.tuple).toEqual(input.tuple);
61+
expect(result.merged).toEqual(input.merged);
62+
expect(result.union).toEqual(input.union);
63+
expect(result.array).toEqual(input.array);
64+
expect(result.sumMinLength).toEqual(input.sumMinLength);
65+
expect(result.intersection).toEqual(input.intersection);
66+
expect(result.enum).toEqual(input.enum);
67+
expect(result.nonstrict).toEqual(input.nonstrict);
68+
expect(result.numProm).toBeInstanceOf(Promise);
69+
expect(typeof result.lenfun).toBe("function");
5670
});

packages/zod/src/v3/tests/function.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const func1 = z.function(args1, returns1);
1010

1111
test("function parsing", () => {
1212
const parsed = func1.parse((arg: any) => arg.length);
13-
parsed("asdf");
13+
const result = parsed("asdf");
14+
expect(result).toBe(4);
1415
});
1516

1617
test("parsed function fail 1", () => {
@@ -226,8 +227,11 @@ test("allow extra parameters", () => {
226227
test("params and returnType getters", () => {
227228
const func = z.function().args(z.string()).returns(z.string());
228229

229-
func.parameters().items[0].parse("asdf");
230-
func.returnType().parse("asdf");
230+
const paramResult = func.parameters().items[0].parse("asdf");
231+
expect(paramResult).toBe("asdf");
232+
233+
const returnResult = func.returnType().parse("asdf");
234+
expect(returnResult).toBe("asdf");
231235
});
232236

233237
test("inference with transforms", () => {

packages/zod/src/v3/tests/nan.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import * as z from "zod/v3";
66
const schema = z.nan();
77

88
test("passing validations", () => {
9-
schema.parse(Number.NaN);
10-
schema.parse(Number("Not a number"));
9+
const result1 = schema.parse(Number.NaN);
10+
expect(Number.isNaN(result1)).toBe(true);
11+
12+
const result2 = schema.parse(Number("Not a number"));
13+
expect(Number.isNaN(result2)).toBe(true);
1114
});
1215

1316
test("failing validations", () => {

packages/zod/src/v4/core/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export function assertNotEqual<A, B>(val: AssertNotEqual<A, B>): AssertNotEqual<
199199
export function assertIs<T>(_arg: T): void {}
200200

201201
export function assertNever(_x: never): never {
202-
throw new Error();
202+
throw new Error("Unexpected value in exhaustive check");
203203
}
204204
export function assert<T>(_: any): asserts _ is T {}
205205

0 commit comments

Comments
 (0)