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

Testing arity of functions no longer works #9792

Closed
nfantone opened this issue Apr 10, 2020 · 6 comments · Fixed by #9796
Closed

Testing arity of functions no longer works #9792

nfantone opened this issue Apr 10, 2020 · 6 comments · Fixed by #9796

Comments

@nfantone
Copy link

nfantone commented Apr 10, 2020

🐛 Bug Report

After the introduction of this commit, testing array-like structures and function arities no longer works with .toHaveLength. This used to behave correctly previously.

To Reproduce

Pass in a function as the argument to .toHaveLength

const myFunction = require('./my-function');

test('should return a function of arity 2', () => {
  expect(typeof myFunction).toBe('function');
  expect(myFunction).toHaveLength(2);
});

Test will fail with:

 FAIL  src/feature/my-function.test.js
  ● should return a function of arity 2

    expect(received).toHaveLength(expected)

    Matcher error: received value must have a length property whose value must be a number

    Received has type:  function
    Received has value: [Function myFunction]

Expected behavior

Test should pass. Also the returned error message ("have a length property whose value must be a number") is rather misleading as the argument being passed clearly conforms to that description. This is a breaking change in a minor release.

envinfo

  System:
    OS: macOS 10.15.4
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 12.16.1 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm
@nfantone
Copy link
Author

nfantone commented Apr 10, 2020

Also, since this was a breaking change introduced as a minor bump in a transitive dependency on the expect package, downgrading jest version to 25.2.7 does not resolve the problem. According to the changelog, 25.2.7 should not contain #9626 - but actually does.

@SimenB
Copy link
Member

SimenB commented Apr 10, 2020

Did you make sure expect was downgraded? It's in semver range


/cc @G-Rath, typeof function has a length property

@SimenB
Copy link
Member

SimenB commented Apr 10, 2020

@nfantone I'd personally find such an assertion a bit confusing, I'd recommend a toHaveArity that only accepted functions. It shouldn't have broken of course

@G-Rath
Copy link
Contributor

G-Rath commented Apr 11, 2020

Yeah that's fair enough - sorry about that.

TypeScript knows that functions have .length, so it's just a matter of adjusting the guard.

(I'm looking forward to having in be a guard someday so we can just do 'length' in x for things like this and call it a day 😬)

I'll make a PR.

@nfantone
Copy link
Author

nfantone commented Apr 12, 2020

@SimenB

Did you make sure expect was downgraded? It's in semver range

I know it is - that's why downgrading jest didn't solve the issue and was surprising. But that's the thing right there: I believe users shouldn't care. We don't typically install expect individually. We use jest as a module. You go to the release notes and you'd expect (no pun intended) things listed there to be true for jest as a tool. Which is not the case even though that's clearly how it is laid out. There is no mention of expect anywhere in there.

I'd personally find such an assertion a bit confusing, I'd recommend a toHaveArity that only accepted functions. It shouldn't have broken of course

I understand where you're coming from. And I agree: having a .toHaveArity matcher would be nice, semantically speaking! But at the end of the day, .toHaveArity would just be a duplicate of .toHaveLength and me and the team don't find that to be confusing at all. The concept of arity and function length are synonymous in the JS world (and have always been); albeit with some quirks.

@G-Rath

Yeah that's fair enough - sorry about that.
TypeScript knows that functions have .length, so it's just a matter of adjusting the guard.

Hey, no need to apologise at all! Great work contributing to an awesome project like jest. Keep at it. And thanks for the PR!

@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 May 11, 2021
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.

3 participants