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

[Feature]: jest-diff should allow configuring sorting of object keys #11938

Closed
vjpr opened this issue Oct 7, 2021 · 6 comments · Fixed by #11992
Closed

[Feature]: jest-diff should allow configuring sorting of object keys #11938

vjpr opened this issue Oct 7, 2021 · 6 comments · Fixed by #11992

Comments

@vjpr
Copy link

vjpr commented Oct 7, 2021

🚀 Feature Proposal

Allow configuring the object key sorting for jest-diff. At present it sorts alphabetically. It should allow using the existing sorting, or providing a function to sort before visual diff is generated.

Motivation

I am trying to diff two package.json files, and it keys are being sorted alphabetically when displaying the diff. This makes it hard to eyeball, because keys are not where you think they will be.

Example

No response

Pitch

There is no way around it with the way jest-diff is designed.

@SimenB
Copy link
Member

SimenB commented Oct 7, 2021

PR welcome!

@D-Andreev
Copy link
Contributor

Hi,

From what I see the sorting is happening here -> https://github.com/facebook/jest/blob/main/packages/pretty-format/src/collections.ts#L12.
I'm just wondering why do the object properties need to be sorted at all, don't you always want them in the same order as they're passed?
Can't the sorting be removed altogether, what would be a use case where you want a specific sorting?

@SimenB
Copy link
Member

SimenB commented Oct 19, 2021

I believe we've always sorted there. The reason is I assume because it's easier to read objects that are sorted alphabetically, so it seems natural something which prints a "pretty" version of an object would do so

@D-Andreev
Copy link
Contributor

That makes sense @SimenB. I'm not aware of the other uses of pretty-format, besides being used from jest-diff.

So the approach for implementing this feature would be to add an option in jest-diff for a custom sorting function (which will be the default sort if one is not passed) and passing it to pretty-format so the keys can be sorted. Does this sound good? Can I open a PR for this?

@SimenB
Copy link
Member

SimenB commented Oct 20, 2021

Yeah, that seems like a reasonable approach! PR very much welcome 🙂

@github-actions
Copy link

This issue 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 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants