jest-zod-matchers
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

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. If zodOutputObject is not provided, the parsed result should at least be valid. If zodOutputObject is 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. If expectedErrorMessage is not provided, the parsed result should at least throw any error. If expectedErrorMessage is provided, it will check if the first thrown error matches the value of expectedErrorMessage.

Installation

npm install -D jest-zod-matchers

License

MIT

Package Sidebar

Install

npm i jest-zod-matchers

Weekly Downloads

158

Version

0.0.7

License

MIT

Unpacked Size

29.6 kB

Total Files

30

Last publish

Collaborators

  • harm-less