Skip to content

Infer actual types for mock properties passed to fromPartial #12

@acelaya

Description

@acelaya

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, vi is globally exposed by vitest, but the expectation would be that jest.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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions