You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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.
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.
🐛 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
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 fornull
.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
, and1n.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 anObject
is also misleading, in thatany(Object)
intuitively impliesanything()
, even though the former matchesnull
and the latter does not. This intuition is reflected in the error message forexpect.any(undefined)
("any() expects to be passed a constructor function. Please pass one or use anything() to match any object"), which suggests usinganything()
as though it were equivalent to "any(*)
".Jasmine
In the
symbol
andnull
cases, but not thebigint
case, Jest deviates from Jasmine's behavior; see Any.js.Link to repl or repo (highly encouraged)
Minimal reproduction repository.
envinfo
The text was updated successfully, but these errors were encountered: