jest-zod-matchers
v0.0.7
Published
Custom Jest matchers to help validate Zod schemas and to prevent having a lot of boilerplate
Maintainers
Readme
jest-zod-matchers
Custom Jest matchers to help validate Zod schemas and to prevent having a lot of boilerplate
Basic Usage | API | Installation | License
Basic Usage
First, add the custom matchers to Jest. A convenient way to do this is via a setup file included in setupFilesAfterEnv:
import {getJestZodMatchers} from 'jest-zod-matchers';
expect.extend(getJestZodMatchers());Now you can use the custom matchers in your tests:
expect(safeParsedZodObjectSchema).toBeValidZodObject({foo: 'bar'});
expect(safeParsedZodSchema).toThrowZodError('Zod error');API
toBeValidZodObject(zodOutputObject?: Record<string, unknown>)
Validates a safe parsed Zod schema and checks whether or not it's the expected successful result.
Arguments
zodOutputObject- Optional An object expecting key/value pairs. IfzodOutputObjectis not provided, the parsed result should at least be valid. IfzodOutputObjectis provided, it will check if all the given keys exist and if their values match the parsed result.
Examples
expect(safeParsedZodObjectSchema).toBeValidZodObject(); // expects a valid parse result
expect(safeParsedZodObjectSchema).toBeValidZodObject({foo: 'bar'}); // expects "foo" to be available in the parsed result and its value be a string of "bar"toThrowZodError(expectedErrorMessage?: string)
Expects a safe parsed Zod schema to throw at least one error. If at least one is thrown, the first error will be tested.
Arguments
expectedErrorMessage- Optional An error string. IfexpectedErrorMessageis not provided, the parsed result should at least throw any error. IfexpectedErrorMessageis provided, it will check if the first thrown error matches the value ofexpectedErrorMessage.
Installation
npm install -D jest-zod-matchersLicense
MIT
