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

fix(snapshot): bring back support for array property matchers #14025

Merged
merged 4 commits into from
Apr 9, 2023

Conversation

bakasmarius
Copy link
Contributor

@bakasmarius bakasmarius commented Mar 23, 2023

Summary

Fixes #13352 which is a regression that happened when #13263 fixed #13134.

Technically, this will now throw an error again on the case mentioned in #13134:
expect([{}]).toMatchSnapshot([expect.anything()])

But I don't see a valid use case for this knowing that these cases worked fine before #13263:

expect([{}]).toMatchSnapshot(expect.anything())
expect([{ foo: 'bar' }]).toMatchSnapshot([{ foo: expect.anything() }])


Test plan

Tests added

@facebook-github-bot
Copy link
Contributor

Hi @bakasmarius!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@mrazauskas
Copy link
Contributor

Technically, this will now throw an error again on the case mentioned in #13134: expect([{}]).toMatchSnapshot([expect.anything()])

Unfortunately the error is hard to understand: "Asymmetric matcher Object does not implement toAsymmetricMatcher()"

But I don't see a valid use case for this knowing that these cases worked fine before #13263:

A user was reporting an error, which was hard to understand. The fix was not about one or another use case.

@bakasmarius
Copy link
Contributor Author

Technically, this will now throw an error again on the case mentioned in #13134: expect([{}]).toMatchSnapshot([expect.anything()])

Unfortunately the error is hard to understand: "Asymmetric matcher Object does not implement toAsymmetricMatcher()"

But I don't see a valid use case for this knowing that these cases worked fine before #13263:

A user was reporting an error, which was hard to understand. The fix was not about one or another use case.

Does this mean my PR won't be approved unless it also makes the error mentioned in #13134 more clear?
If so, could somebody please add a failing test case to my PR (or just paste it here) so I could implement a fix for it?
I'm not that familiar with Jest's inner details so I wasn't able to successfully write such a case myself.
I also spent half a day today debugging this case from my own project and trying to narrow it down but I think I'm starting to lose my sanity so I'll give it a rest for a while 😄
Hopefully, someone could point me in the right direction so I could move forward with this PR.

@bakasmarius
Copy link
Contributor Author

I added some changes (test cases included) that fix #13134 - it doesn't throw any error anymore, because expect.anything() and expect.any(type) just work inside an array now.

These cases (and combinations of them) now also work:
expect([{}]).toMatchSnapshot([expect.anything()])
expect([[]]).toMatchSnapshot([expect.anything()])
expect([5]).toMatchSnapshot([expect.anything()])
expect(['some words']).toMatchSnapshot([expect.anything()])
expect([true, false]).toMatchSnapshot([expect.anything(), expect.anything()])

expect([{}]).toMatchSnapshot([expect.any(Object)])
expect([[]]).toMatchSnapshot([expect.any(Array)])
expect([5]).toMatchSnapshot([expect.any(Number)])
expect(['some words']).toMatchSnapshot([expect.any(String)])
expect([true, false]).toMatchSnapshot([expect.any(Boolean), expect.any(Boolean)])

@SimenB
Copy link
Member

SimenB commented Apr 8, 2023

Technically, this will now throw an error again on the case mentioned in #13134:
expect([{}]).toMatchSnapshot([expect.anything()])

As long as the error is specific, I think that's fine.

EDIT: hah, just noticed your latest comment 😅

Copy link
Contributor

@mrazauskas mrazauskas left a comment

Choose a reason for hiding this comment

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

Great job!

Looks like this patch makes .toMatchSnapshot() matcher work the same as .toMatchObject() does. I checked out the branch and did some manual testing. Just wanted to compare the behaviour and to try out few edge cases. All went smooth.

One detail – the PR is missing change log entry. It could sound "feat(jest-snapshot): support array inputs to .toMatchSnapshot() matcher" or so.

packages/jest-snapshot/src/utils.ts Outdated Show resolved Hide resolved
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.

thanks!

@SimenB SimenB merged commit 93c1967 into jestjs:main Apr 9, 2023
@bakasmarius bakasmarius deleted the bring-back-array-property-matchers branch April 9, 2023 14:58
DmitryMakhnev pushed a commit to DmitryMakhnev/jest that referenced this pull request May 5, 2023
@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 10, 2023
@SimenB
Copy link
Member

SimenB commented Jul 4, 2023

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants