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

[Bug]: Type inferrence in toBeCalledWith broken for method with overloads #13337

Closed
avaly opened this issue Sep 29, 2022 · 3 comments · Fixed by #13339
Closed

[Bug]: Type inferrence in toBeCalledWith broken for method with overloads #13337

avaly opened this issue Sep 29, 2022 · 3 comments · Fixed by #13339

Comments

@avaly
Copy link
Contributor

avaly commented Sep 29, 2022

Version

29.1.1

Steps to reproduce

import { expect, test } from '@jest/globals';

function sampleWithOverloads(first: string, second: number): void;
function sampleWithOverloads(first: string, second: number, third: Function): void;
function sampleWithOverloads(first: string, second: number, third?: Function) {
  // no op
}

function sampleWithoutOverloads(first: string, second: number) {
  // no op
}

test('toBeCalledWith without overloads', () => {
  expect(sampleWithoutOverloads).toHaveBeenCalledWith('foo', 123);
  // ^ This is OK
});

test('toBeCalledWith with overloads', () => {
  expect(sampleWithOverloads).toHaveBeenCalledWith('foo', 123);
  // ^ This is erroring: Expected 3 arguments, but got 2. ts(2554)
});

Expected behavior

The methods with overloads should be inferred correctly based on the number of arguments.

Actual behavior

Methods with overloads are triggering type errors.

Additional context

The bug was introduced in #13268

This is a common pattern in libraries which support both callback and promise syntax, e.g. https://github.com/mongodb/node-mongodb-native/blob/main/src/collection.ts#L265-L285

Environment

System:
  OS: Linux 5.13 Ubuntu 20.04.5 LTS (Focal Fossa)
  CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics
Binaries:
  Node: 18.0.0 - ~/.volta/tools/image/node/18.0.0/bin/node
  Yarn: 1.22.11 - ~/.volta/tools/image/yarn/1.22.11/bin/yarn
  npm: 8.1.4 - ~/.volta/tools/image/npm/8.1.4/bin/npm
npmPackages:
  jest: 29.1.1 => 29.1.1
@avaly avaly changed the title [Bug]: Type inferrence in toBeCalledWith broken for method with overrides [Bug]: Type inferrence in toBeCalledWith broken for method with overloads Sep 29, 2022
@mrazauskas
Copy link
Contributor

Thanks for report. Indeed the types are buggy here.

I was about to revert #13268 and #13278, because there are also other issues with them as well. For instance, current types don’t allow using asymmetric matchers.

@SimenB
Copy link
Member

SimenB commented Sep 30, 2022

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants