UNPKG

dockest

Version:

Dockest is an integration testing tool aimed at alleviating the process of evaluating unit tests whilst running multi-container Docker applications.

88 lines 3.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const zod_1 = require("zod"); const format_zod_error_1 = require("./format-zod-error"); describe('formatZodError', () => { const Person = zod_1.z.object({ name: zod_1.z.string(), age: zod_1.z.number(), nested: zod_1.z.object({ field: zod_1.z.string(), }), }); const Options = zod_1.z.union([zod_1.z.literal('option-1'), zod_1.z.literal('option-2')]); it('should format a ZodError for a union', () => { const invalidOptions = Options.safeParse('not-an-option'); if (invalidOptions.success) { fail("Shouldn't reach this point"); } const result = (0, format_zod_error_1.formatZodError)(invalidOptions.error, 'Options'); expect(result).toMatchInlineSnapshot(` "[ZodValidationError for "Options"] Invalid literal value, expected "option-1" Invalid literal value, expected "option-2"" `); }); it('should format a ZodError with an identity', () => { const invalidPerson = Person.safeParse({ name: 1 }); if (invalidPerson.success) { fail("Shouldn't reach this point"); } const result = (0, format_zod_error_1.formatZodError)(invalidPerson.error, 'Person'); expect(result).toMatchInlineSnapshot(` "[ZodValidationError for "Person"] Expected string, received number at "name" Required at "age" Required at "nested"" `); }); it('should format a ZodError without an identity', () => { const invalidPerson = Person.safeParse({ name: 1 }); if (invalidPerson.success) { fail("Shouldn't reach this point"); } const result = (0, format_zod_error_1.formatZodError)(invalidPerson.error, 'Person'); expect(result).toMatchInlineSnapshot(` "[ZodValidationError for "Person"] Expected string, received number at "name" Required at "age" Required at "nested"" `); }); it('should format a ZodError with several errors', () => { const Person = zod_1.z.object({ name: zod_1.z.string().min(5), age: zod_1.z.number().min(33), }); const invalidPerson = Person.safeParse({ name: 'Rob', age: 32, }); if (invalidPerson.success) { fail("Shouldn't reach this point"); } const result = (0, format_zod_error_1.formatZodError)(invalidPerson.error, 'Person'); expect(result).toMatchInlineSnapshot(` "[ZodValidationError for "Person"] String must contain at least 5 character(s) at "name" Number must be greater than or equal to 33 at "age"" `); }); it('should truncate if more than 100 issues', () => { const Person = zod_1.z.array(zod_1.z.object({ name: zod_1.z.string().min(5), age: zod_1.z.number().min(33), })); const invalidPerson = Person.safeParse(Array.from({ length: 300 }).map(() => ({ name: 'Rob', age: 32, }))); if (invalidPerson.success) { fail("Shouldn't reach this point"); } const result = (0, format_zod_error_1.formatZodError)(invalidPerson.error, 'Person'); expect(result.split('\n').length).toMatchInlineSnapshot(`101`); expect(result).toMatchSnapshot(); }); }); //# sourceMappingURL=format-zod-error.test.js.map