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: Return false from asymmetric matchers if received value isn’t string #7107

Merged
merged 5 commits into from
Dec 24, 2018

Conversation

pedrottimark
Copy link
Contributor

Summary

Fixes #7055

Problem: Although it seemed to make sense at the time to throw an error if received value isn’t string when asymmetric matcher is value of property in .toEqual assertion, it contradicts reasonable expectation in other scenarios:

  • .toBeCalledWith assertion for polymorphic function can throw even if at least one call has an argument that matches
  • ordinary matcher and inverse matcher can both throw instead of returning opposite boolean values

The constructors still throw if the expected value has incorrect type.

Solution:

  • Move isA('String', other) into conditional expression of .stringContaining
  • Delete type check from .stringMatching because regular expression test method returns false if argument isn’t string

Test plan

Rewrite 2 tests 'StringContaining throws for non-strings' as:

  • 'StringContaining throws if expected value is not string'
  • 'StringContaining returns false if received value is not string' (failed before code change)

Rewrite 2 tests 'StringNotContaining throws for non-strings' as:

  • 'StringNotContaining throws if expected value is not string'
  • 'StringNotContaining returns true if received value is not string' (failed before code change)

Rewrite 1 test StringMatching throws for non-strings and non-regexps as:

  • StringMatching throws if expected value is neither string nor regexp

Rewrite 1 test 'StringMatching throws for non-string actual values' as:

  • 'StringMatching returns false if received value is not string' (failed before code change)

Rewrite 1 test StringNotMatching throws for non-strings and non-regexps as:

  • StringNotMatching throws if expected value is neither string nor regexp

Rewrite 1 test 'StringNotMatching throws for non-string actual values' as:

  • 'StringNotMatching returns true if received value is not string' (failed before code change)

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.

Yeah, I agree this makes sense!

@codecov-io
Copy link

Codecov Report

Merging #7107 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7107      +/-   ##
==========================================
- Coverage   66.67%   66.66%   -0.02%     
==========================================
  Files         253      253              
  Lines       10597    10593       -4     
  Branches        3        4       +1     
==========================================
- Hits         7066     7062       -4     
  Misses       3530     3530              
  Partials        1        1
Impacted Files Coverage Δ
packages/expect/src/asymmetric_matchers.js 92.55% <100%> (-0.31%) ⬇️

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 d59a4d3...17b67af. Read the comment docs.

@pedrottimark
Copy link
Contributor Author

Uh oh, the asymmetry of one matcher with && and the other without (pardon pun :) is a mistake.

Added failing test 'StringMatching returns false even if coerced non-string received value matches pattern' and then corrected the code so that the test passes.

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

@pedrottimark mind resolving conflicts? :)

@SimenB SimenB merged commit a733592 into jestjs:master Dec 24, 2018
@pedrottimark pedrottimark deleted the asymmetric-string branch December 24, 2018 13:02
captain-yossarian pushed a commit to captain-yossarian/jest that referenced this pull request Jul 18, 2019
@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 May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants