Skip to content

Mocking implementation with mockImplementation allows incorrect return types #3750

@Namchee

Description

@Namchee

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions