Document the fact that arguments to mocked functions are not cloned #5443
Replies: 1 comment 6 replies
-
I totally get that this kind of mutability issue appears very confusing and hard to track down, but I'm not actually sure if cloning arguments is "correct" either in this context. Not only about what/how to clone and its perf impact, but also, for example, I feel it's also natural for users to wish to assert some arguments of calls are identically equal: const fn = vi.fn();
const something = {};
fn(something);
fn.mock.calls[0][0] === something; // it would be false if deep cloned So, this looks to me as a more general pitfalls of writing mutable code and not necessary about mocking or Vitest. Actually you didn't provide any code, so I'm just guessing your situation. If I misunderstood the issue, I would appreciate if you can provide more concrete code to illustrate your concern. Also do you know what other mocking library such as sinon or jest does? If there is a difference, then that can be also a good guidance about what to do. |
Beta Was this translation helpful? Give feedback.
-
This caught me out and took me a while to debug. Because tinyspy doesn't clone the arguments when tracking calls (https://github.com/tinylibs/tinyspy/blob/cd4d86683a2a62f34d9df90308039d1faa88f9f7/src/internal.ts#L83), you can get quite unexpected results if you are calling a mocked function multiple times while mutating the object it is being called with. The
.mock.calls
property will only show the final value of the object passed to the mock function because the logged calls in tinyspy get mutated out from under it.I'd suggest documenting here: https://vitest.dev/guide/mocking.html#mocking-pitfalls
And an example code could be included showing how you could create your own local list of calls in a mockImplementation if you needed to be able to handle this situation.
Beta Was this translation helpful? Give feedback.
All reactions