Skip to content

Commit 3f7322f

Browse files
committed
test: add comprehensive tests for zod schema wrapper
Add 16 tests covering all major zod schema types: - Basic types (string, number, boolean, array, object) - Advanced features (optional, default, unions, enums, literals) - Refinements and transformations - Nested objects and safeParse Coverage improved: zod.ts from 0% to 100% All tests pass (101 test files, 4679 tests)
1 parent 64e896b commit 3f7322f

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

test/zod.test.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* @fileoverview Unit tests for zod schema validation library wrapper.
3+
*/
4+
5+
import { describe, expect, it } from 'vitest'
6+
7+
import { z } from '@socketsecurity/lib/zod'
8+
9+
describe('zod', () => {
10+
describe('z export', () => {
11+
it('should export z object', () => {
12+
expect(z).toBeDefined()
13+
expect(typeof z).toBe('object')
14+
})
15+
16+
it('should export string schema builder', () => {
17+
expect(typeof z.string).toBe('function')
18+
const schema = z.string()
19+
expect(schema.parse('test')).toBe('test')
20+
})
21+
22+
it('should export number schema builder', () => {
23+
expect(typeof z.number).toBe('function')
24+
const schema = z.number()
25+
expect(schema.parse(123)).toBe(123)
26+
})
27+
28+
it('should export boolean schema builder', () => {
29+
expect(typeof z.boolean).toBe('function')
30+
const schema = z.boolean()
31+
expect(schema.parse(true)).toBe(true)
32+
})
33+
34+
it('should export object schema builder', () => {
35+
expect(typeof z.object).toBe('function')
36+
const schema = z.object({
37+
name: z.string(),
38+
age: z.number(),
39+
})
40+
expect(schema.parse({ name: 'test', age: 25 })).toEqual({
41+
name: 'test',
42+
age: 25,
43+
})
44+
})
45+
46+
it('should export array schema builder', () => {
47+
expect(typeof z.array).toBe('function')
48+
const schema = z.array(z.string())
49+
expect(schema.parse(['a', 'b', 'c'])).toEqual(['a', 'b', 'c'])
50+
})
51+
52+
it('should validate and throw on invalid data', () => {
53+
const schema = z.string()
54+
expect(() => schema.parse(123)).toThrow()
55+
})
56+
57+
it('should support optional fields', () => {
58+
const schema = z
59+
.object({
60+
name: z.string(),
61+
age: z.number().optional(),
62+
})
63+
expect(schema.parse({ name: 'test' })).toEqual({ name: 'test' })
64+
expect(schema.parse({ name: 'test', age: 25 })).toEqual({
65+
name: 'test',
66+
age: 25,
67+
})
68+
})
69+
70+
it('should support default values', () => {
71+
const schema = z.object({
72+
name: z.string(),
73+
age: z.number().default(0),
74+
})
75+
expect(schema.parse({ name: 'test' })).toEqual({ name: 'test', age: 0 })
76+
})
77+
78+
it('should support unions', () => {
79+
const schema = z.union([z.string(), z.number()])
80+
expect(schema.parse('test')).toBe('test')
81+
expect(schema.parse(123)).toBe(123)
82+
expect(() => schema.parse(true)).toThrow()
83+
})
84+
85+
it('should support enums', () => {
86+
const schema = z.enum(['red', 'green', 'blue'])
87+
expect(schema.parse('red')).toBe('red')
88+
expect(() => schema.parse('yellow')).toThrow()
89+
})
90+
91+
it('should support literal values', () => {
92+
const schema = z.literal('hello')
93+
expect(schema.parse('hello')).toBe('hello')
94+
expect(() => schema.parse('world')).toThrow()
95+
})
96+
97+
it('should support refinements', () => {
98+
const schema = z.string().refine((val) => val.length > 3, {
99+
message: 'String must be longer than 3 characters',
100+
})
101+
expect(schema.parse('test')).toBe('test')
102+
expect(() => schema.parse('ab')).toThrow()
103+
})
104+
105+
it('should support transformations', () => {
106+
const schema = z.string().transform((val) => val.toUpperCase())
107+
expect(schema.parse('test')).toBe('TEST')
108+
})
109+
110+
it('should support nested objects', () => {
111+
const schema = z.object({
112+
user: z.object({
113+
name: z.string(),
114+
email: z.string().email(),
115+
}),
116+
})
117+
expect(
118+
schema.parse({ user: { name: 'test', email: 'test@example.com' } }),
119+
).toEqual({ user: { name: 'test', email: 'test@example.com' } })
120+
})
121+
122+
it('should support safeParse for non-throwing validation', () => {
123+
const schema = z.string()
124+
const result1 = schema.safeParse('test')
125+
expect(result1.success).toBe(true)
126+
if (result1.success) {
127+
expect(result1.data).toBe('test')
128+
}
129+
130+
const result2 = schema.safeParse(123)
131+
expect(result2.success).toBe(false)
132+
})
133+
})
134+
})

0 commit comments

Comments
 (0)