Skip to main content

Zod Factory Methods

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 Factory States section below.


Zod Factory Methods

.mock()

Tells the Factory to generate your mocked schema.

Once you have defined your factory, you can create a mocked model by simply calling the mock() method on your instantiated factory.

Let's take a look at using the mock method to generate a model without any specific states applied.

.mock()
import TransformedCarFactory from '@schwab/schema/factories/transformed/TransformedCarSchema';

const data = new TransformedCarFactory().mock();

.mockMany()

Tells the Factory to generate multiple mocks of a schema.

Mock multiple schema Models with Zod Factories using the mockMany() method.

The mockMany method received the a argument reflecting the number of models you would like mocked. In the example below, we are mocking 3 transformed cars.

.mockMany()
import TransformedCarFactory from '@schwab/schema/factories/transformed/TransformedCarSchema';

const data = new TransformedCarFactory().mockMany(3);

.getLocales()

Retrieves an array of Factory supported languages.

Need to test that your assertions are true even when localized characters are received? This method can help.

In this example, we are setting up a test suite that will iterate through each supported locale for testing.

.getLocales()
import TransformedCarFactory from '@schwab/schema/factories/transformed/TransformedCarSchema';

describe.each(new TransformedCarFactory().getLocales())('My TestSuite Description', (locale: string): void => {
// ... tests
});

This couples nicely with the chainable .locale() method enabling us to dynamically pass in current locale.

.locale()
import TransformedCarFactory from '@schwab/schema/factories/transformed/TransformedCarSchema';

describe.each(new TransformedCarFactory().getLocales())('My TestSuite Description', (locale: string): void => {
test(`${locale} input is received`, () => {
const input = new TransformedCarFactory().locale(locale).mock();
// ...
});
});

.locale()

Tells the Factory to generate localized schema mocks.

Mock schema in a specific supported locale by chaining the .locale() method to an instantiated factory.

.locale()
import TransformedCarFactory from '@schwab/schema/factories/transformed/TransformedCarSchema';

const english = new TransformedCarFactory().locale('en_US').mock();
const chinese = new TransformedCarFactory().locale('zh_CN').mock();

.console()

Console log a mocked model from anywhere within the chain.

At times, we will want to take a peek at the data generated by a factory.

.console()
const data = new TransformedCarFactory().sedan().autobot().mock();
console.log(data);

However, taking this approach can be cumbersome and only consoles the end result. To keep things simple, use the chainable .console() method instead.

.console()
const data = new TransformedCarFactory().sedan().autobot().console().mock();
// Result: console.warn
// Factory Mocked Schema Preview: { ... }

The factory will return the mocked model as expected. The only difference being that we will also see the generated model consoled for review.

Note: This method must be called before .mock() or .mockMany() has been called.

Consoling multiple states

Comparing 2 or more mocked states is as easy as chaining multiple .console() methods.

Only the states that appear prior to a .console() call will be applied.

This allows us to chain multiple .console()'s throughout our chain to compare results before and after states are applied.

.console()
const data = new TransformedCarFactory()
.sedan()
.console()
.autobot()
.console()
.blue()
.console()
.mock();

Any states that appear after the .console() method will not be applied to the logged model.

Note: The .console() method is intended for debugging only and should be removed once you're finished viewing your model.

.nullable()

Generates null values for all .nullable() schema properties.

This chainable method tells the Factory to generate a NULL value for all schema properties marked .nullable().

Example Schema:

Example Schema
export const ExampleSchema = z.object({
name: z.string(),
age: z.number().nullable(),
});

When our example schema above is mocked, the generated model will resemble something like this:

.mock()
const data = new TransformedCarFactory().mock();
// Result
{
name: 'Megatron',
age: 438
}

When .nullable() is chained, the factory will generate:

.nullable().mock()
const data = new TransformedCarFactory().nullable().mock();
// Result
{
name: 'Megatron',
age: null
}

.nullish()

Generates undefined values for properties that are marked .optional() or .nullish() and NULL values for all properties marked .nullable().

Example Schema:

Example Schema
export const ExampleSchema = z.object({
name: z.string(),
age: z.number().optional(),
color: z.string().nullable(),
length: z.number().nullish(),
});

When our example schema above is mocked, the generated model will resemble something like this:

.mock()
const data = new TransformedCarFactory().mock();
// Result
{
name: 'Megatron',
age: 438,
color: "chrome",
length: "14'"
}

When .nullish() is chained, the factory will generate:

.nullish().mock()
const data = new TransformedCarFactory().nullish().mock();
// Result
{
name: 'Megatron',
age: undefined,
color: null,
length: undefined,
}

.optional()

Generates undefined values for all .optional() schema properties.

This chainable method tells the Factory to generate undefined values for all schema properties marked .optional().

Example Schema:

Example Schema
export const ExampleSchema = z.object({
name: z.string(),
age: z.number().optional(),
});

When our example schema above is mocked, the generated model will resemble something like this:

.mock()
const data = new TransformedCarFactory().mock();
// Result
{
name: 'Megatron',
age: 438
}

When .optional() is chained, the factory will generate:

.optional().mock()
const data = new TransformedCarFactory().optional().mock();
// Result
{
name: 'Megatron',
age: undefined
}