timkindberg / jest-when

Jest support for mock argument-matched return values.
MIT License
737 stars 39 forks source link

"Overloaded" default mock #103

Closed anton-johansson closed 1 year ago

anton-johansson commented 1 year ago

Hey! Just found this library, very nice utility.

I'd love a way to set up a "default" mock using jest-when. For example:

when(getUser).calledWith(expect.anything()).mockResolvedValue(null);
when(getUser).calledWith(1).mockResolvedValue(userFixture);

But this does not seem to work. The expect.anything() does not seem to work. It works if I only have it, but not in combination with the "real" mock.

As a workaround, I have to specify the "incorrect" user ID that I use in my test, but I would love to not have to do that:

when(getUser).calledWith(666).mockResolvedValue(null);
when(getUser).calledWith(1).mockResolvedValue(userFixture);
timkindberg commented 1 year ago

Does this help?

https://github.com/timkindberg/jest-when#supports-default-behavior

anton-johansson commented 1 year ago

Wow, I'm blind as a bat. Sorry, and thanks for the very quick response, @timkindberg!