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] Fix .any() checks on primitive wrapper classes #11976


Copy link

@lobsterkatie lobsterkatie commented Oct 18, 2021


Currently, the .any() matcher correctly detects instances of the wrapper classes for numbers and strings:

expect(new Number(1231)).toEqual(expect.any(Number)) // pass
expect(new String('Maisey is a good dog')).toEqual(expect.any(String)) // pass

which makes those tests equivalent to the following instanceof tests (which they should be):

expect(new Number(1231) instanceof Number).toBe(true); // pass
expect(new String('Maisey is a good dog') instanceof String).toBe(true); // pass

At the moment, though, the same cannot be said for instances of the wrapper classes for booleans, symbols, and bigints (even though the corresponding instanceof tests correctly pass):

expect(new Boolean(true)).toEqual(expect.any(Boolean)); // fail
expect(new Boolean(true) instanceof Boolean).toBe(true); // pass

expect(Object(Symbol('CharlieIsAlsoAGoodDog'))).toEqual(expect.any(Symbol)); // fail
expect(Object(Symbol('CharlieIsAlsoAGoodDog')) instanceof Symbol).toBe(true); // pass

expect(Object(BigInt(1121n))).toEqual(expect.any(BigInt)); // fail
expect(Object(BigInt(1121n)) instanceof BigInt).toBe(true); // pass

(Note: The Object(...) syntax is necessary because neither symbols nor bigints are constructible, and therefore don't work with the new keyword.)

This is because in the any() matcher, there are instanceof checks for strings and numbers, but not the other primitives:

asymmetricMatch(other: unknown) {
if (this.sample == String) {
return typeof other == 'string' || other instanceof String;
if (this.sample == Number) {
return typeof other == 'number' || other instanceof Number;
if (this.sample == Function) {
return typeof other == 'function' || other instanceof Function;
if (this.sample == Object) {
return typeof other == 'object';
if (this.sample == Boolean) {
return typeof other == 'boolean';
/* global BigInt */
if (this.sample == BigInt) {
return typeof other == 'bigint';
if (this.sample == Symbol) {
return typeof other == 'symbol';
return other instanceof this.sample;

This PR fixes that by adding in the missing instanceof checks.

Test plan

There is a new unit test in the asymetric matcher test file which checks both the currently working cases and the currently broken ones.

@lobsterkatie lobsterkatie changed the title add instanceof checks to primitives missing them [expect] Add instanceof checks to primitives missing them in .any() Oct 18, 2021
@lobsterkatie lobsterkatie changed the title [expect] Add instanceof checks to primitives missing them in .any() [expect] Fix .any() checks on primitive wrapper classes Oct 18, 2021
Copy link

@codecov-commenter codecov-commenter commented Oct 18, 2021

Codecov Report

Merging #11976 (52b5652) into main (2e2b17a) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #11976   +/-   ##
  Coverage   68.74%   68.74%           
  Files         323      323           
  Lines       16649    16649           
  Branches     4805     4808    +3     
  Hits        11445    11445           
  Misses       5171     5171           
  Partials       33       33           
Impacted Files Coverage Δ
packages/expect/src/asymmetricMatchers.ts 83.05% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2e2b17a...52b5652. Read the comment docs.

SimenB approved these changes Oct 19, 2021
Copy link

@SimenB SimenB left a comment

this looks great, thanks!

@SimenB SimenB merged commit 7092dfb into facebook:main Oct 19, 2021
29 of 30 checks passed
Copy link

@SimenB SimenB commented Oct 19, 2021

@lobsterkatie lobsterkatie deleted the kmclb-make-any-match-missing-primitive-wrappers branch Oct 19, 2021
Copy link

@github-actions github-actions bot commented Nov 19, 2021

This pull request 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 Nov 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants