Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Mocking #197

Closed
colinhacks opened this issue Oct 19, 2020 · 4 comments
Closed

RFC: Mocking #197

colinhacks opened this issue Oct 19, 2020 · 4 comments
Labels
wontfix This will not be worked on

Comments

@colinhacks
Copy link
Owner

colinhacks commented Oct 19, 2020

I'm open to a PR for a .mock method that lets you generate mock data for your schemas.

Proposal

New base method: .mock

A .mock method on the base class.

z.string().mock(); // => "AkvoFxGfoHFhdfKF" (random string)
z.number().mock(); // => 18916.19016 (random number)

Overrides

This method could accept some parameters that let you overriding the default mocking functions for each data type (e.g. strings, numbers, tuples, etc).

z.string().mock({
  stringMocker: ()=>['hello', 'goodbye'][Math.round(Math.random())]
});
// => randomly chooses "hello" or "goodbye"

Instance-specific mocking functions

There would also need to be a way to override the mocking function for a particular schema instance (probably a .mocker() or .setMocker() method on the base class).

z.string()
  .refine(val => val === val.toLowerCase())
  .mocker(()=>{
    return randomString().toLowerCase();
  })
  .mock(); // => "akvofxgfohfhdfkf"

Issues

Refinements

Zod has no way of generating a value that satisfies a given refinement. There would also need to be a way to override the mocking function for a particular schema instance (probably a .mocker() or .setMocker() method on the base class).

Unions

Which union "option" should be used to generate the value? Perhaps a randomly chosen one? Should the mocked value for an optional value be undefined 50% of the time?

Transformers

Should Zod mock a value for the input or the output type? Not a huge issue, this can just be an option/flag in the .mock method.

@lo1tuma
Copy link
Contributor

lo1tuma commented Oct 20, 2020

My thoughts on this topic:

  • Since zod is already quite big in terms of bundle-size without support for tree-shaking, I think this should be a separate package or at least a separate file that is not loaded when doing import * as z from 'zod'.
  • I prefer static data over random dynamic data in order to implement reproducible tests. My go-to library here is cooky-cutter which allows you to define factories where you can partially override the default values. This makes it easy to write small test that focus only on some specific properties of an object. Maybe it would be possible to automatically create such factories from a zod schema.

@colinhacks
Copy link
Owner Author

I'm inclined to disagree. This would add maybe a kilobyte to the bundle size max. Plus I consider mocking is a pretty fundamental bit of functionality for a library that is trying to be the single-source of truth for type information.

Pretty sure we could eliminate more than a kilobyte from the bundle by using the removeComments option in tsc...just realized I haven't enabled that 😬

@o-alexandrov
Copy link
Contributor

Based on the same building/bundling-related reasons from #264 (comment)
Please kindly consider to add this feature as a separate utility function

@colinhacks colinhacks mentioned this issue Mar 26, 2021
@stale
Copy link

stale bot commented Mar 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Mar 2, 2022
@stale stale bot closed this as completed Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants