Skip to content

spyOn unable to infer type when using Partial TypeScript utility type #7674

Open
@Owlbertz

Description

@Owlbertz

Describe the bug

I have a mock for an instance of a class that is basically an object type as a Partial<> from the original class.
When using spyOn on a method of this instance, TypeScript cannot infer the return type and shows Property 'mockReturnValue' does not exist on type 'never'.
When I do not use Partial<> but just declare the object it works fine.
However, I want the type safety when declaring the mock, therefore I am using Partial<>. I am just switching from Jest where this exact setup works fine.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-6wdpqq8p?file=test%2Fpartial.test.ts

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (14) x64 Intel(R) Core(TM) Ultra 7 155U
    Memory: 6.34 GB / 23.23 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v22.9.0/bin/yarn
    npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm
  npmPackages:
    vitest: ^3.0.8 => 3.0.8

Used Package Manager

yarn

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions