Skip to content
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

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

Open
yuyasasaki-0 opened this issue Oct 9, 2020 · 6 comments
Open

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

yuyasasaki-0 opened this issue Oct 9, 2020 · 6 comments

Comments

@yuyasasaki-0
Copy link

yuyasasaki-0 commented Oct 9, 2020

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 yuyasasaki-0 changed the title (instance(mock(MyClass)) instanceof MyClass) returns false. (instance(mock(MyClass)) instanceof MyClass) is false. Oct 9, 2020
@prKassad
Copy link

prKassad commented Oct 16, 2020

I also encountered a problem checking instanceof in my code. This doesn't seem to be possible to check via the mock object.
See #133 (comment). If anyone has any ideas on how to test this without changing the existing code, please tell us.

@prKassad
Copy link

prKassad commented Oct 16, 2020

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);

@Lagyu
Copy link

Lagyu commented Oct 25, 2020

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

@jandk008
Copy link

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

@wenPKtalk
Copy link

Nice! This is right

@jakobsa
Copy link

jakobsa commented Feb 21, 2023

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants