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

expect: any() doesn't match symbols or bigints, does match null #10179

Closed
ninevra opened this issue Jun 19, 2020 · 1 comment · Fixed by #10223
Closed

expect: any() doesn't match symbols or bigints, does match null #10179

ninevra opened this issue Jun 19, 2020 · 1 comment · Fixed by #10223

Comments

@ninevra
Copy link
Contributor

ninevra commented Jun 19, 2020

🐛 Bug Report

expect.any(Symbol) does not match Symbols, either well-known or otherwise.

expect.any(BigInt) does not match bigints.

expect.any(Object) does match null.

I can probably submit a pull request fixing this if one is wanted.

To Reproduce

import expect from 'expect';

expect(Symbol()).toEqual(expect.any(Symbol)); // throws

expect(1n).toEqual(expect.any(BigInt)); // throws

expect(null).toEqual(expect.any(Object)); // doesn't throw

Expected behavior

Summary

expect.any(Symbol) should be true for any symbol.

expect.any(BigInt) should be true for any bigint.

expect.any(Object) should be false for null.

Rationale

Symbol and BigInt

The documentation for any() is rather imprecise. However, "matches anything that was created with the given constructor" sounds like it should work for Symbols and bigints; although they are primitives and not technically created with a constructor, Object.getPrototypeOf(Symbol()) === Symbol.prototype, Symbol().constructor === Symbol, Object.getPrototypeOf(1n) === BigInt.prototype, and 1n.constructor === BigInt.

Further, any() matches all other primitive types in this way (i.e. expect(1).toEqual(expect.any(Number)) does not throw). If symbols and bigints are intentionally treated differently than other primitives, then that should be documented.

null

"Matches anything that was created with the given constructor" sounds like it should exclude null, which is not produced by any constructor and does not have a prototype (as it is the terminus of all prototype chains).

The ecmascript standard defines null as the "primitive value that represents the intentional absence of any object value", suggesting that null is not "any Object".

Successfully matching null as an Object is also misleading, in that any(Object) intuitively implies anything(), even though the former matches null and the latter does not. This intuition is reflected in the error message for expect.any(undefined) ("any() expects to be passed a constructor function. Please pass one or use anything() to match any object"), which suggests using anything() as though it were equivalent to "any(*)".

Jasmine

In the symbol and null cases, but not the bigint case, Jest deviates from Jasmine's behavior; see Any.js.

Link to repl or repo (highly encouraged)

Minimal reproduction repository.

envinfo

npx: installed 1 in 1.23s

  System:
    OS: Linux 4.15 Ubuntu 18.04.4 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz
  Binaries:
    Node: 14.4.0 - ~/.nvm/versions/node/v14.4.0/bin/node
    npm: 6.14.5 - ~/.nvm/versions/node/v14.4.0/bin/npm
  npmPackages:
    jest: ^26.0.1 => 26.0.1 

ninevra added a commit to ninevra/jest that referenced this issue Jul 2, 2020
ninevra added a commit to ninevra/jest that referenced this issue Jul 2, 2020
thymikee pushed a commit that referenced this issue Jul 2, 2020
* fix: fix any() for symbols, bigints, null (#10179)

* revert change to null handling in any(Object)
@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.

1 participant