Configurable diff algorithm for snapshots
See original GitHub issueš Feature Proposal
Iād be able to run Jest with more human-friendly diff algorithms like āpatienceā or āhistogramā, for example:
jest --diff-algo patience
Maybe even better, Iād be able to āpipeā Jest output to a diff utility of my choice, for example:
jest --save-snapshots expected.txt actual.txt && code --diff expected.txt actual.txt
There are two main advantages to this second approach:
- Jest doesnāt need to implement all the possible algorithms
- I can plug in any diffing utility I like. For example, I could do this:
- Use Gitās algorithms:
... && git diff --no-index --diff-algorithm=histogram expected.txt actual.txt
- Use Beyond Compare:
... && bcompare expected.txt actual.txt
- Using specific utils for specific formats: json-diff, html-differ, etc.
- Use Gitās algorithms:
Motivation
Currently, snapshots are diffād using the Myers algorithm which is quite basic. I regularly encounter situations like this:
Snapshot:
{
"users": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" },
{ "id": 3, "name": "Charlie" }
],
"products": [
{ "id": 1, "name": "Product 1" },
{ "id": 2, "name": "Product 2" },
{ "id": 3, "name": "Product 3" },
{ "id": 4, "name": "Product 4" },
{ "id": 5, "name": "Product 5" }
]
}
Actual:
{
"users": null,
"products": [
{ "id": 1, "name": "Product 1" },
{ "id": 2, "name": "Product 2" },
{ "id": 3, "name": "Product 3" },
{ "id": 4, "name": "Product 4" },
{ "id": 5, "name": "Product 5" }
]
}
Diff:
- { "id": 1, "name": "Alice" }
+ { "id": 1, "name": "Product 1" }
...
- { "id": 2, "name": "Bob" }
+ { "id": 2, "name": "Product 2" }
Here is another example of how the diffs can be different: https://gist.github.com/roryokane/6f9061d3a60c1ba41237
Pitch
Why does this feature belong in the Jest core platform? Because user-land reporters like jest-stare (which supports side-by-side diff in HTML) only take the output of Jest, so if the primary output is not diffed smartly already, they wonāt help the situation too much.
Other
This feature request now includes some ideas from comments below, specifically, https://github.com/facebook/jest/issues/8998#issuecomment-537638906.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Top GitHub Comments
My colleague @JanVoracek actually had the very same idea today š. I was initially skeptical but he explained very nicely how it would work and that it would be a big improvement over string-based diffs. So itās quite exciting to hear that youāre considering it!
Thank you for a follow up https://github.com/facebook/jest/issues/8998#issuecomment-537638906 which is pure gold:
id
properties are like whatdiff
does to braces and newlines in code