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

Mocking implementation with mockImplementation allows incorrect return types #3750

Closed
6 tasks done
Namchee opened this issue Jul 9, 2023 · 1 comment · Fixed by #4859
Closed
6 tasks done

Mocking implementation with mockImplementation allows incorrect return types #3750

Namchee opened this issue Jul 9, 2023 · 1 comment · Fixed by #4859

Comments

@Namchee
Copy link
Contributor

Namchee commented Jul 9, 2023

Describe the bug

Function mocking through .mockImplementation(Once) allows an incorrect Promise-wrapped type to be returned. For example, a function that returns number can be mocked to return Promise<number> as opposed to number. This is possible because .mockImplementation(Once) accepts an union-type of number | Promise<number> as the return type for the implementation.

Jest, a test runner with similar API doesn't allow Promise<number> to be returned by the mocked implementation and only allows number.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-bayh5s?file=index.test.ts

System Info

System:
  OS: Linux 5.19 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
  CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
  Memory: 9.74 GB / 15.26 GB
  Container: Yes
  Shell: 5.8.1 - /usr/bin/zsh
Binaries:
  Node: 18.16.1
  npm: 9.5.1
  pnpm: 8.6.1
npmPackages:
  vite: ^4.3.9 => 4.3.9 
  vitest: ^0.32.4 => 0.32.4

Used Package Manager

pnpm

Validations

@stackblitz
Copy link

stackblitz bot commented Jul 9, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants