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

(instance(mock(MyClass)) instanceof MyClass) is false.

Open yuyasasaki-0 opened this issue 3 years ago • 6 comments

The mock instance from ts-mockito does not return true with instanceof.

const mockedInstanceOfMyClass = instance(mock(MyClass))
console.log(mockedInstanceOfMyClass  instanceof MyClass) // false, should be true

Thus, when I test my code like following with the mock instance (Proxy), I cannot test as I intended.

if (myArg instanceof MyClass) {
  foo()
} else {
  ...

I can manually set the prototype of parent class to my mock instance with Object.setPrototypeOf() and pass the test of code with instanceof without modification, but it's natural that the mock instance of a class is the instance of the class by default.

yuyasasaki-0 avatar Oct 09 '20 07:10 yuyasasaki-0

I also encountered a problem checking instanceof in my code. This doesn't seem to be possible to check via the mock object. See https://github.com/NagRock/ts-mockito/issues/133#issuecomment-463142976. If anyone has any ideas on how to test this without changing the existing code, please tell us.

prKassad avatar Oct 16 '20 18:10 prKassad

I'am hacked it's via Symbol.hasInstance and duck typing in my test:

Object.defineProperty(MyClass, Symbol.hasInstance, {
  value: (obj) => obj.hasOwnProperty("myProperty"),
});
const mockedMyClass = mock(MyClass);
when(mockedMyClass.myProperty).thenReturn(true);

prKassad avatar Oct 16 '20 18:10 prKassad

If anyone has any ideas on how to test this without changing the existing code, please tell us.

@prKassad As mentioned in the first comment, Object.setPrototypeOf lets you test code with instanceof without change of existing code.

const mockedInstanceOfMyClass = instance(mock(MyClass))
console.log(mockedInstanceOfMyClass instanceof MyClass) // false
Object.setPrototypeOf(mockedInstanceOfMyClass, MyClass.prototype)
console.log(mockedInstanceOfMyClass instanceof MyClass) // true

Lagyu avatar Oct 25 '20 00:10 Lagyu

This one should take more attention for getting ts-mockito more intuitive and convenient

jandk008 avatar Feb 16 '21 11:02 jandk008

Nice! This is right

wenPKtalk avatar Jan 17 '23 05:01 wenPKtalk

how about:

import {instance as wrappedInstance} from 'ts-mockito';

const instance = function(mockClass) {
    const mockedInstanceOfMyClass = wrappedInstance(mockClass)
    Object.setPrototypeOf(mockedInstanceOfMyClass, mockClass.__tsmockitoMocker.clazz.prototype);
    return mockedInstanceOfMyClass;
}

jakobsa avatar Feb 21 '23 14:02 jakobsa