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

Conversation

lobsterkatie
Copy link
Contributor

Summary

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: https://github.com/facebook/jest/blob/a1829e9385bef6b007088a012bc3ceb0fa7867a8/packages/expect/src/asymmetricMatchers.ts#L59-L90

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
@codecov-commenter
Copy link

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.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks great, thanks!

@SimenB SimenB merged commit 7092dfb into jestjs:main Oct 19, 2021
@SimenB
Copy link
Member

SimenB commented Oct 19, 2021

https://github.com/facebook/jest/releases/tag/v27.3.1

@lobsterkatie lobsterkatie deleted the kmclb-make-any-match-missing-primitive-wrappers branch October 19, 2021 13:02
@github-actions
Copy link

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants