unit-test

Generate Vitest tests for Zod schemas using safeParse() and strict validation.

1|Updated Apr 17, 2025
One-click install
npx skills add https://github.com/carrot-foundation/schemas --skill unit-test-carrot-foundation
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-test
Source: https://github.com/carrot-foundation/schemas/tree/main/.cursor/skills/unit-test
Command: npx skills add https://github.com/carrot-foundation/schemas --skill unit-test-carrot-foundation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This guide provides a standardized approach for writing and maintaining Vitest tests for Zod schemas in the schemas package, aimed at achieving 100% test coverage across all four thresholds.

Core Features & Use Cases

  • Tests live alongside source files in tests/ directories, mirroring the source structure.
  • Naming convention is {type}.schema.spec.ts to match the main schema file.
  • Test structure covers valid inputs, invalid inputs, optional fields, and edge cases; tests should use safeParse() and avoid parse().
  • Guidance includes handling unknown properties to enforce strict object schemas and testing enumerations across types.

Quick Start

Create a Vitest test file for each schema type that mirrors the source and uses safeParse for validation.

Frequently Asked Questions about unit-test

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write Vitest tests for Zod schemas to achieve 100% coverage?

To write Vitest tests for Zod schemas with 100% coverage, create test files in __tests__/ directories mirroring the source structure, use safeParse() for validation, and cover valid inputs, invalid inputs, optional fields, and edge cases.

What is the standard naming convention for Zod schema test files in Vitest?

The standard naming convention for Zod schema test files is {type}.schema.spec.ts, placed inside a __tests__/ directory that mirrors the source file structure to maintain organization and clarity.

Should I use parse() or safeParse() when testing Zod schemas?

You should use safeParse() instead of parse() when testing Zod schemas. safeParse() returns a structured success object without throwing exceptions, allowing you to explicitly assert invalid inputs and edge cases gracefully.

How do I enforce strict Zod object schemas in my Vitest tests?

To enforce strict Zod object schemas in Vitest tests, include test scenarios that pass unknown properties and assert the schema rejects them. This ensures unexpected fields are not silently ignored during validation.

Can I use Vitest to test all Zod schema types including Assets, Receipts, and Audit?

Yes, you can use Vitest to test all Zod schema types including Assets, Receipts, Audit, and Reference. The testing scope covers all these schema types to ensure comprehensive validation across the schemas package.

What edge cases should I cover when testing Zod schema validations?

When testing Zod schema validations, cover edge cases including invalid inputs, missing optional fields, and unknown properties. Additionally, test enumerations across all types to ensure strict schema enforcement and robust validation.