florinn / typemoq

A simple mocking library for TypeScript
MIT License
427 stars 43 forks source link

Setup/callback arguments gets cloned #98

Open kenjiqq opened 6 years ago

kenjiqq commented 6 years ago

Sometimes when mocking a class you might be in a situation where you want to save the arguments passed into the mocked function so you can use them late.

But it seems like arguments that gets passed the the callback or returns function during a setup are cloned and this causes problems if you need access to the same instance of the argument that was passed into the mock.

For my case i had a function that takes a RXJS subject and i tried to save this subject in the mock so i can publish data on it later in the test. But because the subject is a clone anything that is subscribed to the original subject will not get any of this data.

I made a small reproducible case here https://codesandbox.io/s/kowqrq8nwv

taylorn16 commented 5 years ago

I am having the exact same issue, also using an RxJS subject. Any estimate on the potential difficulty of making this change? I wouldn't mind helping out with a PR.

For additional context, the line that clones the arguments is right here: https://github.com/florinn/typemoq/blob/0721689785fe533e68fe455280288980020c7eac/src/Proxy/Invocation.ts#L43

It doesn't seem too impossible to be able to pass in an argument when creating the mock to bypass this line.

nickform-ost commented 5 years ago

I'm also affected by this issue. In my case, I'm trying to ensure that the component under test cancels any pending request to an API before it makes another API call. The component under test creates the cancellation token to pass into the API (which I am mocking) but when I capture the token via a callback() setup, I get a copy of it instead of the original object. Would be interested in a fix but also to know why the arguments are cloned in the first place (hope that doesn't make it sound like I'm saying that they ought not to be!).

Btw, thanks for the free code. I used Moq a lot in C# so having this library helped me a ton.