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

TypeScript errors when mocking overloaded functions #6182

Closed
6 tasks done
Tobbe opened this issue Jul 21, 2024 · 0 comments · Fixed by #6181
Closed
6 tasks done

TypeScript errors when mocking overloaded functions #6182

Tobbe opened this issue Jul 21, 2024 · 0 comments · Fixed by #6181

Comments

@Tobbe
Copy link

Tobbe commented Jul 21, 2024

Describe the bug

I found #6085 and have a very similar issue.
So I thought it would have been solved by #6086. But unfortunately I'm still running into issues.

This actually looks to be a regression introduced with 2.0.3
Here it is without any TS errors with 2.0.2 https://stackblitz.com/edit/vitest-dev-vitest-vmu2hy?file=test%2Fbasic.test.ts
And here it is with 2.0.3: https://stackblitz.com/edit/vitest-dev-vitest-huxpgg?file=test%2Fbasic.test.ts

This is my code (also available at the above StackBlitz links)
(With inlined error messages)
image

And in text form, for easier Google indexing:

With string

vi.spyOn(fs, 'readFileSync').mockImplementation(() => {
  return 'string'
})

Error:

Argument of type '() => string' is not assignable to parameter of type '{ (path: PathOrFileDescriptor, options?: { encoding?: null | undefined; flag?: string | undefined; } | null | undefined): Buffer; (path: PathOrFileDescriptor, options: BufferEncoding | { ...; }): string; (path: PathOrFileDescriptor, options?: (ObjectEncodingOptions & { ...; }) | ... 2 more ... | undefined): string |...'.
  Type 'string' is not assignable to type 'Buffer'.

Trying Buffer instead, as that's what suggested by the error message

vi.spyOn(fs, 'readFileSync').mockImplementation(() => {
  return Buffer.from('string')
})

Error:

Argument of type '() => Buffer' is not assignable to parameter of type '{ (path: PathOrFileDescriptor, options?: { encoding?: null | undefined; flag?: string | undefined; } | null | undefined): Buffer; (path: PathOrFileDescriptor, options: BufferEncoding | { ...; }): string; (path: PathOrFileDescriptor, options?: (ObjectEncodingOptions & { ...; }) | ... 2 more ... | undefined): string |...'.
  Type 'Buffer' is not assignable to type 'string'.

Trying the combination of Buffer | string as that's what the actual implementation has

vi.spyOn(fs, 'readFileSync').mockImplementation(() => {
  return 'string' as Buffer | string
})

Error:

Argument of type '() => Buffer | string' is not assignable to parameter of type '{ (path: PathOrFileDescriptor, options?: { encoding?: null | undefined; flag?: string | undefined; } | null | undefined): Buffer; (path: PathOrFileDescriptor, options: BufferEncoding | { ...; }): string; (path: PathOrFileDescriptor, options?: (ObjectEncodingOptions & { ...; }) | ... 2 more ... | undefined): string |...'.
  Type 'string | Buffer' is not assignable to type 'Buffer'.
    Type 'string' is not assignable to type 'Buffer'.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-huxpgg?file=test%2Fbasic.test.ts

System Info

System:
    OS: macOS 14.1.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 53.80 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.1 - /usr/local/bin/node
    Yarn: 4.1.1 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    bun: 1.1.6 - /opt/homebrew/bin/bun
  Browsers:
    Safari: 17.1

Used Package Manager

yarn

Validations

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant