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

Type error on mockImplementation with call signature in object type #6155

Closed
6 tasks done
freakzlike opened this issue Jul 17, 2024 · 3 comments · Fixed by #6181
Closed
6 tasks done

Type error on mockImplementation with call signature in object type #6155

freakzlike opened this issue Jul 17, 2024 · 3 comments · Fixed by #6181
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@freakzlike
Copy link

freakzlike commented Jul 17, 2024

Describe the bug

I have a function type with additional properties:

export interface Handler {
  (v: number): number;
  other(v: number): number;
}

When I try to use mockImplementation on the call signature, I receive the following type error

mockedHandler.mockImplementation((v) => v * 1);
// Property 'other' is missing in type '(v: number) => number' but required in type 'Handler'

There was no error in v1. Now with v2, I have to pass the whole object even though other is not used and not working.
I expect that only the call signature type is required for mockImplementation

As additional help I found a helper to extract the call signature from an object type:

type CallSignature<T extends (...a: any[]) => unknown> = (...a: Parameters<T>) => ReturnType<T>

const onlyCall: CallSignature<Handler> = (v) => v * 2

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-8ikkec?file=test%2Fbasic.test.ts&view=editor

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 2.0.3 
    vite: latest => 5.3.4 
    vitest: latest => 2.0.3

Used Package Manager

pnpm

Validations

@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jul 17, 2024
@sheremet-va sheremet-va moved this to P2 - 4 in Team Board Jul 18, 2024
@is-jonreeves
Copy link

Incase it helps narrow things down, we are also getting a bunch of similar typescript errors after upgrading from 2.0.2 to 2.0.3.

@hi-ogawa
Copy link
Contributor

I think OP's vi.mocked + mockImplementation was already broken in v2.0.0.

@is-jonreeves If you are seeing a issue specific to 2.0.2 -> 2.0.3 (thus due to #6086), then it would be great if you can provide a reproduction.

@is-jonreeves
Copy link

I tested 2.0.4 and it appears to have also resolved my issue. Thanks!


For what it's worth... I re-tried downgrading to 2.0.3 to make sure I wasn't mistaken, and there is definitely a type error over there.

Downgrading to 2.0.3:

Simplest example I could find in our code:

const mockedSelectIsUnlicensed = vi.spyOn(sessionSlice, 'selectIsUnlicensed');
...
mockedSelectIsUnlicensed.mockImplementation(() => true); // <--- complains in 2.0.3 even though type is correct

Type error in 2.0.3 (not in 2.0.2 or 2.0.4):

Type '() => true' is not assignable to type '{ clearCache: () => void; resultsCount: () => number; resetResultsCount: () => void; }'

I don't have a full example I can share, but if it helps, the usecase is a real redux store where we have used vi.spyOn to override an exported selector.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants