-
Notifications
You must be signed in to change notification settings - Fork 93
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
Comments
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'. |
Any updates on this? |
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. |
@MatejSkrbis 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?
|
I've tried it and there is no error on my side. |
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? |
@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 |
That was kind of you, thank you. |
Hi folks,
are there any plans to support mocking of static methods?
Cheers
Ingo
The text was updated successfully, but these errors were encountered: