Giter Club home page Giter Club logo

Comments (6)

jandk008 avatar jandk008 commented on June 26, 2024 3

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

from ts-mockito.

prKassad avatar prKassad commented on June 26, 2024

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.

from ts-mockito.

prKassad avatar prKassad commented on June 26, 2024

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

from ts-mockito.

Lagyu avatar Lagyu commented on June 26, 2024

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

from ts-mockito.

wenPKtalk avatar wenPKtalk commented on June 26, 2024

Nice! This is right

from ts-mockito.

jakobsa avatar jakobsa commented on June 26, 2024

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

from ts-mockito.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.