ts-mockito icon indicating copy to clipboard operation
ts-mockito copied to clipboard

It should warn or throw when used without `instance`

Open k0pernikus opened this issue 5 years ago • 2 comments

ts-mockito requires the use of instance (see: #87) to get the actual mocked object instance instead of a helper object. This is fine.

I find its behavior strange though when used in a test-case when not used with instance. (I have asked and answered a question on StackOverflow about "How to mock a method using ts-mockito? " because of it.)

TypeScript will transpile it, my Intellij WebStorm IDE will show no error, it will even autocomplete functions on the non-instance "mock".

From my perspective, the mock generated by mock looks like the actual mock instance. So I was very confused and spent a few hours figuring out why I was not getting the actual mocked value but this ts-mockito "proxy" object.

I'd propose that ts-mockito should have proxy on every method and issue a "Did you forget to use instance?" hint if possible. It will not catch all cases but for the ones were the developer is calling methods on the proxy object, they would get a more straightforward hint and not a generic runtime TypeError: actualReturnValue.{whatEverWasCalled} is not a function.

k0pernikus avatar Nov 25 '19 14:11 k0pernikus

ts-mockito requires the use of instance (see: #87) to get the actual mocked object instance instead of a helper object. This is fine.

But it looks like other mocking frameworks don't need a separate call to make an instance? For example TypeMoq: https://github.com/florinn/typemoq#dynamic-mocks

pkrukp avatar Apr 23 '20 12:04 pkrukp

Same here. Got confused between Mock and instance(Mock) and spent great time finding the problem.

Mockito from Java is like @k0pernikus described, there are only instances. Shouldn't ts-mockito follow the same pattern?

awvalenti avatar May 22 '20 11:05 awvalenti