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

Partial types for mocked return values #1739

Closed
4 tasks done
jfrs opened this issue Jul 28, 2022 · 1 comment · Fixed by #1742
Closed
4 tasks done

Partial types for mocked return values #1739

jfrs opened this issue Jul 28, 2022 · 1 comment · Fixed by #1742
Labels
enhancement New feature or request pr welcome

Comments

@jfrs
Copy link
Contributor

jfrs commented Jul 28, 2022

Clear and concise description of the problem

As a developer using Vitest I want to be able to mock only certain properties or functions of a module so that I can keep the type checking from TypeScript but only mock what I need for the test.

Currently when using vi.mocked and mockReturnValue with TypeScript it will expect a fully compatible type, but most of the time since you're mocking you only need some of the properties to be set.

This is an issue I face a lot when mocking but I haven't found a proper solution, the most common solution to this seems to be casting to unknown and but then you lose the type checking.

Suggested solution

There's a solution suggested here but I think just changing the return type to be a Partial<T> would work?

(Copied suggestion just in case:)

type DeepPartial<T> = {
    [P in keyof T]?: T[P] extends Array<infer U>
        ? Array<DeepPartial<U>>
        : T[P] extends ReadonlyArray<infer U>
            ? ReadonlyArray<DeepPartial<U>>
            : DeepPartial<T[P]>
};

type PartialReturnType<T extends MockableFunction> = 
    ReturnType<T> extends Promise<infer U>
        ? Promise<DeepPartial<U>>
        : ReturnType<T> extends Array<infer P>
            ? Array<DeepPartial<P>>
            : DeepPartial<ReturnType<T>>;

interface MockWithArgs<T extends MockableFunction> extends jest.MockInstance<PartialReturnType<T>, ArgumentsOf<T>> {
    new (...args: ConstructorArgumentsOf<T>): T;
    (...args: ArgumentsOf<T>): PartialReturnType<T>;
}

Alternative

I use @ts-ignore so I can keep the types but disable the error.

There's also this related issue but it solves a different problem I think.

Additional context

My current use case for this is mocking Pinia stores:

vi.mocked(useAuthStore).mockReturnValue({
  loggedIn: true
});

This will produce an error because the return value is missing a lot of Store properties and functions.

Validations

@sheremet-va
Copy link
Member

I don't see us "just" changing type to partial, since someone might rely on it.

I am not against partiallyMocked utility function thought.

@sheremet-va sheremet-va added enhancement New feature or request pr welcome labels Jul 29, 2022
sheremet-va pushed a commit that referenced this issue Aug 2, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request pr welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants