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

Share more logic between object equality and arrays #11061

Merged
merged 4 commits into from
Apr 23, 2021

Conversation

dubzzz
Copy link
Contributor

@dubzzz dubzzz commented Feb 6, 2021

Summary

While investigating the reasons behind #11055, I found a possible way to factorize the logics of object comparison with array comparison. As it seems to simplify and reduce a bit the amount of code needed, I thought it would be good to let share it with you.

Even if this suggestion clearly enhances the case of the issue #11055, it still does not fully fix it as error message generation seems to still iterate over all the holes (did not impacted it in this PR).

Test plan

Tests have been added for sparse arrays in order to ensure the PR does not introduce unwanted regressions.

As you will see in the snapshots, snapshots for sparse arrays are not perfect as they let users think that the array was full of undefined values. This PR does not try to change this behaviour.

@dubzzz
Copy link
Contributor Author

dubzzz commented Feb 6, 2021

Just in case:

I double-checked locally and confirm that the tests added by this PR were green before the code change and are still green now. By adding them, I wanted to make sure I did not change how Jest considered sparse arrays equality.

Comment on lines 2156 to 2157
<g>- Expected - 1</>
<r>+ Received + 1</>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Expected / Received seems to be wrong there 🤔

Nonetheless it does not seem related to my change as I got the same snapshot before my dev.

Copy link
Member

Choose a reason for hiding this comment

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

wrong how? they reflect the + and - in the diff below, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

<d>  Array [</>
<d>    undefined,</>
<d>    undefined,</>
<g>-   2,</>
<r>+   1,</>
<d>    undefined,</>
<d>  ]</>

It looks that the diff should be:

<g>- Expected  - 2</>
<r>+ Received  + 1</>

(clearly not sure of me on that one)

Copy link
Member

Choose a reason for hiding this comment

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

ah, I see what you mean. You misunderstand (I think 😛) - the heading is a counter of the number of - and + lines, it's not an extracted part of the diff itself

(Object.getOwnPropertySymbols(obj) as Array<any>).filter(
symbol =>
(Object.getOwnPropertyDescriptor(obj, symbol) as PropertyDescriptor)
.enumerable,
Copy link
Member

Choose a reason for hiding this comment

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

thanks prettier

Copy link
Contributor

Choose a reason for hiding this comment

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

I blame the people who put enumerable symbol-keyed properties on objects in the first place ^^

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! would like some extra 👀 on this, although passing tests and less code is always good 🙂

@dubzzz
Copy link
Contributor Author

dubzzz commented Feb 9, 2021

@SimenB By the way I'm working on a PR to have more appropriate snapshots for sparse arrays. I'll certainly have to sync with @pedrottimark regarding that point

@jeysal
Copy link
Contributor

jeysal commented Feb 9, 2021

To be clear, this does not (or at least is not expected to) change behavior, before and after this change toStrictEqual cares about undefined vs missing elements, while toEqual doesn't? In that case I'm okay 😅

@dubzzz
Copy link
Contributor Author

dubzzz commented Mar 1, 2021

To be clear, this does not (or at least is not expected to) change behavior, before and after this change toStrictEqual cares about undefined vs missing elements, while toEqual doesn't? In that case I'm okay 😅

@jeysal I confirm, if I revert my change in packages/expect/src/jasmineUtils.ts, the tests I add with this PR are green. In other words, the added tests were green before and are still green now. They seem to cover the cases with arrays including undefined into them, do you want me to add some extra tests?

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

I trust in our test suite and think this is good to go ^^

@dubzzz
Copy link
Contributor Author

dubzzz commented Apr 21, 2021

@SimenB Do you want me to update the PR? If yes, do you prefer rebase and force push OR merge?

@SimenB
Copy link
Member

SimenB commented Apr 22, 2021

Sorry, forgot about this one. Just merging in master sounds good to me 🙂 (easier to see changes between pushes when there's no force pushing)

@codecov-commenter
Copy link

Codecov Report

Merging #11061 (12c9dbb) into master (c8b1835) will decrease coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11061      +/-   ##
==========================================
- Coverage   64.19%   64.14%   -0.05%     
==========================================
  Files         308      308              
  Lines       13532    13514      -18     
  Branches     3299     3293       -6     
==========================================
- Hits         8687     8669      -18     
  Misses       4131     4131              
  Partials      714      714              
Impacted Files Coverage Δ
packages/expect/src/jasmineUtils.ts 90.72% <100.00%> (-1.46%) ⬇️

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 c8b1835...12c9dbb. 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.

very nice!

@SimenB SimenB merged commit a28f14a into jestjs:master Apr 23, 2021
@dubzzz
Copy link
Contributor Author

dubzzz commented Apr 23, 2021

Thanks a lot 👍

@dubzzz dubzzz deleted the try-share-more-array-obj branch April 23, 2021 06:52
@SimenB
Copy link
Member

SimenB commented Apr 23, 2021

Haha, thank you! 😀

This started out for #11055, what's the status with this and the pretty-format fix?

@dubzzz
Copy link
Contributor Author

dubzzz commented Apr 23, 2021

I think I still have one part of the code of jest to investigate, but we should be close from the full fix.

If I'm not wrong my current fix on pretty-format still iterates over the whole size as we print [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,] for sparse arrays. We should probably print something like Object.assign(Array(10000), { 50: "a-value-for-50" }) or anything else not requiring to iterate and print all the holes. Here is how I print them in fast-check: https://github.com/dubzzz/fast-check/blob/main/test/unit/utils/stringify.spec.ts#L121

I believe the printed value is always computed by expect if I am not wrong 🤔 (even if no error)
So another solution might be to just computed it if needed.

@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 24, 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.

5 participants