You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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
andmockReturnValue
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:)
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:
This will produce an error because the return value is missing a lot of Store properties and functions.
Validations
The text was updated successfully, but these errors were encountered: