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.
npm install -D zodock
import { createMock } from 'zodock';
const schema = z.object({
name: z.string(),
age: z.number(),
});
createMock(schema); // { name: string, age: number }
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);
});
});
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: {},
}),
);
}
};
- ZodNever
MIT License © 2023 Magomed Chemurziev