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 regression in toThrow for message prop and asymmetric matchers #7697

Merged
merged 5 commits into from
Jan 25, 2019

Conversation

pedrottimark
Copy link
Contributor

Summary

Fixes #7692

Test plan

All existing tests pass

Added 8 tests and 4 snapshots for each of the following scenarios:

  • asymmetric any-Class: .toThrow(expect.any(Class))
  • asymmetric anything: .toThrow(expect.anything())
  • asymmetric no-symbol: .toThrow(customMatcher)
  • asymmetric objectContaining: .toThrow(expect.objectContaing(props))
  • error-message: expect(() => { throw { message: 'whatever'}; })

This quote from original pull request still applies to any-Class scenario:

Depending on how classes that extend Error set name property of instances, that report can still seem confusing, but I think that will have to wait for a future pull request to sort out :(

See also pictures compared to baseline Jest 23.6 in following comments

@codecov-io
Copy link

codecov-io commented Jan 24, 2019

Codecov Report

Merging #7697 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7697      +/-   ##
==========================================
+ Coverage   68.22%   68.26%   +0.04%     
==========================================
  Files         251      251              
  Lines        9672     9678       +6     
  Branches        6        6              
==========================================
+ Hits         6599     6607       +8     
+ Misses       3071     3069       -2     
  Partials        2        2
Impacted Files Coverage Δ
packages/expect/src/toThrowMatchers.js 97.59% <100%> (+2.78%) ⬆️

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 28971c5...7e91a74. Read the comment docs.

@pedrottimark
Copy link
Contributor Author

Pictures of baseline 23.6 at left and improved at right

Received error object which has message but not name nor stack properties:

compared 7692

expect.anything()

compared anything

expect.any(Class)

compared any-class

expect.objectContaining(props)

compared objectcontaining

Expected value not helpful for custom asymmetric matcher without $$typeof prop:

compared no-symbol

CHANGELOG.md Outdated Show resolved Hide resolved
return {
hasMessage,
isError: false,
message: hasMessage ? e.message : typeof e === 'string' ? e : String(e),
Copy link
Contributor

Choose a reason for hiding this comment

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

Would prefer

Suggested change
message: hasMessage ? e.message : typeof e === 'string' ? e : String(e),
message: hasMessage ? e.message : String(e),

for better readability.
I assume String('already a string') is probably optimized well enough to not be significantly more expensive than the typeof check anyway

Copy link
Member

@SimenB SimenB Jan 25, 2019

Choose a reason for hiding this comment

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

it's not hot code anyways, so doesn't matter. So +1 for the change

return {message, pass};
};

type AsymmetricMatcher = {
Copy link
Contributor

@jeysal jeysal Jan 25, 2019

Choose a reason for hiding this comment

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

Have you tried putting this here:
https://github.com/facebook/jest/blob/57aeaa21cef41755740e9a8698118fc7947e37a1/types/Matchers.js#L46
In case that causes type errors all over the place (there's probably a reason why it's Object 😄) we should probably save that costly refactoring for another time after Jest 24 is released, but maybe it works just fine?

@rubennorte
Copy link
Contributor

rubennorte commented Jan 25, 2019

Looks good. Please address the comments and I think it's good to merge. Thanks for fixing this!

};
const getThrown = (e: any): Thrown => {
if (
e !== null &&
Copy link
Member

Choose a reason for hiding this comment

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

e != null can replace the separate null and undefined checks

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd say being explicit is better, but whatever in this case 🤷‍♂️

}

const hasMessage =
e !== null && e !== undefined && typeof e.message === 'string';
Copy link
Member

Choose a reason for hiding this comment

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

same here, e != null is the same as the first 2. Could also be e && typeof e.message === 'string', not sure

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.

Looks good (note other reviews)

@SimenB SimenB merged commit 634e5a5 into jestjs:master Jan 25, 2019
@SimenB
Copy link
Member

SimenB commented Jan 25, 2019

Can do a follow-up with nitpick fixes later

@pedrottimark pedrottimark deleted the toThrow-toEqual branch January 25, 2019 13:54
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
Development

Successfully merging this pull request may close these issues.

toThrow does not behave like toEqual for non-errors
7 participants