1+ import ansis from 'ansis' ;
2+ import { z } from 'zod' ;
3+ import { SchemaValidationError } from '@code-pushup/models' ;
14import { stringifyError } from './errors.js' ;
25
36describe ( 'stringifyError' , ( ) => {
@@ -22,4 +25,52 @@ describe('stringifyError', () => {
2225 '{"status":400,"statusText":"Bad Request"}' ,
2326 ) ;
2427 } ) ;
28+
29+ it ( 'should prettify ZodError instances spanning multiple lines' , ( ) => {
30+ const schema = z . object ( {
31+ name : z . string ( ) . min ( 1 ) ,
32+ address : z . string ( ) ,
33+ dateOfBirth : z . iso . date ( ) . optional ( ) ,
34+ } ) ;
35+ const { error } = schema . safeParse ( { name : '' , dateOfBirth : '' } ) ;
36+
37+ expect ( stringifyError ( error ) ) . toBe ( `ZodError:
38+ ✖ Too small: expected string to have >=1 characters
39+ → at name
40+ ✖ Invalid input: expected string, received undefined
41+ → at address
42+ ✖ Invalid ISO date
43+ → at dateOfBirth
44+ ` ) ;
45+ } ) ;
46+
47+ it ( 'should prettify ZodError instances on one line if possible' , ( ) => {
48+ const schema = z . enum ( [ 'json' , 'md' ] ) ;
49+ const { error } = schema . safeParse ( 'html' ) ;
50+
51+ expect ( stringifyError ( error ) ) . toBe (
52+ 'ZodError: ✖ Invalid option: expected one of "json"|"md"' ,
53+ ) ;
54+ } ) ;
55+
56+ it ( 'should use custom SchemaValidationError formatted messages' , ( ) => {
57+ const schema = z
58+ . object ( {
59+ name : z . string ( ) . min ( 1 ) ,
60+ address : z . string ( ) ,
61+ dateOfBirth : z . iso . date ( ) . optional ( ) ,
62+ } )
63+ . meta ( { title : 'User' } ) ;
64+ const { error } = schema . safeParse ( { name : '' , dateOfBirth : '' } ) ;
65+
66+ expect ( stringifyError ( new SchemaValidationError ( error ! , schema , { } ) ) )
67+ . toBe ( `SchemaValidationError: Invalid ${ ansis . bold ( 'User' ) }
68+ ✖ Too small: expected string to have >=1 characters
69+ → at name
70+ ✖ Invalid input: expected string, received undefined
71+ → at address
72+ ✖ Invalid ISO date
73+ → at dateOfBirth
74+ ` ) ;
75+ } ) ;
2576} ) ;
0 commit comments