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

Mock static methods #61

Open
ingoe opened this issue Oct 25, 2017 · 8 comments
Open

Mock static methods #61

ingoe opened this issue Oct 25, 2017 · 8 comments

Comments

@ingoe
Copy link

ingoe commented Oct 25, 2017

Hi folks,

are there any plans to support mocking of static methods?

Cheers
Ingo

@kmannislands
Copy link

Mocking of static methods would be even nicer but the current state is that if the class has any static methods, they cannot be called on the mock version (rendering this package unusable in my case)

ie:

class Foo {
  static sayHello() {
    console.log('hello')
  }
}

const mockFoo = mock(Foo);
mockFoo.sayHello() // error TS2339: Property 'sayHello' does not exist on type 'Foo'.

@IonelLupu
Copy link

Any updates on this?

@MatejSkrbis
Copy link

MatejSkrbis commented Sep 14, 2020

You can try something like this:

class Foo {
    static sayHello(): void {
        console.log('hello')
    }
}

const mockFoo = mock<typeof Foo>(Foo);
mockFoo.sayHello(); // no more error

This is how I get around this problem.

@SatoshiKawabata
Copy link

SatoshiKawabata commented Dec 7, 2020

@MatejSkrbis
I tried your code and use when.

const mockFoo = mock<typeof Foo>(Foo);
mockFoo.sayHello(); // no more error
when(mockFoo.sayHello());

But I got this error. Could you run the test without error?

TypeError: mockFoo.sayHello is not a function

@MatejSkrbis
Copy link

MatejSkrbis commented Dec 8, 2020

@SatoshiKawabata

@MatejSkrbis
I tried your code and use when.

const mockFoo = mock<typeof Foo>(Foo);
mockFoo.sayHello(); // no more error
when(mockFoo.sayHello());

But I got this error. Could you run the test without error?

TypeError: mockFoo.sayHello is not a function

I've tried it and there is no error on my side.
I'm using latest packages of mocha and ts-mockito ("mocha": "8.2.1", "ts-mockito": "2.6.1", "typescript": "4.0.5")

@leegee
Copy link

leegee commented Apr 7, 2022

The above example works for me, but I can't get it working IRL, where the static method to be mocked is in a dependency of the object being tested: could the documentation be updated to include the correct way to handle such a scenario?

@MatejSkrbis
Copy link

MatejSkrbis commented Apr 7, 2022

@leegee This library is no longer maintained. I like it but there are no updates from the author and he didn't give access to anyone else to update the documentation or the code. I don't know what is the correct way, but you could use spy instead of mock.

class Foo {
    static sayHello(): string {
        return 'Nop';
    }
}

const spyFoo = spy(Foo);

when(spyFoo.sayHello()).thenReturn('Yes');

console.log(Foo.sayHello()); // Will print out Yes

@leegee
Copy link

leegee commented Apr 11, 2022

That was kind of you, thank you.

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