question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using .mock() as a .default() only in testing?

See original GitHub issue

Hey hey,

So we are looking at using a “zod first approach” to developing libraries (DDD?).

As such we have just been looking at a way to quickly mock inputs during develop and testing. One idea we had was to use .default() to produce the test data - but then we realised this didn’t make sense as we would then have these defaults in production as well.

It would be great if there were an equivalent .mock() that would return a mock value if no other is received and node env == dev or test (or some other config).

cc @iainkirkpatrick

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Oct 20, 2020

The ability to passing a “dynamic default” function into .default is now available in zod@2.0.0-beta.16 👍

const randomDefault = z
    .number()
    .default(() => Math.random())

// returns a new random number every time `parse` is called
randomDefault.parse(undefined);

Zod makes sure the returned value matches the expected type signature:

const randomDefault = z
    .number()
    .default(() => 'qwerty') // TypeError
1reaction
colinhackscommented, Oct 19, 2020

I just opened an RFC for mocking here: https://github.com/vriad/zod/issues/197

Feel free to comment 🤙

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test default argument called by a mocked method?
Just call it and use : self.assertEqual(sendContent(), "SOME_PATH"). Share.
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
By default, all methods of a mock return “uninitialized” or “empty” values, e.g., zeros for numeric types (both primitive and boxed), false for...
Read more >
unittest.mock — mock object library — Python 3.11.1 ...
If the function returns DEFAULT then the mock will return its normal value (from the return_value ).
Read more >
Clean Unit Tests with Mockito - Reflectoring
In this article we will learn how to mock objects with Mockito. We'll first talk about what test doubles are and then how...
Read more >
Mock Functions - Jest
You can optionally provide a name for your mock functions, which will be displayed instead of 'jest.fn()' in the test error output. Use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found