Skip to main content

Zod Factories

Create Mocked Data For Testing and Development With Zod Factories!

The following will outline how to start generating mocked data models from an existing Zod Schema via Factories.

Step 1: Establish A Factory Name

To name your Factory, simply take the name of your Schema and replace the word "Schema" with "Factory".

For example, if your Zod Schema is named "TransformedCarSchema.ts" your Factory name will be "TransformedCarFactory.ts"

Step 2: Create a Factory

Below is an example of a basic Factory. To make it your own:

  1. Copy this code snippet to a new file.
  2. Update the imports with your desired Zod Schema and inferred type.
TransformedCarFactory.ts
import Factory from '../../../../test/src/zest/Factory';
import { TransformedCarSchema, TTransformedCar } from '@schwab/schema/transformed/TransformedCarSchema';

export default class TransformedCarFactory extends Factory<TTransformedCar> {
constructor() {
super(TransformedCarSchema);
}
}

Step 3: Save Your Factory

Save your Factory file to the /packages/schema/src/factories/ directory and then mirror the subsequent path of the Zod Schema it mocks.

For example: Notice the TransformedCarSchema from above is located at:

/packages/schema/src/transformed/TransformedCarSchema.ts

So in this case, the Zod Schema Factory will be saved to:

packages/schema/src/factories/transformed/TransformedCarFactory.ts

Factory Methods

Start mocking model with .mock(), .mockMany(), and more!

Once you have defined your factory, you can start creating and debugging mocked models by leveraging chainable factory utility methods.

See all available methods and examples below.

Control How Data Is Mocked

Need more control over how your schema is mocked?

Learn how to add "Factory States" to quickly customize your mocked data in the next section.