-
Notifications
You must be signed in to change notification settings - Fork 10
Open
Description
I'm not sure if this is even possible. I have tried a couple approaches in userland code, without successful results.
Basically, I would like to be able to do something like this:
class MyService {
public doSomething(foo: string) {}
}
class OtherService {
constructor(myService: MyService) {}
}
// ...
const myServiceMock = fromPartial<MyService>({ doSomething: vi.fn() });
const otherServiceInstance = new OtherService(myServiceMock);
myServiceMock.doSomething.mockReturnedValue(...);In this example,
viis globally exposed by vitest, but the expectation would be thatjest.fn()and similars also work.
At the moment this is not possible, because myServiceMock is inferred strictly as MyService, instead of, let's say MyService & { doSomething: Mock<...> }.
What I'm doing at the moment to work around this is:
const doSomething = vi.fn();
const myServiceMock = fromPartial<MyService>({ doSomething });
doSomething.mockReturnValue(...);But with this you loose a bit of context of what doSomething is, if you don't go back to the declaration and see it is being passed to the mock.
Metadata
Metadata
Assignees
Labels
No labels