Skip to content
/ zodock Public

A library for generating mock data based on Zod schemas

License

Notifications You must be signed in to change notification settings

ItMaga/zodock

Repository files navigation

Zodock

npm npm bundle size NPM npm

The mocking library for TypeScript-first schema validation Zod, creates a mock object based on the schema. It makes it easy to create mock data for testing purposes, to create mock data for the API request or response, and etc.

Installation

npm install -D zodock

Usage

import { createMock } from 'zodock';

const schema = z.object({
  name: z.string(),
  age: z.number(),
});

createMock(schema); // { name: string, age: number }

Use cases

Mocking data for testing

import { createMock } from 'zodock';

const schema = z.object({
  name: z.string(),
  age: z.number(),
});

describe('test', () => {
  const mockSchema = createMock(schema);

  const apiMock = jest.fn().mockResolvedValue(mockSchema);

  it('should return mock data', async () => {
    const result = await apiMock();

    expect(result).toEqual(mockSchema);
  });
});

Mocking data for API request

import { createMock } from 'zodock';

const schema = z.object({
  name: z.string(),
  age: z.number(),
});

const mockSchema = createMock(schema);

export const api = {
  get: () => {
    return mockSchema;
  }
};

or you can imitate axios response

import { createMock } from 'zodock';

const schema = z.object({
  name: z.string(),
  age: z.number(),
});

export const api = {
  get: () => {
    return new Promise(resolve =>
      resolve({
        status: 200,
        statusText: 'OK',
        headers: {},
        data: createMock(schema),
        config: {},
      }),
    );
  }
};

TODO

  • ZodNever

License

MIT License © 2023 Magomed Chemurziev