-
Notifications
You must be signed in to change notification settings - Fork 4
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
Allow use with mocks created by spyOn
#15
Comments
Hey @RylanBueckert-Broadsign, thanks for this request!
Nice catch! This should be a fairly easy update to the typings to support this. I should be able to address it shortly.
I did a little digging, and Jest and Vitest have some important differences that make this feature difficult to implement in const spy = vi.spyOn(foo, 'bar');
const impl = spy.getMockImplementation(); In To be honest, though, even if there wasn't a technical blocker, I'm not super jazzed about supporting In my opinion, mocks should be used to help you design APIs that are easy to use so you can write code that's easy to test. Using |
Understand that calling the original non-mocked version may not be simple, but what about falling back to the existing mock implementation? For example: let barSpy: Mocked<typeof foo.bar>;
beforeEach(() => {
barSpy = vi.spyOn(foo, 'bar');
barSpy.mockReturnValue('default mocked return value');
});
test('Example', () => {
when(barSpy).calledWith('baz').thenReturn('mocked return value');
expect(foo.bar('buzz')).toBe('default mocked return value');
}); This also currently returns Sometimes |
I'm amendable to this change! Feels pretty consistent with Vitest in general and provides an interesting place to provide a default fallback without increasing the API surface area of the
My take here is a little spicy depending on one's experience with using mocks, but can be summed up as "you shouldn't mock that out (directly)." I assume that sounds super unhelpful! So, if you're curious, a little essay/rant: Most mock usage in the wild is to fake out something that's hard or inconvenient to test - like knocking out Outside of that specific use-case in a TDD flow, I don't think one should really use mocks, because they cost so much in terms of coupling your test cases to your code under test. They also don't really test that your code works in the real world - because you've got a bunch of take things in play. If you're not getting useful structural design feedback of your internals, I haven't found the cost of mocks to be worth it. This idea can roughly be encapsulated in the advice "Don't mock what you don't own". If you're designing some internal logic that needs
For more on this style of mocking, I think this talk is a fantastic introduction, and the concepts it touches on really leveled me up in terms of how I approach testing my software and teach my teams how to test! |
I do get your point. In this case The fact that I do appreciate your explanation, and I will watch that talk. Thanks for maintaining this library. |
In
jest-when
it is possible to use when mocks on spied functions. It would be very nice if we could do the same here.Simple Example to demonstrate what should be possible
There is an type error when using
when
in this example:Argument of type MockInstance<(arg1: string) => string> is not assignable to parameter of type AnyFunction Type MockInstance<(arg1: string) => string> provides no match for the signature (...args: never[]): unknown
If I simply ignore the error and run the test, the when mock does actually work when calling with the specified parameters, but it returns
undefined
instead of the original behavior when the params do not match.The text was updated successfully, but these errors were encountered: