Skip to content

Commit

Permalink
docs: add use cases and license
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed Apr 12, 2023
1 parent 9fb1684 commit e37851c
Showing 1 changed file with 70 additions and 7 deletions.
77 changes: 70 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![NPM version](https://img.shields.io/npm/v/zodock?color=2f68b7&label=)](https://www.npmjs.com/package/zodock)

The mocking library for TypeScript-first schema validation [Zod](https://zod.dev/), creates a mock object based on the schema. It makes it easy to create mock data for testing purposes.
The mocking library for TypeScript-first schema validation [Zod](https://zod.dev/), 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

Expand All @@ -11,17 +11,64 @@ npm install -D zodock
```

## Usage

```ts
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

```ts
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

```ts
import { createMock } from 'zodock';

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

createMock(schema); // string
const mockSchema = createMock(schema);

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

or
or you can imitate axios response

```ts
import { createMock } from 'zodock';

Expand All @@ -30,10 +77,26 @@ const schema = z.object({
age: z.number(),
});

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

## TODO

- ZodNever
- ZodLazy
- ZodLazy

## License

[MIT](./LICENSE) License © 2023 [Magomed Chemurziev](https://github.com/ItMaga)

0 comments on commit e37851c

Please sign in to comment.