Zocker: Zod Schema Test Data Generator
Zocker is a library designed to generate realistic, type-safe test data directly from your Zod schemas. It provides a fluent API for defining how mock data should be created, allowing for customization of various Zod types and the ability to override generation rules globally or for specific schema paths. The current stable version is 3.0.0, which solidified its intuitive fluent API after a significant breaking change in v1.0.0. While there isn't a strict release cadence, minor and patch versions are released as new Zod features are supported or bug fixes are implemented. Zocker differentiates itself by deeply integrating with Zod, leveraging the schema definition itself to ensure generated data adheres to the schema's constraints and types, making it ideal for robust testing and prototyping.
Common errors
-
TypeError: (0 , zocker__WEBPACK_IMPORTED_MODULE_1__.zocker) is not a function
cause Attempting to use `zocker` via CommonJS `require()` or as a default import in an ESM context.fixEnsure you are using named imports for `zocker`: `import { zocker } from 'zocker';`. If in a CommonJS environment, consider configuring your project for ESM or use a transpiler. -
TS2307: Cannot find module 'zocker' or its corresponding type declarations.
cause The 'zocker' package is not installed, or TypeScript cannot locate its type definitions. This can happen with incorrect import paths or mismatched TypeScript configurations.fixVerify that `zocker` is installed (`npm install zocker` or `yarn add zocker`). Check your `tsconfig.json` for correct `moduleResolution` (e.g., `bundler` or `node16`) and `module` settings (e.g., `esnext`). Ensure correct import path `from 'zocker'`. -
Error: Zod schema not found (zocker requires a Zod schema to generate data)
cause This error likely indicates that the `zod` peer dependency is missing or its version is incompatible with `zocker`.fixInstall `zod` as a dependency: `npm install zod` or `yarn add zod`. Ensure the installed `zod` version matches `zocker`'s peer dependency requirement (e.g., `^3.25.0 || ^4.0.0`).
Warnings
- breaking Version 1.0.0 introduced a complete overhaul of the configuration API, replacing the old Generator-based system with a new fluent API. This change breaks all existing setups, including those without custom generators.
- gotcha When using `supply` or `override` methods, incorrect type definitions were present in versions prior to 1.2.1, leading to potential TypeScript errors or unexpected behavior when providing custom generation logic.
- gotcha Support for Zod's `readonly` schemas was introduced in v1.4.0. Using `zocker` with `readonly` schemas in earlier versions might lead to unexpected behavior or incomplete data generation.
Install
-
npm install zocker -
yarn add zocker -
pnpm add zocker
Imports
- z
import { z } from 'zod'; - zocker
import zocker from 'zocker'; const zocker = require('zocker');import { zocker } from 'zocker'; - ZockerOptions
import { ZockerOptions } from 'zocker';import type { ZockerOptions } from 'zocker';
Quickstart
import { z } from 'zod';
import { zocker } from 'zocker';
// Define a Zod schema for a User
const UserSchema = z.object({
id: z.string().uuid(),
name: z.string().min(3).max(50),
email: z.string().email(),
age: z.number().int().min(18).max(99),
isActive: z.boolean(),
roles: z.array(z.enum(['admin', 'editor', 'viewer'])).min(1),
createdAt: z.date().default(() => new Date()),
});
// Create a zocker instance for the User schema
const generateUser = zocker(UserSchema);
// Generate a single user object
const user = generateUser();
console.log('Generated User:', user);
// Generate multiple user objects (e.g., 5 users)
const users = zocker(UserSchema, { seed: 123 }).generateMany(5);
console.log('Generated Users (many):', users);